refactor(ticket): isolate post-commit notifications
parent
f97e935fc0
commit
a83a4d7675
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
final readonly class TicketEvent
|
||||
{
|
||||
private function __construct(
|
||||
public string $kind,
|
||||
public TicketWriteResult $result
|
||||
) {
|
||||
}
|
||||
|
||||
public static function created(TicketWriteResult $result): self
|
||||
{
|
||||
return new self('created', $result);
|
||||
}
|
||||
|
||||
public static function userReply(TicketWriteResult $result): self
|
||||
{
|
||||
return new self('user_reply', $result);
|
||||
}
|
||||
|
||||
public static function adminReply(TicketWriteResult $result): self
|
||||
{
|
||||
return new self('admin_reply', $result);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\Config;
|
||||
|
||||
/**
|
||||
* Delivers post-commit ticket notifications with channel isolation.
|
||||
*
|
||||
* Each recipient and Telegram are independent attempts. A failed transport is
|
||||
* reported and logged but never changes the already-committed ticket result or
|
||||
* prevents the remaining channels from running.
|
||||
*/
|
||||
final class TicketNotificationDispatcher
|
||||
{
|
||||
public function __construct(
|
||||
private ?TicketNotificationTransport $transport = null
|
||||
) {
|
||||
$this->transport ??= new TicketNotificationTransportAdapter();
|
||||
}
|
||||
|
||||
public function dispatch(TicketEvent $event): TicketNotificationReport
|
||||
{
|
||||
$root = $event->result->root;
|
||||
$message = $event->result->message;
|
||||
$owner = User::where('id', (int) $root->userid)->first();
|
||||
$jobs = [];
|
||||
$appName = (string) Config::get('appName');
|
||||
$ticketUrl = rtrim((string) Config::get('baseUrl'), '/')
|
||||
. '/user/ticket/' . (int) $root->id . '/view';
|
||||
|
||||
if ($event->kind === 'created' && $owner !== null && trim((string) $owner->email) !== '') {
|
||||
$jobs[] = ['mail:owner', function () use ($owner, $root, $ticketUrl, $appName): void {
|
||||
$this->transport->sendMail(
|
||||
(string) $owner->email,
|
||||
$appName . ' - 工单已创建',
|
||||
'ticket/created.tpl',
|
||||
['user' => $owner, 'ticket' => $root, 'ticket_url' => $ticketUrl],
|
||||
'ticket_created'
|
||||
);
|
||||
}];
|
||||
}
|
||||
|
||||
if (in_array($event->kind, ['created', 'user_reply'], true)) {
|
||||
foreach (User::where('is_admin', 1)->get() as $admin) {
|
||||
if (trim((string) $admin->email) === '') {
|
||||
continue;
|
||||
}
|
||||
$jobs[] = ['mail:admin:' . (int) $admin->id, function () use ($admin, $root, $appName): void {
|
||||
$this->transport->sendMail(
|
||||
(string) $admin->email,
|
||||
$appName . ' - ' . (string) $root->title,
|
||||
'news/warn.tpl',
|
||||
[
|
||||
'user' => $admin,
|
||||
'text' => '工单 #' . (int) $root->id . ' 有新的消息,请在网站管理页面查看。',
|
||||
]
|
||||
);
|
||||
}];
|
||||
}
|
||||
$jobs[] = ['telegram', function () use ($event, $root, $message): void {
|
||||
$this->transport->sendTelegram(
|
||||
$event->result->event,
|
||||
(int) $root->id,
|
||||
(string) $root->title,
|
||||
(string) $message->content
|
||||
);
|
||||
}];
|
||||
}
|
||||
|
||||
if ($event->kind === 'admin_reply' && $owner !== null && trim((string) $owner->email) !== '') {
|
||||
$jobs[] = ['mail:owner', function () use ($owner, $root, $ticketUrl, $appName): void {
|
||||
$this->transport->sendMail(
|
||||
(string) $owner->email,
|
||||
$appName . ' - 工单收到回复',
|
||||
'ticket/replied.tpl',
|
||||
['user' => $owner, 'ticket' => $root, 'ticket_url' => $ticketUrl],
|
||||
'ticket_replied'
|
||||
);
|
||||
}];
|
||||
}
|
||||
|
||||
$succeeded = 0;
|
||||
$errors = [];
|
||||
foreach ($jobs as [$channel, $job]) {
|
||||
try {
|
||||
$job();
|
||||
$succeeded += 1;
|
||||
} catch (\Throwable $exception) {
|
||||
$errors[] = ['channel' => $channel, 'error' => $exception->getMessage()];
|
||||
error_log('Ticket notification failed [' . $channel . ']: ' . $exception->getMessage());
|
||||
}
|
||||
}
|
||||
return new TicketNotificationReport(count($jobs), $succeeded, $errors);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
final readonly class TicketNotificationReport
|
||||
{
|
||||
public function __construct(
|
||||
public int $attempted,
|
||||
public int $succeeded,
|
||||
public array $errors
|
||||
) {
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
interface TicketNotificationTransport
|
||||
{
|
||||
public function sendMail(
|
||||
string $email,
|
||||
string $subject,
|
||||
string $template,
|
||||
array $data,
|
||||
?string $event = null
|
||||
): void;
|
||||
|
||||
public function sendTelegram(
|
||||
string $event,
|
||||
int $ticketId,
|
||||
string $title,
|
||||
string $content
|
||||
): void;
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
use App\Services\Mail;
|
||||
use App\Services\TicketTelegramNotifier;
|
||||
|
||||
final class TicketNotificationTransportAdapter implements TicketNotificationTransport
|
||||
{
|
||||
public function sendMail(
|
||||
string $email,
|
||||
string $subject,
|
||||
string $template,
|
||||
array $data,
|
||||
?string $event = null
|
||||
): void {
|
||||
Mail::send($email, $subject, $template, $data, [], $event);
|
||||
}
|
||||
|
||||
public function sendTelegram(
|
||||
string $event,
|
||||
int $ticketId,
|
||||
string $title,
|
||||
string $content
|
||||
): void {
|
||||
TicketTelegramNotifier::notify($event, $ticketId, $title, $content);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Models\Ticket;
|
||||
use App\Services\Ticket\TicketEvent;
|
||||
use App\Services\Ticket\TicketNotificationDispatcher;
|
||||
use App\Services\Ticket\TicketNotificationTransport;
|
||||
use App\Services\Ticket\TicketWriteResult;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
$assert = static function ($condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$GLOBALS['System_Config'] = ['baseUrl' => 'https://panel.example.test'];
|
||||
$schema = $capsule->getConnection('default')->getSchemaBuilder();
|
||||
$schema->create('user', static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('user_name');
|
||||
$table->string('email');
|
||||
$table->boolean('is_admin')->default(false);
|
||||
});
|
||||
Capsule::table('user')->insert([
|
||||
['id' => 7, 'user_name' => 'customer', 'email' => 'owner@example.test', 'is_admin' => 0],
|
||||
['id' => 9, 'user_name' => 'admin-one', 'email' => 'admin1@example.test', 'is_admin' => 1],
|
||||
['id' => 10, 'user_name' => 'admin-two', 'email' => 'admin2@example.test', 'is_admin' => 1],
|
||||
]);
|
||||
|
||||
$root = new Ticket();
|
||||
$root->id = 44;
|
||||
$root->userid = 7;
|
||||
$root->title = 'Network issue';
|
||||
$root->content = 'Cannot connect';
|
||||
$root->rootid = 0;
|
||||
$message = clone $root;
|
||||
$result = new TicketWriteResult($root, $message, '新工单', []);
|
||||
|
||||
$transport = new class implements TicketNotificationTransport {
|
||||
public array $mail = [];
|
||||
public array $telegram = [];
|
||||
|
||||
public function sendMail(string $email, string $subject, string $template, array $data, ?string $event = null): void
|
||||
{
|
||||
$this->mail[] = compact('email', 'subject', 'template', 'data', 'event');
|
||||
if ($email === 'owner@example.test') {
|
||||
throw new RuntimeException('mail outage');
|
||||
}
|
||||
}
|
||||
|
||||
public function sendTelegram(string $event, int $ticketId, string $title, string $content): void
|
||||
{
|
||||
$this->telegram[] = compact('event', 'ticketId', 'title', 'content');
|
||||
}
|
||||
};
|
||||
|
||||
$report = (new TicketNotificationDispatcher($transport))->dispatch(TicketEvent::created($result));
|
||||
$assert(count($transport->mail) === 3,
|
||||
'A failed owner confirmation blocked administrator email delivery.');
|
||||
$assert(count($transport->telegram) === 1,
|
||||
'A failed email blocked the independent Telegram channel.');
|
||||
$assert($report->attempted === 4 && $report->succeeded === 3 && count($report->errors) === 1,
|
||||
'Notification report does not describe isolated channel outcomes.');
|
||||
|
||||
echo "Ticket notification isolation checks passed.\n";
|
||||
Loading…
Reference in New Issue