38 lines
1.1 KiB
PHP
38 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\ProjectTicketMetadata;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\TicketPriority;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class StoreProjectTicketPriority
|
|
{
|
|
use ReordersProjectMetadata;
|
|
|
|
/**
|
|
* @param array{name: string, position?: int|null} $validated
|
|
*/
|
|
public function handle(Project $project, array $validated): TicketPriority
|
|
{
|
|
$name = Arr::get($validated, 'name');
|
|
$position = Arr::get($validated, 'position');
|
|
|
|
return DB::transaction(function () use ($project, $name, $position): TicketPriority {
|
|
$ticketPriority = $project->ticketPriorities()->create([
|
|
'name' => $name,
|
|
'position' => $project->ticketPriorities()->count() + 1,
|
|
]);
|
|
|
|
$this->reorderModels(
|
|
$project->ticketPriorities()->whereKeyNot($ticketPriority->id)->get()->values(),
|
|
$ticketPriority,
|
|
$position ?? $project->ticketPriorities()->count(),
|
|
);
|
|
|
|
return $ticketPriority;
|
|
});
|
|
}
|
|
}
|