| @@ -6,8 +6,9 @@ | ||
| 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 | /** |
| @@ -18,9 +19,9 @@ | ||
| 18 | 19 | * @version 1.0.0 |
| 19 | 20 | */ |
| 20 | 21 | class ReportingController extends Controller |
| 21 | 22 | { |
| 22 | - private static function getSanitizedDateRange(Request $request) | |
| 23 | + public static function getSanitizedDateRange(Request $request) | |
| 23 | 24 | { |
| 24 | 25 | $dateRange = $request->get('date_range', []); |
| 25 | 26 | |
| 26 | 27 | if (is_array($dateRange) && count($dateRange) >= 2) { |
| @@ -40,9 +41,9 @@ | ||
| 40 | 41 | |
| 41 | 42 | return ['', '']; |
| 42 | 43 | } |
| 43 | 44 | |
| 44 | - private static function getAgentIdsForGroup(Request $request) | |
| 45 | + public static function getAgentIdsForGroup(Request $request) | |
| 45 | 46 | { |
| 46 | 47 | $groupId = $request->getSafe('agent_group_id', 'intval'); |
| 47 | 48 | if (!$groupId) { |
| 48 | 49 | return null; |
| @@ -53,21 +54,8 @@ | ||
| 53 | 54 | ->pluck('source_id') |
| 54 | 55 | ->toArray(); |
| 55 | 56 | } |
| 56 | 57 | |
| 57 | - /** | |
| 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 | |
| 60 | - * @param Request $request | |
| 61 | - * @return array | |
| 62 | - */ | |
| 63 | - public function getOverallReports(Request $request) | |
| 64 | - { | |
| 65 | - return [ | |
| 66 | - 'overall_reports' => StatModule::getOverAllStats() | |
| 67 | - ]; | |
| 68 | - } | |
| 69 | - | |
| 70 | 58 | public function getActiveTicketsByProduct() |
| 71 | 59 | { |
| 72 | 60 | return [ |
| 73 | 61 | 'stats' => StatModule::getActiveTicketsByProductStats() |
| @@ -74,126 +62,12 @@ | ||
| 74 | 62 | ]; |
| 75 | 63 | } |
| 76 | 64 | |
| 77 | 65 | /** |
| 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 | 66 | * getAgentOverallReports method will return the overall statistics report for logged-in agent |
| 177 | 67 | * @param Request $request |
| 178 | 68 | * @return array |
| 179 | 69 | */ |
| 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 | 70 | public function getAgentOverallReports(Request $request): array |
| 197 | 71 | { |
| 198 | 72 | $agent = Helper::getAgentByUserId(get_current_user_id()); |
| 199 | 73 | |
| @@ -203,60 +77,8 @@ | ||
| 203 | 77 | ]; |
| 204 | 78 | } |
| 205 | 79 | |
| 206 | 80 | /** |
| 207 | - * getResponseGrowthChart method will generate response statistics for ticket by date range for product or mailbox | |
| 208 | - * @param Request $request | |
| 209 | - * @param Reporting $reporting | |
| 210 | - * @return array | |
| 211 | - */ | |
| 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 | 81 | * getAgentResolveChart method will generate ticket data for resolved ticket |
| 260 | 82 | * @param Request $request |
| 261 | 83 | * @param Reporting $reporting |
| 262 | 84 | * @return array |
| @@ -303,37 +125,8 @@ | ||
| 303 | 125 | 'summary' => $reporting->agentSummary($request->getSafe('from', 'sanitize_text_field'), $request->getSafe('to', 'sanitize_text_field'), $agent->id) |
| 304 | 126 | ]; |
| 305 | 127 | } |
| 306 | 128 | |
| 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 | 129 | /** |
| 337 | 130 | * getStats method will return statistics similar to getOverallReports but with filters |
| 338 | 131 | * Returns: New Tickets, Active Tickets, Closed Tickets, and Responses |
| 339 | 132 | * Filters: date_range, mailbox_id (business_box), product_id, agent_id, customer_id |
| @@ -352,9 +145,13 @@ | ||
| 352 | 145 | ]; |
| 353 | 146 | |
| 354 | 147 | $agentIds = self::getAgentIdsForGroup($request); |
| 355 | 148 | |
| 356 | - $baseQuery = Ticket::query(); | |
| 149 | + $access = new AgentTicketAccess(); | |
| 150 | + | |
| 151 | + // Bound first: a caller-supplied mailbox_id below is not proof of access. | |
| 152 | + $baseQuery = $access->applyMailboxRestrictionScope(Ticket::query()); | |
| 153 | + | |
| 357 | 154 | foreach ($filters as $field => $value) { |
| 358 | 155 | if ($value) { |
| 359 | 156 | $baseQuery->where($field, $value); |
| 360 | 157 | } |
| @@ -388,9 +185,11 @@ | ||
| 388 | 185 | $openQuery->where('status', '!=', 'closed'); |
| 389 | 186 | $applyDateRange($openQuery); |
| 390 | 187 | $openTickets = $openQuery->count(); |
| 391 | 188 | |
| 392 | - $responsesQuery = Conversation::query()->where('conversation_type', 'response'); | |
| 189 | + $responsesQuery = $access->applyMailboxRestrictionScopeViaTicket( | |
| 190 | + Conversation::query()->where('conversation_type', 'response') | |
| 191 | + ); | |
| 393 | 192 | |
| 394 | 193 | if (array_filter($filters) || $agentIds !== null) { |
| 395 | 194 | $responsesQuery->whereHas('ticket', function ($q) use ($filters, $agentIds) { |
| 396 | 195 | foreach ($filters as $field => $value) { |
| @@ -409,11 +208,13 @@ | ||
| 409 | 208 | |
| 410 | 209 | $agentId = $filters['agent_id']; |
| 411 | 210 | |
| 412 | 211 | if ($agentId) { |
| 413 | - $repliesQuery = Conversation::query() | |
| 414 | - ->where('person_id', $agentId) | |
| 415 | - ->where('conversation_type', 'response'); | |
| 212 | + $repliesQuery = $access->applyMailboxRestrictionScopeViaTicket( | |
| 213 | + Conversation::query() | |
| 214 | + ->where('person_id', $agentId) | |
| 215 | + ->where('conversation_type', 'response') | |
| 216 | + ); | |
| 416 | 217 | |
| 417 | 218 | $applyDateRange($repliesQuery, 'created_at'); |
| 418 | 219 | $totalReplies = $repliesQuery->count(); |
| 419 | 220 | |