Implement ticket links and blocker state
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Ticket;
|
||||
use App\Models\TicketLink;
|
||||
use App\Models\TicketLinkType;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<TicketLink>
|
||||
*/
|
||||
class TicketLinkFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'source_ticket_id' => null,
|
||||
'target_ticket_id' => null,
|
||||
'ticket_link_type_id' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function configure(): static
|
||||
{
|
||||
return $this->afterMaking(function (TicketLink $ticketLink): void {
|
||||
$sourceTicket = $ticketLink->source_ticket_id === null
|
||||
? Ticket::factory()->create()
|
||||
: Ticket::query()->findOrFail($ticketLink->source_ticket_id);
|
||||
|
||||
$targetTicket = $ticketLink->target_ticket_id === null
|
||||
? Ticket::factory()->create([
|
||||
'project_id' => $sourceTicket->project_id,
|
||||
'ticket_priority_id' => $sourceTicket->ticket_priority_id,
|
||||
'ticket_type_id' => $sourceTicket->ticket_type_id,
|
||||
'sequence_number' => $sourceTicket->sequence_number + 1,
|
||||
'ticket_key' => "{$sourceTicket->project->key}-".($sourceTicket->sequence_number + 1),
|
||||
])
|
||||
: Ticket::query()->findOrFail($ticketLink->target_ticket_id);
|
||||
|
||||
if ($sourceTicket->id === $targetTicket->id) {
|
||||
$targetTicket = Ticket::factory()->create([
|
||||
'project_id' => $sourceTicket->project_id,
|
||||
'ticket_priority_id' => $sourceTicket->ticket_priority_id,
|
||||
'ticket_type_id' => $sourceTicket->ticket_type_id,
|
||||
'sequence_number' => $sourceTicket->sequence_number + 1,
|
||||
'ticket_key' => "{$sourceTicket->project->key}-".($sourceTicket->sequence_number + 1),
|
||||
]);
|
||||
}
|
||||
|
||||
$ticketLinkType = $ticketLink->ticket_link_type_id === null
|
||||
? TicketLinkType::query()
|
||||
->where('project_id', $sourceTicket->project_id)
|
||||
->orderBy('position')
|
||||
->first()
|
||||
?? TicketLinkType::factory()->for($sourceTicket->project)->create([
|
||||
'label' => 'Relates to',
|
||||
'position' => 1,
|
||||
])
|
||||
: TicketLinkType::query()->findOrFail($ticketLink->ticket_link_type_id);
|
||||
|
||||
$sortedIds = [$sourceTicket->id, $targetTicket->id];
|
||||
sort($sortedIds);
|
||||
|
||||
$ticketLink->source_ticket_id = $sortedIds[0];
|
||||
$ticketLink->target_ticket_id = $sortedIds[1];
|
||||
$ticketLink->ticket_link_type_id = $ticketLinkType->id;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user