PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.1.1
Fluent Support – Helpdesk & Customer Support Ticket System v2.1.1
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Models / Traits / AgentTrait.php

AgentTrait.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.1.1, at app/Models/Traits/AgentTrait.php

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