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