first response initial pass

This commit is contained in:
Mark Gregory
2026-06-23 10:36:18 +01:00
parent c86c804bcf
commit d449f01593
5 changed files with 93 additions and 20 deletions
+68 -14
View File
@@ -6,6 +6,7 @@ namespace Modules\Helpdesk\Services;
use App\Models\User;
use Modules\Helpdesk\Models\SlaPolicy;
use Modules\Helpdesk\Models\Comment;
use Modules\Helpdesk\Models\Ticket;
use Modules\Helpdesk\Models\TicketSlaEvent;
@@ -38,28 +39,81 @@ final class SlaService
return $ticket->fresh() ?? $ticket;
}
/**
* Record a public ticket response and satisfy the first-response SLA.
*
* A public Helpdesk response is deliberate customer-visible communication,
* so it must satisfy the first-response SLA even in local/single-user
* support workflows where the same authenticated user may be testing both
* sides of the ticket conversation.
*/
public function recordPublicResponse(Ticket $ticket, User $actor): void
{
if ($ticket->first_response_at === null && (int) $ticket->user_id !== (int) $actor->id) {
$breached = $ticket->sla_first_response_due_at !== null && now()->greaterThan($ticket->sla_first_response_due_at);
$ticket->forceFill([
'first_response_at' => now(),
'sla_first_response_breached_at' => $breached ? now() : $ticket->sla_first_response_breached_at,
])->save();
$respondedAt = now();
TicketSlaEvent::query()->create([
'ticket_id' => $ticket->id,
'sla_policy_id' => $ticket->sla_policy_id,
'event' => 'sla.first_response',
'due_at' => $ticket->sla_first_response_due_at,
'occurred_at' => now(),
'breached' => $breached,
]);
if ($ticket->first_response_at === null) {
$this->markFirstResponse($ticket, $respondedAt);
}
$ticket->increment('public_replies_count');
}
/**
* Rebuild first-response SLA state from the earliest public comment.
*
* This keeps older tickets correct when public comments existed before the
* explicit Public/Internal selector was added or when counters were manually
* changed outside the SLA service.
*/
public function syncFirstResponseFromComments(Ticket $ticket): void
{
if ($ticket->first_response_at !== null) {
return;
}
/** @var Comment|null $firstPublicComment */
$firstPublicComment = Comment::query()
->where('ticket_id', $ticket->id)
->where('is_internal', false)
->whereNull('tombstoned_at')
->oldest('created_at')
->first();
if ($firstPublicComment === null) {
return;
}
$this->markFirstResponse($ticket, $firstPublicComment->created_at ?? now());
}
private function markFirstResponse(Ticket $ticket, mixed $respondedAt): void
{
$respondedAt = $respondedAt instanceof \Illuminate\Support\Carbon
? $respondedAt
: now();
$breached = $ticket->sla_first_response_due_at !== null
&& $respondedAt->greaterThan($ticket->sla_first_response_due_at);
$ticket->forceFill([
'first_response_at' => $respondedAt,
'sla_first_response_breached_at' => $breached ? $respondedAt : null,
])->save();
TicketSlaEvent::query()->firstOrCreate(
[
'ticket_id' => $ticket->id,
'event' => 'sla.first_response',
],
[
'sla_policy_id' => $ticket->sla_policy_id,
'due_at' => $ticket->sla_first_response_due_at,
'occurred_at' => $respondedAt,
'breached' => $breached,
],
);
}
public function recordClosure(Ticket $ticket): void
{
$breached = $ticket->sla_resolution_due_at !== null && now()->greaterThan($ticket->sla_resolution_due_at);