| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Modules\StatModule; |
| 7 |
use FluentSupport\App\Services\AvatarUploder; |
| 8 |
use FluentSupport\App\Services\Helper; |
| 9 |
use FluentSupport\Framework\Http\Request\Request; |
| 10 |
use FluentSupport\App\Modules\PermissionManager; |
| 11 |
use FluentSupport\App\Http\Requests\AgentCreateRequest; |
| 12 |
|
| 13 |
/** |
| 14 |
* AgentController class for REST API |
| 15 |
* This class is responsible for getting data for all request related to agent |
| 16 |
* |
| 17 |
* @package FluentSupport\App\Http\Controllers |
| 18 |
* |
| 19 |
* @version 1.0.0 |
| 20 |
*/ |
| 21 |
class AgentController extends Controller |
| 22 |
{ |
| 23 |
public function index(Request $request, Agent $agent) |
| 24 |
{ |
| 25 |
return [ |
| 26 |
'agents' => $agent->getAgents($request->getSafe('search', 'sanitize_text_field')), |
| 27 |
'permissions' => PermissionManager::getReadablePermissionGroups(), |
| 28 |
'businessBoxes' => PermissionManager::getMailboxesForRestriction(), |
| 29 |
]; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* addAgent method will add new agent in person table |
| 34 |
* @param AgentCreateRequest $request |
| 35 |
* @return \WP_REST_Response | array |
| 36 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 37 |
*/ |
| 38 |
public function addAgent(AgentCreateRequest $request, Agent $agent) |
| 39 |
{ |
| 40 |
$data = $this->sanitizeAgentPayload($request, true); |
| 41 |
|
| 42 |
try { |
| 43 |
return [ |
| 44 |
'message' => __('Support Staff has been added', 'fluent-support'), |
| 45 |
'agent' => $agent->createAgent($data) |
| 46 |
]; |
| 47 |
} catch (\Exception $e) { |
| 48 |
return $this->sendError([ |
| 49 |
'message' => Helper::getSafeErrorMessage($e) |
| 50 |
]); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* updateAgent method will update the information of an exiting agent |
| 56 |
* @param AgentCreateRequest $request |
| 57 |
* @param Agent $agent |
| 58 |
* @param $agent_id |
| 59 |
* @return \WP_REST_Response | array |
| 60 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 61 |
*/ |
| 62 |
public function updateAgent(AgentCreateRequest $request, Agent $agent, $agent_id) |
| 63 |
{ |
| 64 |
$agent = $agent::findOrFail($agent_id); |
| 65 |
$data = $this->sanitizeAgentPayload($request); |
| 66 |
|
| 67 |
if (!$agent->user_id && ($user = get_user_by('email', $agent->email))) { |
| 68 |
$agent->user_id = $user->ID; |
| 69 |
} |
| 70 |
|
| 71 |
try { |
| 72 |
return [ |
| 73 |
'message' => __('Support Staff has been updated', 'fluent-support'), |
| 74 |
'agent' => $agent->updateAgent($data, $agent) |
| 75 |
]; |
| 76 |
} catch (\Exception $e) { |
| 77 |
return $this->sendError([ |
| 78 |
'message' => Helper::getSafeErrorMessage($e) |
| 79 |
]); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* deleteAgent will delete an exiting agent and add an alternative agent as replacement |
| 85 |
* @param Request $request |
| 86 |
* @param Agent $agent |
| 87 |
* @param $agent_id |
| 88 |
* @return \WP_REST_Response | array |
| 89 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 90 |
*/ |
| 91 |
public function deleteAgent(Request $request, Agent $agent, $agent_id) |
| 92 |
{ |
| 93 |
try { |
| 94 |
$agent->deleteAgent($request->getSafe('fallback_agent_id', 'intval'), $agent_id); |
| 95 |
|
| 96 |
return [ |
| 97 |
'message' => __('Support Staff has been deleted', 'fluent-support') |
| 98 |
]; |
| 99 |
} catch (\Exception $e) { |
| 100 |
return $this->sendError([ |
| 101 |
'message' => Helper::getSafeErrorMessage($e) |
| 102 |
]); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @param Request $request |
| 108 |
* @return \WP_REST_Response | array |
| 109 |
*/ |
| 110 |
public function myStats(Request $request) |
| 111 |
{ |
| 112 |
// Get logged in agent information. |
| 113 |
$agent = Helper::getAgentByUserId(); |
| 114 |
|
| 115 |
try { |
| 116 |
// Get ticket statistics. |
| 117 |
$stats = StatModule::getAgentStat($agent->id); |
| 118 |
$with = $request->get('with', []); |
| 119 |
$with = is_array($with) ? map_deep($with, 'sanitize_text_field') : []; |
| 120 |
|
| 121 |
$response = (new Agent())->getAgentStat($stats, $with, $agent->id); |
| 122 |
|
| 123 |
if (defined('FLUENTSUPPORTPRO')) { |
| 124 |
$response['dashboard_notice'] = apply_filters('fluent_support/dashboard_notice', '', $agent); |
| 125 |
} |
| 126 |
|
| 127 |
return $response; |
| 128 |
} catch (\Exception $e) { |
| 129 |
return $this->sendError([ |
| 130 |
'message' => Helper::getSafeErrorMessage($e) |
| 131 |
]); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public function agentPerformance() |
| 136 |
{ |
| 137 |
try { |
| 138 |
if (PermissionManager::currentUserCan('fst_agent_today_performance')) { |
| 139 |
$agentTodayStats = StatModule::getAgentTodayStats(); |
| 140 |
|
| 141 |
return ['agent_today_stats' => $agentTodayStats]; |
| 142 |
} |
| 143 |
} catch (\Exception $e) { |
| 144 |
return $this->sendError([ |
| 145 |
'message' => Helper::getSafeErrorMessage($e) |
| 146 |
]); |
| 147 |
} |
| 148 |
|
| 149 |
return []; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* addOrUpdateProfileImage method will upload profile picture for a given agent id |
| 154 |
* For a successful upload it's required to send file object, agent id and the user type(agent) |
| 155 |
* @param Request $request |
| 156 |
* @param AvatarUploder $avatarUploder |
| 157 |
* @return \WP_REST_Response | array |
| 158 |
*/ |
| 159 |
public function addOrUpdateProfileImage(Request $request, AvatarUploder $avatarUploder) |
| 160 |
{ |
| 161 |
try { |
| 162 |
return $avatarUploder->addOrUpdateProfileImage($request->files(), $request->getSafe('agent_id', 'intval'), 'agent'); |
| 163 |
} catch (\Exception $e) { |
| 164 |
return $this->sendError([ |
| 165 |
'message' => Helper::getSafeErrorMessage($e) |
| 166 |
]); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* resetAvatar method will restore a Support Staff avatar |
| 172 |
* For a successful upload it's required to send file object, Support Staff id and the user type(Support Staff) |
| 173 |
* @param Agent $agent |
| 174 |
* @param $agent_id |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
public function resetAvatar(Agent $agent) |
| 178 |
{ |
| 179 |
try { |
| 180 |
$agent->restoreAvatar(); |
| 181 |
|
| 182 |
return [ |
| 183 |
'message' => __('Support Staff avatar reset to gravatar default', 'fluent-support') |
| 184 |
]; |
| 185 |
} catch (\Exception $e) { |
| 186 |
return $this->sendError([ |
| 187 |
'message' => Helper::getSafeErrorMessage($e) |
| 188 |
]); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
public function getAgentInsights(Request $request, Agent $agent) |
| 193 |
{ |
| 194 |
return [ |
| 195 |
'agents' => $agent->agentInsights($request->getSafe('search', 'sanitize_text_field')), |
| 196 |
]; |
| 197 |
} |
| 198 |
|
| 199 |
public function ping(Request $request) |
| 200 |
{ |
| 201 |
return [ |
| 202 |
'ping' => 'pong', |
| 203 |
]; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Sanitize payload data for create/update operations. |
| 208 |
* |
| 209 |
* @param AgentCreateRequest $request |
| 210 |
* @param bool $includeEmail |
| 211 |
* @return array |
| 212 |
*/ |
| 213 |
private function sanitizeAgentPayload(AgentCreateRequest $request, $includeEmail = false) |
| 214 |
{ |
| 215 |
$data = [ |
| 216 |
'first_name' => $request->getSafe('first_name', 'sanitize_text_field'), |
| 217 |
'last_name' => $request->getSafe('last_name', 'sanitize_text_field'), |
| 218 |
'title' => $request->getSafe('title', 'sanitize_text_field'), |
| 219 |
'permissions' => is_array($request->get('permissions')) ? array_map('sanitize_key', $request->get('permissions')) : [], |
| 220 |
'telegram_chat_id' => $request->getSafe('telegram_chat_id', 'sanitize_text_field'), |
| 221 |
'slack_user_id' => $request->getSafe('slack_user_id', 'sanitize_text_field'), |
| 222 |
'whatsapp_number' => $request->getSafe('whatsapp_number', 'sanitize_text_field'), |
| 223 |
'restrictions' => $this->sanitizeRestrictions($request->get('restrictions')), |
| 224 |
]; |
| 225 |
|
| 226 |
if ($request->has('agent_signature')) { |
| 227 |
$data['agent_signature'] = wp_kses_post(wp_unslash($request->get('agent_signature', ''))); |
| 228 |
} |
| 229 |
|
| 230 |
if ($request->has('agent_signature_enabled')) { |
| 231 |
$data['agent_signature_enabled'] = $request->get('agent_signature_enabled') === 'yes' ? 'yes' : 'no'; |
| 232 |
} |
| 233 |
|
| 234 |
if ($includeEmail) { |
| 235 |
$data['email'] = $request->getSafe('email', 'sanitize_email'); |
| 236 |
} |
| 237 |
|
| 238 |
return $data; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Sanitize restrictions data for agent |
| 243 |
* |
| 244 |
* @param mixed $restrictions |
| 245 |
* @return array |
| 246 |
*/ |
| 247 |
private function sanitizeRestrictions($restrictions) |
| 248 |
{ |
| 249 |
if (!is_array($restrictions)) { |
| 250 |
return [ |
| 251 |
'restrictedBusinessBoxes' => [], |
| 252 |
'businessBoxRestrictions' => false, |
| 253 |
]; |
| 254 |
} |
| 255 |
|
| 256 |
$restrictedBusinessBoxes = isset($restrictions['restrictedBusinessBoxes']) && is_array($restrictions['restrictedBusinessBoxes']) |
| 257 |
? array_map('absint', $restrictions['restrictedBusinessBoxes']) |
| 258 |
: []; |
| 259 |
|
| 260 |
$businessBoxRestrictions = isset($restrictions['businessBoxRestrictions']) |
| 261 |
? rest_sanitize_boolean($restrictions['businessBoxRestrictions']) |
| 262 |
: false; |
| 263 |
|
| 264 |
return [ |
| 265 |
'restrictedBusinessBoxes' => $restrictedBusinessBoxes, |
| 266 |
'businessBoxRestrictions' => $businessBoxRestrictions, |
| 267 |
]; |
| 268 |
} |
| 269 |
} |
| 270 |
|