51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\ProjectBoard;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\TicketStatus;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class DeleteTicketStatus
|
|
{
|
|
use ManagesTicketStatusPositions;
|
|
|
|
/**
|
|
* @return array{context_label: string, deleted_name: string}
|
|
*/
|
|
public function handle(Project $project, TicketStatus $ticketStatus): array
|
|
{
|
|
$ticketStatus->load('boardColumn');
|
|
$boardColumnId = $ticketStatus->board_column_id;
|
|
|
|
if ($this->statusContextQuery($project, $boardColumnId)->count() <= 1) {
|
|
throw ValidationException::withMessages([
|
|
'ticket_status_id' => "{$this->statusContextLabel($ticketStatus)} needs at least one ticket status.",
|
|
]);
|
|
}
|
|
|
|
$deletedName = $ticketStatus->name;
|
|
$contextLabel = $this->statusContextLabel($ticketStatus);
|
|
|
|
DB::transaction(function () use ($boardColumnId, $project, $ticketStatus): void {
|
|
$replacementStatus = $this->statusContextQuery($project, $boardColumnId)
|
|
->whereKeyNot($ticketStatus->id)
|
|
->firstOrFail();
|
|
|
|
$ticketStatus->tickets()->update([
|
|
'ticket_status_id' => $replacementStatus->id,
|
|
]);
|
|
|
|
$ticketStatus->delete();
|
|
|
|
$this->normalizeStatusPositions($project, $boardColumnId);
|
|
});
|
|
|
|
return [
|
|
'context_label' => $contextLabel,
|
|
'deleted_name' => $deletedName,
|
|
];
|
|
}
|
|
}
|