Files
WMS/Modules/Helpdesk/Http/Controllers/TicketCommentDeleteController.php
2026-09-21 10:14:57 +01:00

77 lines
2.5 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace Modules\Helpdesk\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\Helpdesk\Models\Comment;
use Modules\Helpdesk\Models\Ticket;
final class TicketCommentDeleteController extends Controller
{
/**
* Tombstone a ticket response. Only MPBA/platform super administrators may delete responses.
*/
public function __invoke(Request $request, Ticket $ticket, Comment $comment): RedirectResponse
{
$user = $request->user();
abort_unless($user !== null && method_exists($user, 'isMpbaPlatformAdmin') && $user->isMpbaPlatformAdmin(), 403);
abort_unless((int) $comment->ticket_id === (int) $ticket->id, 404);
DB::transaction(function () use ($comment, $ticket, $user): void {
$this->deleteComment($comment, (int) $user->id);
$this->touchTicket($ticket);
});
return redirect()
->route('helpdesk.tickets.show', $ticket)
->with('ticket-comment-deleted', 'Response deleted.');
}
private function deleteComment(Comment $comment, int $userId): void
{
$updates = $this->tombstoneUpdates($comment, $userId);
if ($updates === []) {
$comment->delete();
return;
}
DB::table('helpdesk_comments')->where('id', $comment->id)->update($updates);
}
/** @return array<string, mixed> */
private function tombstoneUpdates(Comment $comment, int $userId): array
{
$now = now();
$candidates = [
'tombstoned_at' => $now,
'deleted_by' => $userId,
'delete_reason' => 'Deleted by MPBA platform administrator.',
'sync_sequence' => ((int) ($comment->sync_sequence ?? 0)) + 1,
'sync_hash' => hash('sha256', implode('|', [(string) ($comment->guid ?? $comment->id), 'deleted', $now->toISOString()])),
'updated_at' => $now,
];
return array_filter(
$candidates,
static fn (mixed $value, string $column): bool => Schema::hasColumn('helpdesk_comments', $column),
ARRAY_FILTER_USE_BOTH,
);
}
private function touchTicket(Ticket $ticket): void
{
if (Schema::hasColumn('helpdesk_tickets', 'last_activity_at')) {
$ticket->forceFill(['last_activity_at' => now()])->save();
}
}
}