1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slackbot001\SlashCommandHandlers\Jobs; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
use Slackbot001\SessionManager; |
7
|
|
|
use Spatie\SlashCommand\Attachment; |
8
|
|
|
use Spatie\SlashCommand\AttachmentAction; |
9
|
|
|
use Spatie\SlashCommand\AttachmentField; |
10
|
|
|
use Spatie\SlashCommand\Jobs\SlashCommandResponseJob; |
11
|
|
|
|
12
|
|
|
class SearchTDTicketJob extends SlashCommandResponseJob |
13
|
|
|
{ |
14
|
|
|
// notice here that Laravel will automatically inject dependencies here |
15
|
|
|
public function handle() |
16
|
|
|
{ |
17
|
|
|
$build = \Tremby\LaravelGitVersion\GitVersionHelper::getVersion(); |
18
|
|
|
|
19
|
|
|
$userSession = new SessionManager(); |
20
|
|
|
$TDinstance = $userSession->setupSession($this->request->userId, $this->request->token); |
21
|
|
|
|
22
|
|
View Code Duplication |
if ($TDinstance->checkToken()) { |
|
|
|
|
23
|
|
|
Log::info('CP_SearchTDTicketJob: There is a token.'); |
24
|
|
|
$auth = true; |
25
|
|
|
$tickets = $TDinstance->searchTicketsName($this->request->text); |
26
|
|
|
$userSession()->increment('td_searches'); |
27
|
|
|
} else { |
28
|
|
|
Log::info('CP_SearchTDTicketJob: No token.'); |
29
|
|
|
$auth = false; |
30
|
|
|
$tickets = null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$opencount = 0; |
34
|
|
|
if (!count($tickets) && $auth) { |
35
|
|
|
return $this->respondToSlack('🕵️ I could not find any tickets with that requestor name.')->send(); |
|
|
|
|
36
|
|
|
} elseif (!$auth) { |
37
|
|
|
return $this->respondToSlack("🕵️ I wasn't authorized to access TeamDynamix.")->send(); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
foreach ($tickets as $ticket) { |
41
|
|
|
if ($ticket['StatusName'] != 'Closed' && $ticket['StatusName'] != 'Cancelled') { |
42
|
|
|
$opencount++; |
43
|
|
|
$date = date('F jS, Y', strtotime($ticket['CreatedDate'])); |
44
|
|
|
//$ticketURL = (string) $TDinstance->rootAppsUrl().'Tickets/TicketDet?TicketID='.$ticket['ID']; |
45
|
|
|
$ticketDescription = $TDinstance->ticket($ticket['ID']); //Need to do this because in the TD API, searched ticket's desctiption is empty. |
46
|
|
|
|
47
|
|
|
$attachmentFields[] = AttachmentField::create('Opened On', $date); |
48
|
|
|
$attachmentFields[] = AttachmentField::create('Requested By', $ticket['RequestorName'])->displaySideBySide(); |
49
|
|
|
$attachmentFields[] = AttachmentField::create('Assigned To', $ticket['ResponsibleFullName'])->displaySideBySide(); |
50
|
|
|
|
51
|
|
|
$attachmentAction = AttachmentAction::create('action', '📋 Show Ticket', 'button') |
52
|
|
|
->setValue($ticket['ID']) |
53
|
|
|
->setStyle('good'); |
54
|
|
|
|
55
|
|
|
$action = $attachmentAction->toArray(); |
56
|
|
|
$ticketattachments[] = Attachment::create() |
57
|
|
|
->setFallback('Ticket '.$ticket['ID']) |
58
|
|
|
->setTitle('🏷 '.$ticket['ID']) |
59
|
|
|
->setText($ticketDescription['Description']) |
60
|
|
|
->setFields($attachmentFields) |
61
|
|
|
->setCallbackId($ticket['ID']) |
62
|
|
|
->addAction($action) |
63
|
|
|
->setColor('good') |
64
|
|
|
->setFooter('CP_TD/S API bot microservice v'.$TDinstance->getVersion().' | Build: '.$build); |
65
|
|
|
} |
66
|
|
|
unset($attachmentFields); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($opencount > 0) { |
70
|
|
|
$this |
71
|
|
|
->respondToSlack('🕵️ I found '.$opencount.' open tickets.') |
72
|
|
|
->withAttachments($ticketattachments) |
|
|
|
|
73
|
|
|
->displayResponseToEveryoneOnChannel() |
74
|
|
|
->send(); |
75
|
|
|
} elseif ($opencount > 100) { |
76
|
|
|
$this |
77
|
|
|
->respondToSlack('🕵️ I found '.$opencount.' open tickets, but Slack only supports 100 attachments. Sorry!') |
78
|
|
|
->displayResponseToEveryoneOnChannel() |
79
|
|
|
->send(); |
80
|
|
|
} else { |
81
|
|
|
return $this |
|
|
|
|
82
|
|
|
->respondToSlack('🕵️ I found '.$opencount.' open tickets.') |
83
|
|
|
->withAttachment(Attachment::create() |
84
|
|
|
->setFallback('🕵️ I found no open tickets.') |
85
|
|
|
->setColor('warning') |
86
|
|
|
->setFooter('CP_TD/S API bot microservice v'.$TDinstance->getVersion().' | Build: '.$build) |
87
|
|
|
) |
88
|
|
|
->displayResponseToEveryoneOnChannel() |
89
|
|
|
->send(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.