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\AttachmentField; |
9
|
|
|
use Spatie\SlashCommand\Jobs\SlashCommandResponseJob; |
10
|
|
|
|
11
|
|
|
class ShowTDTicketJob extends SlashCommandResponseJob |
12
|
|
|
{ |
13
|
|
|
// notice here that Laravel will automatically inject dependencies here |
14
|
|
|
public function handle() |
15
|
|
|
{ |
16
|
|
|
$build = \Tremby\LaravelGitVersion\GitVersionHelper::getVersion(); |
17
|
|
|
|
18
|
|
|
$userSession = new SessionManager(); |
19
|
|
|
$TDinstance = $userSession->setupSession($this->request->userId, $this->request->token); |
20
|
|
|
|
21
|
|
View Code Duplication |
if ($TDinstance->checkToken()) { |
|
|
|
|
22
|
|
|
Log::info('CP_ShowTDTicketJob: There is a token.'); |
23
|
|
|
$auth = true; |
24
|
|
|
$ticket = $TDinstance->ticket($this->request->text); |
25
|
|
|
} else { |
26
|
|
|
Log::info('CP_ShowTDTicketJob: No token.'); |
27
|
|
|
$auth = false; |
28
|
|
|
$ticket = null; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if ($ticket && $auth) { |
32
|
|
|
$date = date('F jS, Y', strtotime($ticket['CreatedDate'])); |
33
|
|
|
$ticketURL = (string) $TDinstance->rootAppsUrl().'Tickets/TicketDet?TicketID='.$ticket['ID']; |
34
|
|
|
$assets = $TDinstance->searchAssets($ticket['ID']); |
35
|
|
|
$assetnames = 'No Assets'; |
36
|
|
|
$userSession()->increment('td_tickets'); |
37
|
|
|
|
38
|
|
|
if (count($assets)) { |
39
|
|
|
$assetnames = ''; |
40
|
|
|
foreach ($assets as $asset) { |
41
|
|
|
$assetnames = $assetnames.$asset['Name'].', '; |
42
|
|
|
} |
43
|
|
|
$assetnames = substr($assetnames, 0, -2); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$attachmentFields[] = AttachmentField::create('Description', $ticket['Description']); |
|
|
|
|
47
|
|
|
$attachmentFields[] = AttachmentField::create('Requestor', $ticket['RequestorName']."\n".$ticket['RequestorEmail']."\n".$ticket['RequestorPhone'])->displaySideBySide(); |
48
|
|
|
$attachmentFields[] = AttachmentField::create('Location', $ticket['LocationName']."\n".$ticket['LocationRoomName'])->displaySideBySide(); |
49
|
|
|
$attachmentFields[] = AttachmentField::create('Service', $ticket['ServiceName'])->displaySideBySide(); |
50
|
|
|
$attachmentFields[] = AttachmentField::create('Status', $ticket['StatusName'])->displaySideBySide(); |
51
|
|
|
$attachmentFields[] = AttachmentField::create('Assets', $assetnames)->displaySideBySide(); |
52
|
|
|
$attachmentFields[] = AttachmentField::create('Assigned To', $ticket['ResponsibleFullName']."\n".$ticket['ResponsibleEmail'])->displaySideBySide(); |
53
|
|
|
$attachmentFields[] = AttachmentField::create('Date Opened', $date)->displaySideBySide(); |
54
|
|
|
|
55
|
|
|
$this |
56
|
|
|
->respondToSlack("🕵️ Here's what I found about ticket `{$this->request->text}`") |
57
|
|
|
->withAttachment(Attachment::create() |
58
|
|
|
->setFallback("{$ticket['Title']}") |
59
|
|
|
->setColor('#439FE0') |
60
|
|
|
->setTitle("{$ticket['Title']}") |
61
|
|
|
->setTitleLink("{$ticketURL}") |
62
|
|
|
->setFields($attachmentFields) |
63
|
|
|
->setFooter('CP_TD/S API bot microservice v'.$TDinstance->getVersion().' | Build: '.$build) |
64
|
|
|
) |
65
|
|
|
->displayResponseToEveryoneOnChannel() |
66
|
|
|
->send(); |
67
|
|
|
} elseif (!$ticket && $auth) { |
68
|
|
|
$this->respondToSlack('🕵️ I could not find that ticket.')->send(); |
69
|
|
|
} else { |
70
|
|
|
$this->respondToSlack("🕵️ I wasn't authorized to access TeamDynamix.")->send(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
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.