PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
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 trunk, at app/Models/Traits/AgentTrait.php

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