new helpdesk module
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Modules\Helpdesk\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Modules\Helpdesk\Models\Ticket;
|
||||
use Modules\Helpdesk\Models\WorkflowLog;
|
||||
use Modules\Helpdesk\Models\WorkflowTemplate;
|
||||
use Throwable;
|
||||
|
||||
final class WorkflowNotificationService
|
||||
{
|
||||
/**
|
||||
* Send all active workflow messages for the supplied ticket event.
|
||||
*
|
||||
* @param Ticket $ticket
|
||||
* @param string $event
|
||||
* @param User|null $actor
|
||||
* @return void
|
||||
*/
|
||||
public function dispatch(Ticket $ticket, string $event, ?User $actor = null): void
|
||||
{
|
||||
$ticket->loadMissing(['owner', 'agent', 'status', 'priority', 'category', 'project', 'team']);
|
||||
|
||||
WorkflowTemplate::query()
|
||||
->where('event', $event)
|
||||
->where('is_active', true)
|
||||
->get()
|
||||
->each(function (WorkflowTemplate $template) use ($ticket, $event, $actor): void {
|
||||
$recipient = $this->recipient($template->to_role, $ticket, $actor);
|
||||
|
||||
if ($recipient === null || blank($recipient->email ?? null)) {
|
||||
$this->log($template, $ticket, $event, null, 'skipped', 'No recipient email was available.');
|
||||
return;
|
||||
}
|
||||
|
||||
$subject = $this->renderText($template->subject, $ticket, $actor);
|
||||
$html = $this->wrapHtml($this->renderText($template->html_body, $ticket, $actor), $ticket);
|
||||
$text = $this->renderText($template->text_body ?: strip_tags($template->html_body), $ticket, $actor);
|
||||
|
||||
try {
|
||||
Mail::html($html, function ($message) use ($recipient, $template, $subject): void {
|
||||
$message->to($recipient->email, $recipient->name ?? null)->subject($subject);
|
||||
|
||||
if (filled($template->cc)) {
|
||||
$message->cc(array_map('trim', explode(',', (string) $template->cc)));
|
||||
}
|
||||
|
||||
if (filled($template->bcc)) {
|
||||
$message->bcc(array_map('trim', explode(',', (string) $template->bcc)));
|
||||
}
|
||||
});
|
||||
|
||||
$this->log($template, $ticket, $event, (string) $recipient->email, 'sent', null, $subject);
|
||||
} catch (Throwable $exception) {
|
||||
Log::warning('Helpdesk workflow email failed.', ['event' => $event, 'ticket_id' => $ticket->id, 'error' => $exception->getMessage()]);
|
||||
$this->log($template, $ticket, $event, (string) $recipient->email, 'failed', $exception->getMessage(), $subject);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** @return User|null */
|
||||
private function recipient(string $role, Ticket $ticket, ?User $actor): ?User
|
||||
{
|
||||
return match ($role) {
|
||||
'agent' => $ticket->agent,
|
||||
'actor' => $actor,
|
||||
default => $ticket->owner,
|
||||
};
|
||||
}
|
||||
|
||||
private function renderText(string $template, Ticket $ticket, ?User $actor): string
|
||||
{
|
||||
$tokens = [
|
||||
'{{ ticket.reference }}' => $ticket->reference,
|
||||
'{{ ticket.subject }}' => $ticket->subject,
|
||||
'{{ ticket.status }}' => (string) ($ticket->status?->name ?? ''),
|
||||
'{{ ticket.priority }}' => (string) ($ticket->priority?->name ?? ''),
|
||||
'{{ ticket.category }}' => (string) ($ticket->category?->name ?? ''),
|
||||
'{{ ticket.project }}' => (string) ($ticket->project?->title ?? ''),
|
||||
'{{ ticket.team }}' => (string) ($ticket->team?->name ?? ''),
|
||||
'{{ requester.name }}' => (string) ($ticket->owner?->name ?? ''),
|
||||
'{{ agent.name }}' => (string) ($ticket->agent?->name ?? ''),
|
||||
'{{ actor.name }}' => (string) ($actor?->name ?? ''),
|
||||
'{{ ticket.url }}' => route('helpdesk.tickets.show', ['ticket' => $ticket->getKey()]),
|
||||
];
|
||||
|
||||
return strtr($template, $tokens);
|
||||
}
|
||||
|
||||
private function wrapHtml(string $body, Ticket $ticket): string
|
||||
{
|
||||
return '<div style="font-family:Inter,Arial,sans-serif;background:#f6f8fb;padding:24px">'
|
||||
. '<div style="max-width:720px;margin:0 auto;background:#ffffff;border:1px solid #d8dee8;border-radius:18px;overflow:hidden">'
|
||||
. '<div style="background:#0f172a;color:#ffffff;padding:24px 28px">'
|
||||
. '<div style="font-size:12px;letter-spacing:.18em;text-transform:uppercase;color:#bfdbfe">Helpdesk notification</div>'
|
||||
. '<h1 style="margin:8px 0 0;font-size:22px">' . e($ticket->reference) . '</h1>'
|
||||
. '<p style="margin:8px 0 0;color:#dbeafe">' . e($ticket->subject) . '</p>'
|
||||
. '</div><div style="padding:28px;color:#1f2937;line-height:1.65">'
|
||||
. $body
|
||||
. '<p style="margin-top:28px"><a href="' . e(route('helpdesk.tickets.show', ['ticket' => $ticket->getKey()])) . '" style="display:inline-block;background:#2563eb;color:#ffffff;text-decoration:none;padding:11px 16px;border-radius:10px">Open ticket</a></p>'
|
||||
. '</div></div></div>';
|
||||
}
|
||||
|
||||
private function log(WorkflowTemplate $template, Ticket $ticket, string $event, ?string $recipient, string $status, ?string $error = null, ?string $subject = null): void
|
||||
{
|
||||
WorkflowLog::query()->create([
|
||||
'template_id' => $template->id,
|
||||
'ticket_id' => $ticket->id,
|
||||
'event' => $event,
|
||||
'recipient' => $recipient,
|
||||
'subject' => $subject,
|
||||
'status' => $status,
|
||||
'error' => $error,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user