Repo rebuild
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Modules\Helpdesk\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Modules\Helpdesk\Models\Category;
|
||||
use Modules\Helpdesk\Models\Ticket;
|
||||
|
||||
final class AgentAssignmentService
|
||||
{
|
||||
/**
|
||||
* Select the least-loaded available agent for a category.
|
||||
*/
|
||||
public function selectAgentId(?int $categoryId): ?int
|
||||
{
|
||||
$query = User::query()
|
||||
->select('users.*')
|
||||
->where(static function (Builder $query): void {
|
||||
$query->where('helpdesk_agent', true)->orWhere('helpdesk_admin', true);
|
||||
})
|
||||
->selectSub(
|
||||
Ticket::query()
|
||||
->selectRaw('count(*)')
|
||||
->whereColumn('helpdesk_tickets.agent_id', 'users.id')
|
||||
->whereNull('helpdesk_tickets.completed_at'),
|
||||
'open_helpdesk_tickets_count',
|
||||
);
|
||||
|
||||
if ($categoryId !== null) {
|
||||
$category = Category::query()->with('agents:id')->find($categoryId);
|
||||
$agentIds = $category?->agents->pluck('id')->all() ?? [];
|
||||
|
||||
if ($agentIds !== []) {
|
||||
$query->whereIn('id', $agentIds);
|
||||
}
|
||||
}
|
||||
|
||||
/** @var User|null $agent */
|
||||
$agent = $query->orderBy('open_helpdesk_tickets_count')->orderBy('id')->first();
|
||||
|
||||
return $agent?->id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user