82 lines
2.6 KiB
PHP
82 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
|
|
use App\Models\Project;
|
|
use App\Models\Ticket;
|
|
use App\Models\TicketStatus;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
|
|
/**
|
|
* @extends Factory<Ticket>
|
|
*/
|
|
class TicketFactory extends Factory
|
|
{
|
|
/**
|
|
* Define the model's default state.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function definition(): array
|
|
{
|
|
return [
|
|
'acceptance_criteria' => fake()->paragraph(),
|
|
'assignee_user_id' => null,
|
|
'author_user_id' => User::factory(),
|
|
'blocked_comment' => null,
|
|
'description' => fake()->paragraphs(2, true),
|
|
'is_blocked' => false,
|
|
'parent_ticket_id' => null,
|
|
'sequence_number' => 1,
|
|
'story_points' => null,
|
|
'ticket_key' => 'TEST-1',
|
|
'title' => fake()->sentence(4),
|
|
'ticket_status_id' => null,
|
|
];
|
|
}
|
|
|
|
public function configure(): static
|
|
{
|
|
return $this->afterMaking(function (Ticket $ticket): void {
|
|
if (
|
|
$ticket->project_id !== null
|
|
&& $ticket->ticket_type_id !== null
|
|
&& $ticket->ticket_priority_id !== null
|
|
&& $ticket->ticket_status_id !== null
|
|
) {
|
|
return;
|
|
}
|
|
|
|
$createdProject = $ticket->project_id === null;
|
|
$project = $createdProject
|
|
? Project::factory()->create([
|
|
'key' => fake()->unique()->lexify('????'),
|
|
])
|
|
: Project::query()->findOrFail($ticket->project_id);
|
|
|
|
app(InitializeProjectTicketMetadata::class)->handle($project);
|
|
|
|
$ticket->project_id = $project->id;
|
|
$ticket->ticket_type_id ??= $project->ticketTypes()->firstOrFail()->id;
|
|
$ticket->ticket_priority_id ??= $project->ticketPriorities()->firstOrFail()->id;
|
|
$ticket->ticket_status_id ??= $project->ticketStatuses()
|
|
->whereNull('board_column_id')
|
|
->first()?->id
|
|
?? TicketStatus::factory()->for($project)->create([
|
|
'name' => 'Backlog',
|
|
'position' => 1,
|
|
])->id;
|
|
|
|
if ($createdProject || $ticket->sequence_number === null) {
|
|
$ticket->sequence_number = 1;
|
|
}
|
|
|
|
if ($createdProject || $ticket->ticket_key === null) {
|
|
$ticket->ticket_key = "{$project->key}-{$ticket->sequence_number}";
|
|
}
|
|
});
|
|
}
|
|
}
|