| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models\Traits; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Attachment; |
| 6 |
use FluentSupport\App\Models\Conversation; |
| 7 |
use FluentSupport\App\Models\Product; |
| 8 |
use FluentSupport\App\Models\Ticket; |
| 9 |
use FluentSupport\App\Modules\PermissionManager; |
| 10 |
use FluentSupport\App\Models\Person; |
| 11 |
use FluentSupport\App\Modules\Reporting\Reporting; |
| 12 |
use FluentSupport\App\Modules\StatModule; |
| 13 |
use FluentSupport\App\Services\TicketHelper; |
| 14 |
use FluentSupport\Framework\Support\Arr; |
| 15 |
use Exception; |
| 16 |
|
| 17 |
trait AgentTrait |
| 18 |
{ |
| 19 |
public function getAgents($search) |
| 20 |
{ |
| 21 |
$agents = static::latest() |
| 22 |
->searchBy($search) |
| 23 |
->paginate(); |
| 24 |
|
| 25 |
foreach ($agents as $agent) { |
| 26 |
$agent->permissions = PermissionManager::getUserPermissions($agent->user_id); |
| 27 |
|
| 28 |
if ($agent->user_id) { |
| 29 |
$agent->user_profile = admin_url('user-edit.php?user_id=' . $agent->user_id); |
| 30 |
} |
| 31 |
|
| 32 |
$agent->replies_count = Conversation::where('person_id', $agent->id)->count(); |
| 33 |
$agent->interactions_count = Conversation::where('person_id', $agent->id)->groupBy('ticket_id')->get()->count(); |
| 34 |
|
| 35 |
$agent->telegram_chat_id = $agent->getMeta('telegram_chat_id'); |
| 36 |
$agent->slack_user_id = $agent->getMeta('slack_user_id'); |
| 37 |
$agent->whatsapp_number = $agent->getMeta('whatsapp_number'); |
| 38 |
$agent->restrictions = $agent->getMeta('agent_restrictions', [ |
| 39 |
'restrictedBusinessBoxes' => [], |
| 40 |
'businessBoxRestrictions' => false |
| 41 |
]); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
return $agents; |
| 46 |
} |
| 47 |
|
| 48 |
public function createAgent(array $data) |
| 49 |
{ |
| 50 |
if (!$user = get_user_by('email', $data['email'])) { |
| 51 |
throw new \Exception('Sorry, Connected user could not be found with the provided email address'); |
| 52 |
} |
| 53 |
|
| 54 |
$data = $this->setAgentInfo($data, $user); |
| 55 |
|
| 56 |
// check if another agent has same email address |
| 57 |
$person = Person::where('email', $data['email'])->first(); |
| 58 |
|
| 59 |
if ($person) { |
| 60 |
throw new \Exception('Sorry, Another agent/person exist with the same email address. Please use a different email address'); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
return $this->setAgentMeta( |
| 65 |
$user, |
| 66 |
$data, |
| 67 |
static::create($data) |
| 68 |
); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Method to update agent info |
| 74 |
* @param array $data |
| 75 |
* @param $agent |
| 76 |
* @return object |
| 77 |
* @throws Exception |
| 78 |
*/ |
| 79 |
public function updateAgent(array $data, object $agent) |
| 80 |
{ |
| 81 |
if (!$user = get_user_by('ID', $agent->user_id)) { |
| 82 |
throw new \Exception('Sorry, Connected user could not be found with the provided email address'); |
| 83 |
} |
| 84 |
|
| 85 |
PermissionManager::attachPermissions($user, Arr::get($data, 'permissions', [])); |
| 86 |
|
| 87 |
$updateData = Arr::only($data, ['first_name', 'last_name', 'title']); |
| 88 |
$updateData['user_id'] = $user->ID; |
| 89 |
$updateData['email'] = $user->user_email; |
| 90 |
|
| 91 |
static::where('id', $agent->id) |
| 92 |
->update($updateData); |
| 93 |
|
| 94 |
$agent = $this->setAgentMeta($user, $data, $agent); |
| 95 |
|
| 96 |
return $agent; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* This method will delete an agent by agent id |
| 102 |
* @param int $fallBackAgentId |
| 103 |
* @param int $agentId |
| 104 |
* @return void |
| 105 |
* @throws Exception |
| 106 |
*/ |
| 107 |
public function deleteAgent($fallBackAgentId, $agentId) |
| 108 |
{ |
| 109 |
if ($fallBackAgentId == $agentId) { |
| 110 |
throw new \Exception('Old Agent and New agent is same person'); |
| 111 |
} |
| 112 |
|
| 113 |
$agent = static::findOrFail($agentId); |
| 114 |
|
| 115 |
PermissionManager::attachPermissions($agent->user_id, []); |
| 116 |
|
| 117 |
try { |
| 118 |
$newAgent = static::findOrFail($fallBackAgentId); |
| 119 |
} catch (\Exception $e) { |
| 120 |
throw new \Exception('Fallback agent could not be found'); |
| 121 |
} |
| 122 |
|
| 123 |
$this->assignDataToFallbackAgent($agent->id, $newAgent); |
| 124 |
|
| 125 |
$agent->deleteAllMeta(); |
| 126 |
|
| 127 |
static::where('id', $agentId)->delete(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* This method will assign data to fallback agent |
| 132 |
* @param int $agentId |
| 133 |
* @param object $newAgent |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
private function assignDataToFallbackAgent($agentId, $newAgent) |
| 137 |
{ |
| 138 |
Attachment::where('person_id', $agentId)->update([ |
| 139 |
'person_id' => $newAgent->id |
| 140 |
]); |
| 141 |
|
| 142 |
Conversation::where('person_id', $agentId)->update([ |
| 143 |
'person_id' => $newAgent->id |
| 144 |
]); |
| 145 |
|
| 146 |
Product::where('created_by', $agentId)->update([ |
| 147 |
'created_by' => $newAgent->id |
| 148 |
]); |
| 149 |
|
| 150 |
Ticket::where('agent_id', $agentId)->update([ |
| 151 |
'agent_id' => $newAgent->id |
| 152 |
]); |
| 153 |
} |
| 154 |
|
| 155 |
public function getAgentStat($stats, $with, $agentId) |
| 156 |
{ |
| 157 |
if (PermissionManager::currentUserCan('fst_manage_unassigned_tickets')) { |
| 158 |
$stats['unassigned_tickets'] = [ |
| 159 |
'title' => __('Unassigned Tickets', 'fluent-support'), |
| 160 |
'count' => Ticket::whereNull('agent_id')->where('status', '!=', 'closed')->count() |
| 161 |
]; |
| 162 |
} |
| 163 |
|
| 164 |
$data = [ |
| 165 |
'stats' => $stats |
| 166 |
]; |
| 167 |
|
| 168 |
return $this->agentDashboardWidgets($data, $with, $agentId); |
| 169 |
} |
| 170 |
|
| 171 |
private function agentDashboardWidgets($data, $with, $agentId) |
| 172 |
{ |
| 173 |
//If the request come with suggested_tickets |
| 174 |
if (in_array('suggested_tickets', $with)) { |
| 175 |
//Get suggested tickets from ticketHelper |
| 176 |
$data['suggested_tickets'] = TicketHelper::getSuggestedTickets($agentId); |
| 177 |
} |
| 178 |
|
| 179 |
//If the request come with mentioned_tickets |
| 180 |
if (defined('FLUENTSUPPORTPRO') && in_array('ticket_to_watch', $with)) { |
| 181 |
//Get the overall statistics by the agent |
| 182 |
$data['ticket_to_watch'] = TicketHelper::getTicketsToWatch(); |
| 183 |
} |
| 184 |
|
| 185 |
//If the request come with overall_stats |
| 186 |
if (in_array('overall_stats', $with)) { |
| 187 |
//Get overall status |
| 188 |
$data['overall_stats'] = (new Reporting())->getActiveStats(); |
| 189 |
} |
| 190 |
|
| 191 |
//If the request come with individual_stat |
| 192 |
if (in_array('individual_stat', $with)) { |
| 193 |
//get overall statistics by agent id |
| 194 |
$data['individual_stat'] = (new Reporting())->getActiveStatByAgent($agentId); |
| 195 |
} |
| 196 |
//If the request come with my_overall_stats |
| 197 |
if (in_array('my_overall_stats', $with)) { |
| 198 |
//Get the overall statistics by the agent |
| 199 |
$data['my_overall_stats'] = StatModule::getAgentOverallStats($agentId); |
| 200 |
} |
| 201 |
|
| 202 |
if (in_array('tickets_by_products', $with)) { |
| 203 |
//Get tickets by product which are waiting for agent reply |
| 204 |
$data['tickets_by_product'] = StatModule::getActiveTicketsByProductStats(); |
| 205 |
} |
| 206 |
|
| 207 |
return $data; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Method to set agent info |
| 212 |
* @param array $data |
| 213 |
* @param $user |
| 214 |
* @return array |
| 215 |
*/ |
| 216 |
private function setAgentInfo(array $data, $user) |
| 217 |
{ |
| 218 |
$data['user_id'] = $user->ID; |
| 219 |
|
| 220 |
if (empty($data['first_name'])) { |
| 221 |
$data['first_name'] = $user->first_name; |
| 222 |
} |
| 223 |
|
| 224 |
if (empty($data['last_name'])) { |
| 225 |
$data['last_name'] = $user->last_name; |
| 226 |
} |
| 227 |
|
| 228 |
return $data; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Method to set agent meta data |
| 233 |
* @param $user |
| 234 |
* @param array $data |
| 235 |
* @param object $agent |
| 236 |
* @return object |
| 237 |
*/ |
| 238 |
private function setAgentMeta($user, $data, $agent) |
| 239 |
{ |
| 240 |
PermissionManager::attachPermissions($user, Arr::get($data, 'permissions', [])); |
| 241 |
|
| 242 |
if (isset($data['telegram_chat_id'])) { |
| 243 |
$chatId = sanitize_text_field($data['telegram_chat_id']); |
| 244 |
$agent->updateMeta('telegram_chat_id', $chatId); |
| 245 |
$agent->telegram_chat_id = $chatId; |
| 246 |
} |
| 247 |
|
| 248 |
if (isset($data['slack_user_id'])) { |
| 249 |
$chatId = sanitize_text_field($data['slack_user_id']); |
| 250 |
$agent->updateMeta('slack_user_id', $chatId); |
| 251 |
$agent->slack_user_id = $chatId; |
| 252 |
} |
| 253 |
|
| 254 |
if (isset($data['whatsapp_number'])) { |
| 255 |
$whatsappNumber = sanitize_text_field($data['whatsapp_number']); |
| 256 |
$agent->updateMeta('whatsapp_number', $whatsappNumber); |
| 257 |
$agent->whatsapp_number = $whatsappNumber; |
| 258 |
} |
| 259 |
|
| 260 |
if (isset($data['restrictions'])) { |
| 261 |
$restrictedBusinessBoxes = $data['restrictions']['restrictedBusinessBoxes'] ?? []; |
| 262 |
$businessBoxRestrictions = filter_var($data['restrictions']['businessBoxRestrictions'] ?? false, FILTER_VALIDATE_BOOLEAN); |
| 263 |
|
| 264 |
$restrictions = [ |
| 265 |
'restrictedBusinessBoxes' => is_array($restrictedBusinessBoxes) ? array_map('intval', $restrictedBusinessBoxes) : [], |
| 266 |
'businessBoxRestrictions' => $businessBoxRestrictions |
| 267 |
]; |
| 268 |
|
| 269 |
$agent->updateMeta('agent_restrictions', $restrictions); |
| 270 |
$agent->restrictions = $restrictions; |
| 271 |
} |
| 272 |
|
| 273 |
return $agent; |
| 274 |
} |
| 275 |
|
| 276 |
public function agentInsights($search) |
| 277 |
{ |
| 278 |
$agents = static::latest() |
| 279 |
->select('id', 'first_name', 'last_name', 'email', 'user_id') |
| 280 |
->searchBy($search) |
| 281 |
->get(); |
| 282 |
|
| 283 |
foreach ($agents as $agent) { |
| 284 |
$agent->permissions = PermissionManager::getUserPermissions($agent->user_id); |
| 285 |
|
| 286 |
if ($agent->user_id) { |
| 287 |
$agent->user_profile = admin_url('user-edit.php?user_id=' . $agent->user_id); |
| 288 |
} |
| 289 |
|
| 290 |
$agent->restrictions = $agent->getMeta('agent_restrictions', [ |
| 291 |
'restrictedBusinessBoxes' => [], |
| 292 |
'businessBoxRestrictions' => false |
| 293 |
]); |
| 294 |
} |
| 295 |
|
| 296 |
return $agents; |
| 297 |
} |
| 298 |
} |
| 299 |
|