| @@ -6,21 +6,18 @@ | ||
| 6 | 6 | use FluentSupport\App\Modules\StatModule; |
| 7 | 7 | use FluentSupport\App\Services\Helper; |
| 8 | 8 | use FluentSupport\Framework\Http\Request\Request; |
| 9 | 9 | use FluentSupport\App\Models\Ticket; |
| 10 | +use FluentSupport\App\Services\Tickets\AgentTicketAccess; | |
| 10 | 11 | use FluentSupport\App\Models\Conversation; |
| 11 | 12 | use FluentSupport\App\Models\TagPivot; |
| 12 | 13 | |
| 13 | 14 | /** |
| 14 | - * ReportingController class for REST API | |
| 15 | - * This class is responsible for getting data for all request related to report | |
| 16 | - * @package FluentSupport\App\Http\Controllers | |
| 17 | - * | |
| 18 | - * @version 1.0.0 | |
| 15 | + * Report endpoints. | |
| 19 | 16 | */ |
| 20 | 17 | class ReportingController extends Controller |
| 21 | 18 | { |
| 22 | - private static function getSanitizedDateRange(Request $request) | |
| 19 | + public static function getSanitizedDateRange(Request $request) | |
| 23 | 20 | { |
| 24 | 21 | $dateRange = $request->get('date_range', []); |
| 25 | 22 | |
| 26 | 23 | if (is_array($dateRange) && count($dateRange) >= 2) { |
| @@ -40,9 +37,9 @@ | ||
| 40 | 37 | |
| 41 | 38 | return ['', '']; |
| 42 | 39 | } |
| 43 | 40 | |
| 44 | - private static function getAgentIdsForGroup(Request $request) | |
| 41 | + public static function getAgentIdsForGroup(Request $request) | |
| 45 | 42 | { |
| 46 | 43 | $groupId = $request->getSafe('agent_group_id', 'intval'); |
| 47 | 44 | if (!$groupId) { |
| 48 | 45 | return null; |
| @@ -54,146 +51,13 @@ | ||
| 54 | 51 | ->toArray(); |
| 55 | 52 | } |
| 56 | 53 | |
| 57 | 54 | /** |
| 58 | - * getOverallReports method will return the overall statistics of all ticket by ticket statuses | |
| 59 | - * The response will have an array with ticket number by ticket status | |
| 55 | + * Stats for the logged-in agent. | |
| 56 | + * | |
| 60 | 57 | * @param Request $request |
| 61 | 58 | * @return array |
| 62 | 59 | */ |
| 63 | - public function getOverallReports(Request $request) | |
| 64 | - { | |
| 65 | - return [ | |
| 66 | - 'overall_reports' => StatModule::getOverAllStats() | |
| 67 | - ]; | |
| 68 | - } | |
| 69 | - | |
| 70 | - public function getActiveTicketsByProduct() | |
| 71 | - { | |
| 72 | - return [ | |
| 73 | - 'stats' => StatModule::getActiveTicketsByProductStats() | |
| 74 | - ]; | |
| 75 | - } | |
| 76 | - | |
| 77 | - /** | |
| 78 | - * getTicketsChart method will generate statistics for all tickets within a date range and return ticket number by date | |
| 79 | - * @param Request $request | |
| 80 | - * @param Reporting $reporting | |
| 81 | - * @return array | |
| 82 | - */ | |
| 83 | - public function getTicketsChart(Request $request, Reporting $reporting) | |
| 84 | - { | |
| 85 | - list($from, $to) = self::getSanitizedDateRange($request); | |
| 86 | - | |
| 87 | - $filter = [ | |
| 88 | - 'agent_id' => $request->getSafe('agent_id', 'intval') ?: null, | |
| 89 | - 'product_id' => $request->getSafe('product_id', 'intval') ?: null, | |
| 90 | - 'mailbox_id' => $request->getSafe('mailbox_id', 'intval') ?: null, | |
| 91 | - ]; | |
| 92 | - | |
| 93 | - $agentIds = self::getAgentIdsForGroup($request); | |
| 94 | - if ($agentIds !== null) { | |
| 95 | - $filter['agent_ids'] = $agentIds; | |
| 96 | - } | |
| 97 | - | |
| 98 | - $stats = $reporting->getTicketsGrowth($from, $to, $filter); | |
| 99 | - | |
| 100 | - return [ | |
| 101 | - 'stats' => $stats | |
| 102 | - ]; | |
| 103 | - } | |
| 104 | - | |
| 105 | - /** | |
| 106 | - * getResolveChart method will generate statistics for closed tickets within a date range and return ticket number by date | |
| 107 | - * @param Request $request | |
| 108 | - * @param Reporting $reporting | |
| 109 | - * @return array | |
| 110 | - */ | |
| 111 | - public function getResolveChart(Request $request, Reporting $reporting): array | |
| 112 | - { | |
| 113 | - $type = $request->getSafe('type', 'sanitize_text_field'); | |
| 114 | - list($from, $to) = self::getSanitizedDateRange($request); | |
| 115 | - | |
| 116 | - $filter = [ | |
| 117 | - 'agent_id' => $request->getSafe('agent_id', 'intval') ?: null, | |
| 118 | - 'product_id' => $request->getSafe('product_id', 'intval') ?: null, | |
| 119 | - 'mailbox_id' => $request->getSafe('mailbox_id', 'intval') ?: null, | |
| 120 | - ]; | |
| 121 | - | |
| 122 | - $agentIds = self::getAgentIdsForGroup($request); | |
| 123 | - if ($agentIds !== null) { | |
| 124 | - $filter['agent_ids'] = $agentIds; | |
| 125 | - } | |
| 126 | - | |
| 127 | - $stats = $reporting->getTicketResolveGrowth($from, $to, $filter,$type); | |
| 128 | - | |
| 129 | - return [ | |
| 130 | - 'stats' => $stats | |
| 131 | - ]; | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * getResponseChart method will generate response statistics for ticket by date range | |
| 136 | - * @param Request $request | |
| 137 | - * @param Reporting $reporting | |
| 138 | - * @return array | |
| 139 | - */ | |
| 140 | - public function getResponseChart(Request $request, Reporting $reporting) | |
| 141 | - { | |
| 142 | - list($from, $to) = self::getSanitizedDateRange($request); | |
| 143 | - $filter = []; | |
| 144 | - | |
| 145 | - if ($person_id = $request->getSafe('agent_id', 'intval')) { | |
| 146 | - $filter['person_id'] = $person_id; | |
| 147 | - } | |
| 148 | - | |
| 149 | - $agentIds = self::getAgentIdsForGroup($request); | |
| 150 | - if ($agentIds !== null) { | |
| 151 | - $filter['person_ids'] = $agentIds; | |
| 152 | - } | |
| 153 | - | |
| 154 | - $stats = $reporting->getResponseGrowth($from, $to, $filter); | |
| 155 | - | |
| 156 | - return [ | |
| 157 | - 'stats' => $stats | |
| 158 | - ]; | |
| 159 | - } | |
| 160 | - | |
| 161 | - /** | |
| 162 | - * getAgentsSummary method will generate summary for agent | |
| 163 | - * This method will count closed tickets, open tickets, responses/interactions with ticket by agent within a date range | |
| 164 | - * @param Request $request | |
| 165 | - * @param Reporting $reporting | |
| 166 | - * @return array | |
| 167 | - */ | |
| 168 | - public function getAgentsSummary(Request $request, Reporting $reporting) | |
| 169 | - { | |
| 170 | - return [ | |
| 171 | - 'summary' => $reporting->agentSummary($request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field')) | |
| 172 | - ]; | |
| 173 | - } | |
| 174 | - | |
| 175 | - /** | |
| 176 | - * getAgentOverallReports method will return the overall statistics report for logged-in agent | |
| 177 | - * @param Request $request | |
| 178 | - * @return array | |
| 179 | - */ | |
| 180 | - /** | |
| 181 | - * getAgentGroupsSummary method will generate summary aggregated by agent group | |
| 182 | - * @param Request $request | |
| 183 | - * @param Reporting $reporting | |
| 184 | - * @return array | |
| 185 | - */ | |
| 186 | - public function getAgentGroupsSummary(Request $request, Reporting $reporting) | |
| 187 | - { | |
| 188 | - return [ | |
| 189 | - 'summary' => $reporting->agentGroupSummary( | |
| 190 | - $request->getSafe('from', 'sanitize_text_field'), | |
| 191 | - $request->getSafe('to', 'sanitize_text_field') | |
| 192 | - ) | |
| 193 | - ]; | |
| 194 | - } | |
| 195 | - | |
| 196 | 60 | public function getAgentOverallReports(Request $request): array |
| 197 | 61 | { |
| 198 | 62 | $agent = Helper::getAgentByUserId(get_current_user_id()); |
| 199 | 63 | |
| @@ -203,68 +67,14 @@ | ||
| 203 | 67 | ]; |
| 204 | 68 | } |
| 205 | 69 | |
| 206 | 70 | /** |
| 207 | - * getResponseGrowthChart method will generate response statistics for ticket by date range for product or mailbox | |
| 208 | 71 | * @param Request $request |
| 209 | 72 | * @param Reporting $reporting |
| 210 | 73 | * @return array |
| 211 | 74 | */ |
| 212 | - public function getResponseGrowthChart(Request $request, Reporting $reporting): array | |
| 213 | - { | |
| 214 | - $type = $request->getSafe('type', 'sanitize_text_field'); | |
| 215 | - list($from, $to) = self::getSanitizedDateRange($request); | |
| 216 | - | |
| 217 | - $filter = [ | |
| 218 | - 'product_id' => $request->getSafe('product_id', 'intval') ?: null, | |
| 219 | - 'mailbox_id' => $request->getSafe('mailbox_id', 'intval') ?: null, | |
| 220 | - ]; | |
| 221 | - | |
| 222 | - $stats = $reporting->getResponseGrowthChart($from, $to, $filter,$type); | |
| 223 | - | |
| 224 | - return [ | |
| 225 | - 'stats' => $stats | |
| 226 | - ]; | |
| 227 | - } | |
| 228 | - | |
| 229 | - /** | |
| 230 | - * getProductsSummary method will generate summary for product | |
| 231 | - * This method will count closed tickets, open tickets, responses, interactions with ticket by agent within a date range | |
| 232 | - * @param Request $request | |
| 233 | - * @param Reporting $reporting | |
| 234 | - * @return array | |
| 235 | - */ | |
| 236 | - public function getProductsSummary(Request $request, Reporting $reporting): array | |
| 237 | - { | |
| 238 | - return [ | |
| 239 | - 'summary' => $reporting->getSummary('product',$request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field')) | |
| 240 | - ]; | |
| 241 | - | |
| 242 | - } | |
| 243 | - | |
| 244 | - /** | |
| 245 | - * getMailBoxesSummary method will generate summary for mailbox | |
| 246 | - * This method will count closed tickets, open tickets, responses, interactions with ticket by agent within a date range | |
| 247 | - * @param Request $request | |
| 248 | - * @param Reporting $reporting | |
| 249 | - * @return array | |
| 250 | - */ | |
| 251 | - public function getMailBoxesSummary(Request $request, Reporting $reporting): array | |
| 252 | - { | |
| 253 | - return [ | |
| 254 | - 'summary' => $reporting->getSummary('mailbox',$request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field')) | |
| 255 | - ]; | |
| 256 | - } | |
| 257 | - | |
| 258 | - /** | |
| 259 | - * getAgentResolveChart method will generate ticket data for resolved ticket | |
| 260 | - * @param Request $request | |
| 261 | - * @param Reporting $reporting | |
| 262 | - * @return array | |
| 263 | - */ | |
| 264 | 75 | public function getAgentResolveChart(Request $request, Reporting $reporting) |
| 265 | 76 | { |
| 266 | - //Get logged in agent information | |
| 267 | 77 | $agent = Helper::getAgentByUserId(get_current_user_id()); |
| 268 | 78 | list($from, $to) = self::getSanitizedDateRange($request); |
| 269 | 79 | |
| 270 | 80 | return [ |
| @@ -272,9 +82,8 @@ | ||
| 272 | 82 | ]; |
| 273 | 83 | } |
| 274 | 84 | |
| 275 | 85 | /** |
| 276 | - * getAgentResponseChart method will generate the statistics of response by agent in tickets within date range | |
| 277 | 86 | * @param Request $request |
| 278 | 87 | * @param Reporting $reporting |
| 279 | 88 | * @return array |
| 280 | 89 | */ |
| @@ -288,10 +97,10 @@ | ||
| 288 | 97 | ]; |
| 289 | 98 | } |
| 290 | 99 | |
| 291 | 100 | /** |
| 292 | - * getPersonalSummary method will generate summary for specific agent | |
| 293 | - * This method will count closed tickets, open tickets, responses/interactions with ticket by agent within a date range | |
| 101 | + * Closed/open ticket and response counts for the logged-in agent in a date range. | |
| 102 | + * | |
| 294 | 103 | * @param Reporting $reporting |
| 295 | 104 | * @param Request $request |
| 296 | 105 | * @return array |
| 297 | 106 | */ |
| @@ -303,41 +112,13 @@ | ||
| 303 | 112 | 'summary' => $reporting->agentSummary($request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field'), $agent->id) |
| 304 | 113 | ]; |
| 305 | 114 | } |
| 306 | 115 | |
| 307 | - public function dayTimeStats(Reporting $reporting, Request $request) | |
| 308 | - { | |
| 309 | - list($from, $to) = self::getSanitizedDateRange($request); | |
| 310 | - | |
| 311 | - $filter = [ | |
| 312 | - 'report_type' => $request->getSafe('report_type', 'sanitize_text_field') ?: null, | |
| 313 | - 'agent_id' => $request->getSafe('agent_id', 'intval') ?: null, | |
| 314 | - ]; | |
| 315 | - | |
| 316 | - $results = $reporting->getQueryResults($from, $to, $filter); | |
| 317 | - | |
| 318 | - return $this->send([ | |
| 319 | - 'stats' => $results | |
| 320 | - ]); | |
| 321 | - } | |
| 322 | - | |
| 323 | - public function ticketResponseStats(Reporting $reporting, Request $request) | |
| 324 | - { | |
| 325 | - | |
| 326 | - list($from, $to) = self::getSanitizedDateRange($request); | |
| 327 | - | |
| 328 | - $filter = [ | |
| 329 | - 'person_type' => $request->getSafe('person_type', 'sanitize_text_field') ?: null, | |
| 330 | - 'person_id' => $request->getSafe('person_id', 'intval') ?: null, | |
| 331 | - ]; | |
| 332 | - | |
| 333 | - return $reporting->getTicketResponseStats($from, $to, $filter); | |
| 334 | - } | |
| 335 | - | |
| 336 | 116 | /** |
| 337 | - * getStats method will return statistics similar to getOverallReports but with filters | |
| 338 | - * Returns: New Tickets, Active Tickets, Closed Tickets, and Responses | |
| 339 | - * Filters: date_range, mailbox_id (business_box), product_id, agent_id, customer_id | |
| 117 | + * Overview counts filtered by date_range, mailbox_id (or business_box), product_id, | |
| 118 | + * agent_id, agent_group_id and customer_id. With agent_id, active_tickets is | |
| 119 | + * replaced by the agent's total_replies. | |
| 120 | + * | |
| 340 | 121 | * @param Request $request |
| 341 | 122 | * @return array |
| 342 | 123 | */ |
| 343 | 124 | public function getStats(Request $request) |
| @@ -352,9 +133,13 @@ | ||
| 352 | 133 | ]; |
| 353 | 134 | |
| 354 | 135 | $agentIds = self::getAgentIdsForGroup($request); |
| 355 | 136 | |
| 356 | - $baseQuery = Ticket::query(); | |
| 137 | + $access = new AgentTicketAccess(); | |
| 138 | + | |
| 139 | + // Bound first: a caller-supplied mailbox_id below is not proof of access. | |
| 140 | + $baseQuery = $access->applyMailboxRestrictionScope(Ticket::query()); | |
| 141 | + | |
| 357 | 142 | foreach ($filters as $field => $value) { |
| 358 | 143 | if ($value) { |
| 359 | 144 | $baseQuery->where($field, $value); |
| 360 | 145 | } |
| @@ -388,13 +173,24 @@ | ||
| 388 | 173 | $openQuery->where('status', '!=', 'closed'); |
| 389 | 174 | $applyDateRange($openQuery); |
| 390 | 175 | $openTickets = $openQuery->count(); |
| 391 | 176 | |
| 392 | - $responsesQuery = Conversation::query()->where('conversation_type', 'response'); | |
| 177 | + $agentId = $filters['agent_id']; | |
| 393 | 178 | |
| 394 | - if (array_filter($filters) || $agentIds !== null) { | |
| 395 | - $responsesQuery->whereHas('ticket', function ($q) use ($filters, $agentIds) { | |
| 396 | - foreach ($filters as $field => $value) { | |
| 179 | + $responsesQuery = $access->applyMailboxRestrictionScopeViaTicket( | |
| 180 | + Conversation::query()->where('conversation_type', 'response') | |
| 181 | + ); | |
| 182 | + | |
| 183 | + // For a single agent, count the replies they wrote, not every reply on tickets assigned to them. | |
| 184 | + $ticketFilters = $filters; | |
| 185 | + if ($agentId) { | |
| 186 | + $responsesQuery->where('person_id', $agentId); | |
| 187 | + unset($ticketFilters['agent_id']); | |
| 188 | + } | |
| 189 | + | |
| 190 | + if (array_filter($ticketFilters) || $agentIds !== null) { | |
| 191 | + $responsesQuery->whereHas('ticket', function ($q) use ($ticketFilters, $agentIds) { | |
| 192 | + foreach ($ticketFilters as $field => $value) { | |
| 397 | 193 | if ($value) { |
| 398 | 194 | $q->where($field, $value); |
| 399 | 195 | } |
| 400 | 196 | } |
| @@ -406,20 +202,10 @@ | ||
| 406 | 202 | |
| 407 | 203 | $applyDateRange($responsesQuery, 'created_at'); |
| 408 | 204 | $responses = $responsesQuery->count(); |
| 409 | 205 | |
| 410 | - $agentId = $filters['agent_id']; | |
| 411 | - | |
| 412 | 206 | if ($agentId) { |
| 413 | - $repliesQuery = Conversation::query() | |
| 414 | - ->where('person_id', $agentId) | |
| 415 | - ->where('conversation_type', 'response'); | |
| 416 | - | |
| 417 | - $applyDateRange($repliesQuery, 'created_at'); | |
| 418 | - $totalReplies = $repliesQuery->count(); | |
| 419 | - | |
| 420 | 207 | $stats = [ |
| 421 | - 'total_replies' => $totalReplies, | |
| 422 | 208 | 'new_tickets' => $newTickets, |
| 423 | 209 | 'closed_tickets' => $closedTickets, |
| 424 | 210 | 'responses' => $responses, |
| 425 | 211 | 'open_tickets' => $openTickets, |
| @@ -436,9 +222,8 @@ | ||
| 436 | 222 | ]; |
| 437 | 223 | } |
| 438 | 224 | |
| 439 | 225 | $labels = [ |
| 440 | - 'total_replies' => __('Total Replies', 'fluent-support'), | |
| 441 | 226 | 'new_tickets' => __('New Tickets', 'fluent-support'), |
| 442 | 227 | 'active_tickets' => __('Active Tickets', 'fluent-support'), |
| 443 | 228 | 'closed_tickets' => __('Closed Tickets', 'fluent-support'), |
| 444 | 229 | 'responses' => __('Responses', 'fluent-support'), |
| @@ -443,8 +228,12 @@ | ||
| 443 | 228 | 'closed_tickets' => __('Closed Tickets', 'fluent-support'), |
| 444 | 229 | 'responses' => __('Responses', 'fluent-support'), |
| 445 | 230 | 'open_tickets' => __('Open Tickets', 'fluent-support'), |
| 446 | 231 | ]; |
| 232 | + | |
| 233 | + if ($agentId) { | |
| 234 | + $labels['responses'] = __('Total Responses', 'fluent-support'); | |
| 235 | + } | |
| 447 | 236 | |
| 448 | 237 | $overallReports = []; |
| 449 | 238 | foreach ($stats as $key => $count) { |
| 450 | 239 | $overallReports[$key] = [ |