| @@ -5,9 +5,8 @@ | ||
| 5 | 5 | use FluentSupport\App\Models\Agent; |
| 6 | 6 | use FluentSupport\App\Models\Conversation; |
| 7 | 7 | use FluentSupport\App\Models\Ticket; |
| 8 | 8 | use FluentSupport\App\Models\Product; |
| 9 | -use FluentSupport\App\Services\Tickets\AgentTicketAccess; | |
| 10 | 9 | |
| 11 | 10 | /** |
| 12 | 11 | * StatModule class is responsible for getting data related to report |
| 13 | 12 | * @package FluentSupport\App\Modules |
| @@ -34,41 +33,37 @@ | ||
| 34 | 33 | |
| 35 | 34 | $startDate = gmdate('Y-m-d 00:00.01', strtotime($startDate)); |
| 36 | 35 | $endDate = gmdate('Y-m-d 23:59.59', strtotime($endDate)); |
| 37 | 36 | |
| 38 | - $access = new AgentTicketAccess(); | |
| 39 | - | |
| 40 | 37 | //Get list of new ticket by agent |
| 41 | - $newTickets = $access->applyMailboxRestrictionScope( | |
| 42 | - Ticket::where('agent_id', $agentId)->where('status', 'new') | |
| 43 | - )->count(); | |
| 38 | + $newTickets = Ticket::where('agent_id', $agentId) | |
| 39 | + ->where('status', 'new') | |
| 40 | + ->count(); | |
| 44 | 41 | |
| 45 | 42 | //Get list of active ticket by agent |
| 46 | - $activeTickets = $access->applyMailboxRestrictionScope( | |
| 47 | - Ticket::where('agent_id', $agentId)->where('status', 'active') | |
| 48 | - )->count(); | |
| 43 | + $activeTickets = Ticket::where('agent_id', $agentId) | |
| 44 | + ->where('status', 'active') | |
| 45 | + ->count(); | |
| 49 | 46 | |
| 50 | 47 | //Get list of closed ticket by agent within a date range(default today) |
| 51 | - $closedTickets = $access->applyMailboxRestrictionScope( | |
| 52 | - Ticket::where('agent_id', $agentId) | |
| 53 | - ->where('status', 'closed') | |
| 54 | - ->whereBetween('resolved_at', [$startDate, $endDate]) | |
| 55 | - )->count(); | |
| 48 | + $closedTickets = Ticket::where('agent_id', $agentId) | |
| 49 | + ->where('status', 'closed') | |
| 50 | + ->whereBetween('resolved_at', [$startDate, $endDate]) | |
| 51 | + ->count(); | |
| 56 | 52 | |
| 57 | 53 | //Get list of response by agent id within a date range(default today) |
| 58 | - $responses = $access->applyMailboxRestrictionScopeViaTicket( | |
| 59 | - Conversation::where('conversation_type', 'response') | |
| 60 | - ->where('person_id', $agentId) | |
| 61 | - ->whereBetween('created_at', [$startDate, $endDate]) | |
| 62 | - )->count(); | |
| 54 | + $responses = Conversation::where('conversation_type', 'response') | |
| 55 | + ->where('person_id', $agentId) | |
| 56 | + ->whereBetween('created_at', [$startDate, $endDate]) | |
| 57 | + ->count(); | |
| 63 | 58 | |
| 64 | 59 | //Count the response in tickets within a date range(default today) for agent |
| 65 | - $interactions = $access->applyMailboxRestrictionScopeViaTicket( | |
| 66 | - Conversation::where('person_id', $agentId) | |
| 67 | - ->where('conversation_type', 'response') | |
| 68 | - ->whereBetween('created_at', [$startDate, $endDate]) | |
| 69 | - ->groupBy('ticket_id') | |
| 70 | - )->get()->count(); | |
| 60 | + $interactions = Conversation::where('person_id', $agentId) | |
| 61 | + ->where('conversation_type', 'response') | |
| 62 | + ->whereBetween('created_at', [$startDate, $endDate]) | |
| 63 | + ->groupBy('ticket_id') | |
| 64 | + ->get() | |
| 65 | + ->count(); | |
| 71 | 66 | |
| 72 | 67 | return [ |
| 73 | 68 | 'new_tickets' => [ |
| 74 | 69 | 'title' => __('New Tickets', 'fluent-support'), |
| @@ -99,24 +94,20 @@ | ||
| 99 | 94 | * @return array[] |
| 100 | 95 | */ |
| 101 | 96 | public static function getOverAllStats() |
| 102 | 97 | { |
| 103 | - $access = new AgentTicketAccess(); | |
| 98 | + $newTickets = Ticket::where('status', 'new') | |
| 99 | + ->count(); | |
| 104 | 100 | |
| 105 | - // Same mailbox boundary as the ticket list; these totals are agent-facing. | |
| 106 | - $newTickets = $access->applyMailboxRestrictionScope(Ticket::where('status', 'new')) | |
| 101 | + $activeTickets = Ticket::where('status', 'active') | |
| 107 | 102 | ->count(); |
| 108 | 103 | |
| 109 | - $activeTickets = $access->applyMailboxRestrictionScope(Ticket::where('status', 'active')) | |
| 104 | + $closedTickets = Ticket::where('status', 'closed') | |
| 110 | 105 | ->count(); |
| 111 | 106 | |
| 112 | - $closedTickets = $access->applyMailboxRestrictionScope(Ticket::where('status', 'closed')) | |
| 107 | + $responses = Conversation::where('conversation_type', 'response') | |
| 113 | 108 | ->count(); |
| 114 | 109 | |
| 115 | - $responses = $access->applyMailboxRestrictionScopeViaTicket( | |
| 116 | - Conversation::where('conversation_type', 'response') | |
| 117 | - )->count(); | |
| 118 | - | |
| 119 | 110 | return [ |
| 120 | 111 | 'new_tickets' => [ |
| 121 | 112 | 'title' => __('New Tickets', 'fluent-support'), |
| 122 | 113 | 'count' => $newTickets |
| @@ -142,31 +133,23 @@ | ||
| 142 | 133 | * @param bool|int $agentId By default value set to false however when it gets an agent id it will fetch |
| 143 | 134 | * the result by this id |
| 144 | 135 | * @return array result in array format |
| 145 | 136 | */ |
| 146 | - public static function getTodayStats($agentId = false, ?AgentTicketAccess $access = null) | |
| 137 | + public static function getTodayStats($agentId = false) | |
| 147 | 138 | { |
| 148 | 139 | $start = gmdate('Y-m-d 00:00.01'); |
| 149 | 140 | $end = gmdate('Y-m-d 23:59.59'); |
| 150 | 141 | |
| 151 | - $access = $access ?: new AgentTicketAccess(); | |
| 142 | + $newTickets = Ticket::whereBetween('created_at', [$start, $end]); | |
| 152 | 143 | |
| 153 | - $newTickets = $access->applyMailboxRestrictionScope( | |
| 154 | - Ticket::whereBetween('created_at', [$start, $end]) | |
| 155 | - ); | |
| 144 | + $closedTickets = Ticket::where('status', 'closed')->whereBetween('resolved_at', [$start, $end]); | |
| 156 | 145 | |
| 157 | - $closedTickets = $access->applyMailboxRestrictionScope( | |
| 158 | - Ticket::where('status', 'closed')->whereBetween('resolved_at', [$start, $end]) | |
| 159 | - ); | |
| 146 | + $responses = Conversation::where('conversation_type', 'response') | |
| 147 | + ->whereHas('person', function ($q) { | |
| 148 | + $q->where('person_type', 'agent'); | |
| 149 | + }) | |
| 150 | + ->whereBetween('created_at', [$start, $end]); | |
| 160 | 151 | |
| 161 | - $responses = $access->applyMailboxRestrictionScopeViaTicket( | |
| 162 | - Conversation::where('conversation_type', 'response') | |
| 163 | - ->whereHas('person', function ($q) { | |
| 164 | - $q->where('person_type', 'agent'); | |
| 165 | - }) | |
| 166 | - ->whereBetween('created_at', [$start, $end]) | |
| 167 | - ); | |
| 168 | - | |
| 169 | 152 | if ($agentId) { |
| 170 | 153 | $newTickets->where('agent_id', $agentId); |
| 171 | 154 | $closedTickets->where('agent_id', $agentId); |
| 172 | 155 | $responses->where('person_id', $agentId); |
| @@ -195,23 +178,19 @@ | ||
| 195 | 178 | */ |
| 196 | 179 | public static function getAgentOverallStats($agentId) |
| 197 | 180 | { |
| 198 | 181 | //Get count of response by the agent |
| 199 | - $replies_count = (new AgentTicketAccess())->applyMailboxRestrictionScopeViaTicket( | |
| 200 | - Conversation::where('person_id', $agentId) | |
| 201 | - )->count(); | |
| 182 | + $replies_count = Conversation::where('person_id', $agentId)->count(); | |
| 202 | 183 | |
| 203 | 184 | //Get the number of interactions/responses by agent with tickets |
| 204 | - $interactions_count = (new AgentTicketAccess())->applyMailboxRestrictionScopeViaTicket( | |
| 205 | - Conversation::where('person_id', $agentId) | |
| 206 | - ->where('conversation_type', 'response') | |
| 207 | - ->groupBy('ticket_id') | |
| 208 | - )->get()->count(); | |
| 185 | + $interactions_count = Conversation::where('person_id', $agentId) | |
| 186 | + ->where('conversation_type', 'response') | |
| 187 | + ->groupBy('ticket_id') | |
| 188 | + ->get() | |
| 189 | + ->count(); | |
| 209 | 190 | |
| 210 | 191 | //Get the number of tickets that are closed by this agent |
| 211 | - $total_closed = (new AgentTicketAccess())->applyMailboxRestrictionScope( | |
| 212 | - Ticket::where('agent_id', $agentId)->where('status', 'closed') | |
| 213 | - )->count(); | |
| 192 | + $total_closed = Ticket::where('agent_id', $agentId)->where('status', 'closed')->count(); | |
| 214 | 193 | |
| 215 | 194 | return [ |
| 216 | 195 | 'replies_count' => [ |
| 217 | 196 | 'title' => __('Total Replies', 'fluent-support'), |
| @@ -235,14 +214,13 @@ | ||
| 235 | 214 | public static function getActiveTicketsByProductStats() |
| 236 | 215 | { |
| 237 | 216 | $products = Product::all(); |
| 238 | 217 | $result = []; |
| 239 | - $access = new AgentTicketAccess(); | |
| 240 | 218 | |
| 241 | 219 | foreach ($products as $product) { |
| 242 | 220 | $result[$product->id] = [ |
| 243 | 221 | 'title' => $product->title, |
| 244 | - 'count' => static::countAwaitingTickets('product_id', $product->id, $access) | |
| 222 | + 'count' => static::countAwaitingTickets('product_id', $product->id) | |
| 245 | 223 | ]; |
| 246 | 224 | } |
| 247 | 225 | |
| 248 | 226 | return $result; |
| @@ -254,19 +232,16 @@ | ||
| 254 | 232 | * @param string $whereClause // This is the where clause that will be used in the query |
| 255 | 233 | * @param string $whereClauseValue // This is the value of the where clause |
| 256 | 234 | * @return int $awatingTicketCount |
| 257 | 235 | */ |
| 258 | - public static function countAwaitingTickets($whereClause = null, $whereClauseValue = null, ?AgentTicketAccess $access = null) | |
| 236 | + public static function countAwaitingTickets($whereClause = null, $whereClauseValue = null) | |
| 259 | 237 | { |
| 260 | - // Must be a builder: ->where() on a bare model returns a new one, losing the scope. | |
| 261 | - $ticket = Ticket::query(); | |
| 238 | + $ticket = new Ticket; | |
| 262 | 239 | |
| 263 | 240 | if ($whereClause && $whereClauseValue) { |
| 264 | 241 | $ticket = $ticket->where(sanitize_text_field($whereClause), sanitize_text_field($whereClauseValue)); |
| 265 | 242 | } |
| 266 | 243 | |
| 267 | - $ticket = ($access ?: new AgentTicketAccess())->applyMailboxRestrictionScope($ticket); | |
| 268 | - | |
| 269 | 244 | $awatingTicketCount = $ticket->where('status', '!=', 'closed') |
| 270 | 245 | ->where(function ($query) { |
| 271 | 246 | $query->whereColumn('last_agent_response', '<', 'last_customer_response') |
| 272 | 247 | ->orWhereNull('last_agent_response') |
| @@ -282,15 +257,11 @@ | ||
| 282 | 257 | */ |
| 283 | 258 | public static function getAgentTodayStats() |
| 284 | 259 | { |
| 285 | 260 | $stats = []; |
| 286 | - // One instance for the whole loop: resolving restrictions costs an agent and | |
| 287 | - // meta read, and a fresh instance per agent would repeat it 44 times. | |
| 288 | - $access = new AgentTicketAccess(); | |
| 289 | - | |
| 290 | - Agent::get()->each(function ($agent) use (&$stats, $access) { | |
| 291 | - $agentStat = static::getTodayStats($agent->id, $access); | |
| 292 | - $waiting = static::countAwaitingTickets('agent_id', $agent->id, $access); | |
| 261 | + Agent::get()->each(function ($agent) use (&$stats) { | |
| 262 | + $agentStat = static::getTodayStats($agent->id); | |
| 263 | + $waiting = static::countAwaitingTickets('agent_id', $agent->id); | |
| 293 | 264 | if(!empty($agentStat['responses']['count']) || $waiting) { |
| 294 | 265 | $stats[] = [ |
| 295 | 266 | 'agent_id' => $agent->id, |
| 296 | 267 | 'agent_name' => $agent->full_name, |