| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Models\Meta; |
| 7 |
use FluentSupport\App\Models\Ticket; |
| 8 |
use FluentSupport\App\Modules\PermissionManager; |
| 9 |
|
| 10 |
class TicketHelper |
| 11 |
{ |
| 12 |
/** |
| 13 |
* getActivity method will return the activity in a ticket by agent |
| 14 |
* @param $ticketId |
| 15 |
* @param false $currentAgentId |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
public static function getActivity($ticketId, $currentAgentId = false) |
| 19 |
{ |
| 20 |
//Get the ticket meta information |
| 21 |
$meta = Meta::where('object_type', 'ticket_meta') |
| 22 |
->where('key', '_live_activity') |
| 23 |
->where('object_id', $ticketId) |
| 24 |
->first(); |
| 25 |
|
| 26 |
$activities = []; |
| 27 |
if ($meta) { |
| 28 |
$activities = maybe_unserialize($meta->value); |
| 29 |
} |
| 30 |
|
| 31 |
foreach ($activities as $index => $activity) { |
| 32 |
if ((time() - $activity) > 60) { |
| 33 |
unset($activities[$index]); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if (!$currentAgentId) { |
| 38 |
return self::getAgentsInfoFromActivities($activities); |
| 39 |
} |
| 40 |
|
| 41 |
$activities[$currentAgentId] = time(); |
| 42 |
|
| 43 |
if ($meta) { |
| 44 |
$meta->value = maybe_serialize($activities); |
| 45 |
$meta->save(); |
| 46 |
} else { |
| 47 |
Meta::insert([ |
| 48 |
'object_type' => 'ticket_meta', |
| 49 |
'key' => '_live_activity', |
| 50 |
'object_id' => $ticketId, |
| 51 |
'value' => maybe_serialize($activities) |
| 52 |
]); |
| 53 |
} |
| 54 |
|
| 55 |
return self::getAgentsInfoFromActivities($activities); |
| 56 |
} |
| 57 |
|
| 58 |
public static function getAgentsInfoFromActivities($activities) |
| 59 |
{ |
| 60 |
if (!$activities) { |
| 61 |
return []; |
| 62 |
} |
| 63 |
|
| 64 |
return Agent::select(['email', 'first_name', 'last_name']) |
| 65 |
->whereIn('id', array_keys($activities)) |
| 66 |
->get(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* removeFromActivities method will remove the old live activity by agent and ticket id |
| 71 |
* @param $ticketId |
| 72 |
* @param $agentId |
| 73 |
* @return bool |
| 74 |
*/ |
| 75 |
public static function removeFromActivities($ticketId, $agentId) |
| 76 |
{ |
| 77 |
$meta = Meta::where('object_type', 'ticket_meta') |
| 78 |
->where('key', '_live_activity') |
| 79 |
->where('object_id', $ticketId) |
| 80 |
->first(); |
| 81 |
|
| 82 |
$activities = []; |
| 83 |
if ($meta) { |
| 84 |
$activities = maybe_unserialize($meta->value); |
| 85 |
} |
| 86 |
|
| 87 |
if (!$activities) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
unset($activities[$agentId]); |
| 92 |
foreach ($activities as $index => $activity) { |
| 93 |
if ((time() - $activity) > 60) { |
| 94 |
unset($activities[$index]); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
$meta->value = maybe_serialize($activities); |
| 99 |
$meta->save(); |
| 100 |
|
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* getSuggestedTickets method will return the list of Suggested tickets |
| 106 |
* This method will get the agent id as parameter and fetch ticket information that are waiting to reply or unassigned |
| 107 |
* @param $agentId |
| 108 |
* @param int $limit |
| 109 |
* @return mixed |
| 110 |
*/ |
| 111 |
public static function getSuggestedTickets($agentId, $limit = 5) |
| 112 |
{ |
| 113 |
//Get lis of tickets which are waiting for reply |
| 114 |
$tickets = Ticket::where('agent_id', $agentId) |
| 115 |
->where('status', '!=', 'closed') |
| 116 |
->applyFilters([ |
| 117 |
'waiting_for_reply' => 'yes' |
| 118 |
]) |
| 119 |
->orderBy('last_customer_response', 'ASC') |
| 120 |
->limit($limit) |
| 121 |
->with('customer') |
| 122 |
->get(); |
| 123 |
|
| 124 |
//If no ticket is available for reply and logged-in user has permission to manage unassigned tickets |
| 125 |
if($tickets->isEmpty() && PermissionManager::currentUserCan('fst_manage_unassigned_tickets')) { |
| 126 |
//Get the ticket list which status is not closed and agent id is null or 0 |
| 127 |
$tickets = Ticket::where('status', '!=', 'closed') |
| 128 |
->orderBy('id', 'ASC') |
| 129 |
->where(function ($q) { |
| 130 |
$q->whereNull('agent_id'); |
| 131 |
$q->orWhere('agent_id', '0'); |
| 132 |
}) |
| 133 |
->with('customer') |
| 134 |
->limit($limit) |
| 135 |
->get(); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
return $tickets; |
| 140 |
|
| 141 |
} |
| 142 |
} |
| 143 |
|