| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Models\Attachment; |
| 7 |
use FluentSupport\App\Models\Person; |
| 8 |
use FluentSupport\App\Models\Product; |
| 9 |
use FluentSupport\App\Models\Conversation; |
| 10 |
use FluentSupport\App\Models\Ticket; |
| 11 |
use FluentSupport\App\Modules\Reporting\Reporting; |
| 12 |
use FluentSupport\App\Modules\StatModule; |
| 13 |
use FluentSupport\App\Services\Helper; |
| 14 |
use FluentSupport\App\Services\TicketHelper; |
| 15 |
use FluentSupport\Framework\Request\Request; |
| 16 |
use FluentSupport\App\Modules\PermissionManager; |
| 17 |
use FluentSupport\Framework\Support\Arr; |
| 18 |
|
| 19 |
class AgentController extends Controller |
| 20 |
{ |
| 21 |
public function index(Request $request) |
| 22 |
{ |
| 23 |
$agents = Agent::orderBy('id', 'DESC') |
| 24 |
//->whereNotNull('user_id') |
| 25 |
->searchBy($request->get('search')) |
| 26 |
->paginate(); |
| 27 |
|
| 28 |
foreach ($agents as $agent) { |
| 29 |
$agent->permissions = PermissionManager::getUserPermissions($agent->user_id); |
| 30 |
if ($agent->user_id) { |
| 31 |
$agent->user_profile = admin_url('user-edit.php?user_id=' . $agent->user_id); |
| 32 |
} |
| 33 |
$agent->replies_count = Conversation::where('person_id', $agent->id)->count(); |
| 34 |
$agent->interactions_count = Conversation::where('person_id', $agent->id)->groupBy('ticket_id')->get()->count(); |
| 35 |
$agent->telegram_chat_id = $agent->getMeta('telegram_chat_id'); |
| 36 |
$agent->slack_user_id = $agent->getMeta('slack_user_id'); |
| 37 |
} |
| 38 |
|
| 39 |
return [ |
| 40 |
'agents' => $agents, |
| 41 |
'permissions' => PermissionManager::getReadablePermissionGroups() |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
public function addAgent(Request $request) |
| 46 |
{ |
| 47 |
$data = $request->all(); |
| 48 |
$this->validate($data, [ |
| 49 |
'email' => 'required|email' |
| 50 |
]); |
| 51 |
|
| 52 |
$email = $data['email']; |
| 53 |
|
| 54 |
$user = get_user_by('email', $email); |
| 55 |
|
| 56 |
if (!$user || is_wp_error($user)) { |
| 57 |
return $this->sendError([ |
| 58 |
'message' => __('Sorry, Connected user could not be found with the provided email address', 'fluent-support') |
| 59 |
]); |
| 60 |
} |
| 61 |
|
| 62 |
if (empty($data['first_name'])) { |
| 63 |
$data['first_name'] = $user->first_name; |
| 64 |
} |
| 65 |
|
| 66 |
if (empty($data['last_name'])) { |
| 67 |
$data['last_name'] = $user->last_name; |
| 68 |
} |
| 69 |
|
| 70 |
// check if another agent has same email address |
| 71 |
$exist = Person::where('email', $email)->first(); |
| 72 |
if ($exist) { |
| 73 |
return $this->sendError([ |
| 74 |
'message' => __('Sorry, Another agent/person exist with the same email address. Please use a different email address', 'fluent-support') |
| 75 |
]); |
| 76 |
} |
| 77 |
|
| 78 |
$data['user_id'] = $user->ID; |
| 79 |
$agent = Agent::create($data); |
| 80 |
PermissionManager::attachPermissions($user, Arr::get($data, 'permissions', [])); |
| 81 |
|
| 82 |
if(!empty($data['telegram_chat_id'])) { |
| 83 |
$chatId = sanitize_text_field($data['telegram_chat_id']); |
| 84 |
$agent->updateMeta('telegram_chat_id', $chatId); |
| 85 |
$agent->telegram_chat_id = $chatId; |
| 86 |
} |
| 87 |
|
| 88 |
if(!empty($data['slack_user_id'])) { |
| 89 |
$chatId = sanitize_text_field($data['slack_user_id']); |
| 90 |
$agent->updateMeta('slack_user_id', $chatId); |
| 91 |
$agent->slack_user_id = $chatId; |
| 92 |
} |
| 93 |
|
| 94 |
return [ |
| 95 |
'message' => __('Support Staff has been added', 'fluent-support'), |
| 96 |
'agent' => $agent |
| 97 |
]; |
| 98 |
} |
| 99 |
|
| 100 |
public function updateAgent(Request $request, $agentId) |
| 101 |
{ |
| 102 |
$agent = Agent::findOrFail($agentId); |
| 103 |
$data = $request->all(); |
| 104 |
$this->validate($data, [ |
| 105 |
'email' => 'required|email', |
| 106 |
'first_name' => 'required', |
| 107 |
'last_name' => 'required', |
| 108 |
]); |
| 109 |
|
| 110 |
$user = get_user_by('ID', $agent->user_id); |
| 111 |
|
| 112 |
if (!$user || is_wp_error($user)) { |
| 113 |
return $this->sendError([ |
| 114 |
'message' => __('Sorry, Connected user could not be found with the provided email address', 'fluent-support') |
| 115 |
]); |
| 116 |
} |
| 117 |
|
| 118 |
PermissionManager::attachPermissions($user, Arr::get($data, 'permissions', [])); |
| 119 |
|
| 120 |
$updateData = Arr::only($data, ['first_name', 'last_name', 'title']); |
| 121 |
$updateData['email'] = $user->user_email; |
| 122 |
|
| 123 |
Agent::where('id', $agent->id) |
| 124 |
->update($updateData); |
| 125 |
|
| 126 |
PermissionManager::attachPermissions($user, Arr::get($data, 'permissions', [])); |
| 127 |
|
| 128 |
$agent = Agent::findOrFail($agentId); |
| 129 |
|
| 130 |
if(isset($data['telegram_chat_id'])) { |
| 131 |
$chatId = sanitize_text_field($data['telegram_chat_id']); |
| 132 |
$agent->updateMeta('telegram_chat_id', $chatId); |
| 133 |
$agent->telegram_chat_id = $chatId; |
| 134 |
} |
| 135 |
|
| 136 |
if(isset($data['slack_user_id'])) { |
| 137 |
$chatId = sanitize_text_field($data['slack_user_id']); |
| 138 |
$agent->updateMeta('slack_user_id', $chatId); |
| 139 |
$agent->slack_user_id = $chatId; |
| 140 |
} |
| 141 |
|
| 142 |
return [ |
| 143 |
'message' => __('Support Staff has been updated', 'fluent-support'), |
| 144 |
'agent' => $agent |
| 145 |
]; |
| 146 |
} |
| 147 |
|
| 148 |
public function deleteAgent(Request $request, $agentId) |
| 149 |
{ |
| 150 |
$fallBackAgentId = $request->get('fallback_agent_id'); |
| 151 |
|
| 152 |
if ($fallBackAgentId == $agentId) { |
| 153 |
return $this->sendError([ |
| 154 |
'message' => __('Old Agent and New agent is same person', 'fluent-support') |
| 155 |
]); |
| 156 |
} |
| 157 |
|
| 158 |
$agent = Agent::findOrFail($agentId); |
| 159 |
|
| 160 |
PermissionManager::attachPermissions($agent->user_id, []); |
| 161 |
|
| 162 |
$newAgent = Agent::findOrFail($fallBackAgentId); |
| 163 |
|
| 164 |
Attachment::where('person_id', $agentId)->update([ |
| 165 |
'person_id' => $newAgent->id |
| 166 |
]); |
| 167 |
|
| 168 |
Conversation::where('person_id', $agentId)->update([ |
| 169 |
'person_id' => $newAgent->id |
| 170 |
]); |
| 171 |
|
| 172 |
Product::where('created_by', $agentId)->update([ |
| 173 |
'created_by' => $newAgent->id |
| 174 |
]); |
| 175 |
|
| 176 |
Ticket::where('agent_id', $agentId)->update([ |
| 177 |
'agent_id' => $newAgent->id |
| 178 |
]); |
| 179 |
|
| 180 |
$agent->deleteAllMeta(); |
| 181 |
|
| 182 |
Agent::where('id', $agentId)->delete(); |
| 183 |
|
| 184 |
return [ |
| 185 |
'message' => __('Selected support staff has been deleted', 'fluent-support') |
| 186 |
]; |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
public function myStats(Request $request) |
| 191 |
{ |
| 192 |
$agent = Helper::getAgentByUserId(); |
| 193 |
$response = $this->getAgentStat($request, $agent->id); |
| 194 |
|
| 195 |
if(defined('FLUENTSUPPORTPRO')) { |
| 196 |
$response['dashboard_notice'] = apply_filters('fluent_support/dashboard_notice', '', $agent); |
| 197 |
} |
| 198 |
|
| 199 |
return $response; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
public function getAgentStat(Request $request, $agentId) |
| 204 |
{ |
| 205 |
$agent = Agent::findOrFail($agentId); |
| 206 |
$stats = StatModule::getAgentStat($agentId); |
| 207 |
|
| 208 |
if (PermissionManager::currentUserCan('fst_manage_unassigned_tickets')) { |
| 209 |
$stats['unassigned_tickets'] = [ |
| 210 |
'title' => __('Unassigned Tickets', 'fluent-support'), |
| 211 |
'count' => Ticket::whereNull('agent_id')->where('status', '!=', 'closed')->count() |
| 212 |
]; |
| 213 |
} |
| 214 |
|
| 215 |
$data = [ |
| 216 |
'stats' => $stats |
| 217 |
]; |
| 218 |
|
| 219 |
$with = $request->get('with', []); |
| 220 |
|
| 221 |
if (in_array('suggested_tickets', $with)) { |
| 222 |
$data['suggested_tickets'] = TicketHelper::getSuggestedTickets($agent->id); |
| 223 |
} |
| 224 |
|
| 225 |
if(in_array('overall_stats', $with)) { |
| 226 |
$data['overall_stats'] = (new Reporting())->getActiveStats(); |
| 227 |
} |
| 228 |
|
| 229 |
if(in_array('individual_stat', $with)) { |
| 230 |
$data['individual_stat'] = (new Reporting())->getActiveStatByAgent($agent->id); |
| 231 |
} |
| 232 |
if(in_array('my_overall_stats', $with)){ |
| 233 |
$data['my_overall_stats'] = StatModule::getAgentOverallStats($agent->id); |
| 234 |
} |
| 235 |
|
| 236 |
return $data; |
| 237 |
} |
| 238 |
} |
| 239 |
|