| 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\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::getBusinessBoxesForRestriction(), |
| 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 = [ |
| 41 |
'email' => $request->getSafe('email', 'sanitize_email'), |
| 42 |
'first_name' => $request->getSafe('first_name', 'sanitize_text_field'), |
| 43 |
'last_name' => $request->getSafe('last_name', 'sanitize_text_field'), |
| 44 |
'title' => $request->getSafe('title', 'sanitize_text_field'), |
| 45 |
'permissions' => $request->getSafe('permissions', null, []), |
| 46 |
'restrictions' => $request->getSafe('restrictions', null, []), |
| 47 |
]; |
| 48 |
|
| 49 |
try { |
| 50 |
return [ |
| 51 |
'message' => __('Support Staff has been added', 'fluent-support'), |
| 52 |
'agent' => $agent->createAgent($data) |
| 53 |
]; |
| 54 |
|
| 55 |
} catch (\Exception $e) { |
| 56 |
return $this->sendError([ |
| 57 |
'message' => $e->getMessage() |
| 58 |
]); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* updateAgent method will update the information of an exiting agent |
| 64 |
* @param AgentCreateRequest $request |
| 65 |
* @param Agent $agent |
| 66 |
* @param $agent_id |
| 67 |
* @return \WP_REST_Response | array |
| 68 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 69 |
*/ |
| 70 |
public function updateAgent(AgentCreateRequest $request, Agent $agent, $agent_id) |
| 71 |
{ |
| 72 |
$agent = $agent::findOrFail($agent_id); |
| 73 |
|
| 74 |
$data = [ |
| 75 |
'first_name' => $request->getSafe('first_name', 'sanitize_text_field'), |
| 76 |
'last_name' => $request->getSafe('last_name', 'sanitize_text_field'), |
| 77 |
'title' => $request->getSafe('title', 'sanitize_text_field'), |
| 78 |
'permissions' => $request->getSafe('permissions', null, []), |
| 79 |
'telegram_chat_id' => $request->getSafe('telegram_chat_id', 'sanitize_text_field'), |
| 80 |
'slack_user_id' => $request->getSafe('slack_user_id', 'sanitize_text_field'), |
| 81 |
'whatsapp_number' => $request->getSafe('whatsapp_number', 'sanitize_text_field'), |
| 82 |
'restrictions' => $request->getSafe('restrictions', null, []), |
| 83 |
]; |
| 84 |
|
| 85 |
if (!$agent->user_id && ($user = get_user_by('email', $agent->email))) { |
| 86 |
$agent->user_id = $user->ID; |
| 87 |
} |
| 88 |
|
| 89 |
if ($agent) { |
| 90 |
try { |
| 91 |
return [ |
| 92 |
'message' => __('Support Staff has been updated', 'fluent-support'), |
| 93 |
'agent' => $agent->updateAgent($data, $agent) |
| 94 |
]; |
| 95 |
} catch (\Exception $e) { |
| 96 |
return $this->sendError([ |
| 97 |
'message' => $e->getMessage() |
| 98 |
]); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* deleteAgent will delete an exiting agent and add an alternative agent as replacement |
| 106 |
* @param Request $request |
| 107 |
* @param Agent $agent |
| 108 |
* @param $agent_id |
| 109 |
* @return \WP_REST_Response | array |
| 110 |
* @throws \FluentSupport\Framework\Validator\ValidationException |
| 111 |
*/ |
| 112 |
public function deleteAgent(Request $request, Agent $agent, $agent_id) |
| 113 |
{ |
| 114 |
try { |
| 115 |
$agent->deleteAgent($request->getSafe('fallback_agent_id', 'intval'), $agent_id); |
| 116 |
|
| 117 |
return [ |
| 118 |
'message' => __('Support Staff has been deleted', 'fluent-support') |
| 119 |
]; |
| 120 |
|
| 121 |
} catch (\Exception $e) { |
| 122 |
return $this->sendError([ |
| 123 |
'message' => $e->getMessage() |
| 124 |
]); |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @param Request $request |
| 131 |
* @return \WP_REST_Response | array |
| 132 |
*/ |
| 133 |
public function myStats(Request $request) |
| 134 |
{ |
| 135 |
|
| 136 |
$agent = Helper::getAgentByUserId();//Get logged in agent information |
| 137 |
|
| 138 |
try { |
| 139 |
$stats = StatModule::getAgentStat($agent->id); //Get ticket statistics |
| 140 |
|
| 141 |
$with = $request->getSafe('with'); |
| 142 |
|
| 143 |
$response = (new Agent())->getAgentStat($stats, $with, $agent->id); |
| 144 |
|
| 145 |
if (defined('FLUENTSUPPORTPRO')) { |
| 146 |
$response['dashboard_notice'] = apply_filters('fluent_support/dashboard_notice', '', $agent); |
| 147 |
} |
| 148 |
return $response; |
| 149 |
} catch (\Exception $e) { |
| 150 |
return $this->sendError([ |
| 151 |
'message' => $e->getMessage() |
| 152 |
]); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
public function agentPerformance() |
| 157 |
{ |
| 158 |
try { |
| 159 |
if (PermissionManager::currentUserCan('fst_agent_today_performance')) { |
| 160 |
$agentTodayStats = StatModule::getAgentTodayStats(); |
| 161 |
return ['agent_today_stats' => $agentTodayStats]; |
| 162 |
} |
| 163 |
} catch (\Exception $e) { |
| 164 |
return $this->sendError([ |
| 165 |
'message' => $e->getMessage() |
| 166 |
]); |
| 167 |
} |
| 168 |
|
| 169 |
return []; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* addOrUpdateProfileImage method will upload profile picture for a given agent id |
| 174 |
* For a successful upload it's required to send file object, agent id and the user type(agent) |
| 175 |
* @param Request $request |
| 176 |
* @param AvatarUploder $avatarUploder |
| 177 |
* @return \WP_REST_Response | array |
| 178 |
*/ |
| 179 |
public function addOrUpdateProfileImage(Request $request, AvatarUploder $avatarUploder) |
| 180 |
{ |
| 181 |
try { |
| 182 |
return $avatarUploder->addOrUpdateProfileImage( $request->files(), $request->getSafe('agent_id', 'intval'), 'agent'); |
| 183 |
} catch (\Exception $e) { |
| 184 |
return $this->sendError([ |
| 185 |
'message' => $e->getMessage() |
| 186 |
]); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* resetAvatar method will restore a Support Staff avatar |
| 192 |
* For a successful upload it's required to send file object, Support Staff id and the user type(Support Staff) |
| 193 |
* @param Agent $agent |
| 194 |
* @param $agent_id |
| 195 |
* @return array |
| 196 |
*/ |
| 197 |
public function resetAvatar(Agent $agent, $agent_id){ |
| 198 |
try { |
| 199 |
$agent->restoreAvatar($agent, $agent_id); |
| 200 |
|
| 201 |
return [ |
| 202 |
'message' => __('Support Staff avatar reset to gravatar default', 'fluent-support') |
| 203 |
]; |
| 204 |
} catch (\Exception $e) { |
| 205 |
return [ |
| 206 |
'message' => $e->getMessage() |
| 207 |
]; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
public function getAgentInsights(Request $request, Agent $agent) |
| 212 |
{ |
| 213 |
return [ |
| 214 |
'agents' => $agent->agentInsights($request->getSafe('search','sanitize_text_field')), |
| 215 |
]; |
| 216 |
} |
| 217 |
|
| 218 |
public function ping(Request $request) |
| 219 |
{ |
| 220 |
return [ |
| 221 |
'ping' => 'pong' |
| 222 |
]; |
| 223 |
} |
| 224 |
} |
| 225 |
|