63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Tickets;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\Ticket;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CreateProjectTicket
|
|
{
|
|
public function __construct(
|
|
private readonly CreateTicketWorklogEntry $createTicketWorklogEntry,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{
|
|
* acceptance_criteria: string|null,
|
|
* description: string|null,
|
|
* ticket_priority_id: int,
|
|
* ticket_type_id: int,
|
|
* title: string
|
|
* } $attributes
|
|
*/
|
|
public function handle(Project $project, array $attributes, ?User $actor = null): Ticket
|
|
{
|
|
return DB::transaction(function () use ($actor, $attributes, $project): Ticket {
|
|
$nextSequenceNumber = (int) Ticket::query()
|
|
->where('project_id', $project->id)
|
|
->max('sequence_number') + 1;
|
|
|
|
$ticket = Ticket::query()->create([
|
|
'acceptance_criteria' => $attributes['acceptance_criteria'],
|
|
'board_column_id' => null,
|
|
'description' => $attributes['description'],
|
|
'project_id' => $project->id,
|
|
'sequence_number' => $nextSequenceNumber,
|
|
'ticket_key' => "{$project->key}-{$nextSequenceNumber}",
|
|
'ticket_priority_id' => $attributes['ticket_priority_id'],
|
|
'ticket_type_id' => $attributes['ticket_type_id'],
|
|
'title' => $attributes['title'],
|
|
]);
|
|
|
|
if ($actor !== null) {
|
|
$this->createTicketWorklogEntry->handle(
|
|
$ticket,
|
|
$actor,
|
|
'created',
|
|
'Ticket was created in Backlog.',
|
|
[
|
|
'from' => null,
|
|
'to' => [
|
|
'label' => 'Backlog',
|
|
],
|
|
],
|
|
);
|
|
}
|
|
|
|
return $ticket;
|
|
});
|
|
}
|
|
}
|