Implement parent child ticket hierarchy
This commit is contained in:
@@ -3,10 +3,13 @@
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\TicketType;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
class UpdateProjectTicketRequest extends FormRequest
|
||||
{
|
||||
@@ -40,6 +43,11 @@ class UpdateProjectTicketRequest extends FormRequest
|
||||
],
|
||||
'field_values.*.value_number' => ['nullable', 'numeric'],
|
||||
'field_values.*.value_user_id' => ['nullable', 'integer', Rule::exists('users', 'id')],
|
||||
'parent_ticket_id' => [
|
||||
'nullable',
|
||||
'integer',
|
||||
Rule::exists('tickets', 'id')->where('project_id', $project->id),
|
||||
],
|
||||
'ticket_priority_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
@@ -54,10 +62,69 @@ class UpdateProjectTicketRequest extends FormRequest
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, callable(Validator): void>
|
||||
*/
|
||||
public function after(): array
|
||||
{
|
||||
return [
|
||||
function (Validator $validator): void {
|
||||
/** @var Project $project */
|
||||
$project = $this->route('project');
|
||||
|
||||
/** @var Ticket $ticket */
|
||||
$ticket = $this->route('ticket');
|
||||
|
||||
$parentTicketId = $this->input('parent_ticket_id');
|
||||
$parentTicketId = is_numeric($parentTicketId) ? (int) $parentTicketId : null;
|
||||
|
||||
if ($parentTicketId !== null) {
|
||||
if ($parentTicketId === $ticket->id) {
|
||||
$validator->errors()->add('parent_ticket_id', 'A ticket cannot be its own parent.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$parentTicket = Ticket::query()
|
||||
->where('project_id', $project->id)
|
||||
->with('ticketType')
|
||||
->find($parentTicketId);
|
||||
|
||||
if ($parentTicket !== null && $parentTicket->ticketType?->system_key !== 'parent') {
|
||||
$validator->errors()->add('parent_ticket_id', 'Only tickets of type Parent can be selected as a parent.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($parentTicket !== null && $this->createsCycle($ticket, $parentTicket)) {
|
||||
$validator->errors()->add('parent_ticket_id', 'A ticket cannot be assigned to one of its descendants.');
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$requestedTicketTypeId = $this->input('ticket_type_id');
|
||||
$requestedTicketType = is_numeric($requestedTicketTypeId)
|
||||
? TicketType::query()
|
||||
->where('project_id', $project->id)
|
||||
->find((int) $requestedTicketTypeId)
|
||||
: null;
|
||||
|
||||
if (
|
||||
$requestedTicketType?->system_key !== 'parent'
|
||||
&& $ticket->childTickets()->exists()
|
||||
) {
|
||||
$validator->errors()->add('ticket_type_id', 'Tickets with child tickets must keep a Parent type.');
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* acceptance_criteria: string|null,
|
||||
* description: string|null,
|
||||
* parent_ticket_id: int|null,
|
||||
* ticket_priority_id: int,
|
||||
* ticket_type_id: int,
|
||||
* title: string
|
||||
@@ -68,18 +135,27 @@ class UpdateProjectTicketRequest extends FormRequest
|
||||
/** @var array{
|
||||
* acceptance_criteria: string|null,
|
||||
* description: string|null,
|
||||
* parent_ticket_id: int|null,
|
||||
* ticket_priority_id: int,
|
||||
* ticket_type_id: int,
|
||||
* title: string
|
||||
* } $attributes
|
||||
*/
|
||||
$attributes = Arr::only($this->validated(), [
|
||||
'acceptance_criteria',
|
||||
'description',
|
||||
'ticket_priority_id',
|
||||
'ticket_type_id',
|
||||
'title',
|
||||
]);
|
||||
$attributes = array_merge(
|
||||
[
|
||||
'acceptance_criteria' => null,
|
||||
'description' => null,
|
||||
'parent_ticket_id' => null,
|
||||
],
|
||||
Arr::only($this->validated(), [
|
||||
'acceptance_criteria',
|
||||
'description',
|
||||
'parent_ticket_id',
|
||||
'ticket_priority_id',
|
||||
'ticket_type_id',
|
||||
'title',
|
||||
]),
|
||||
);
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
@@ -103,4 +179,23 @@ class UpdateProjectTicketRequest extends FormRequest
|
||||
|
||||
return $fieldValues;
|
||||
}
|
||||
|
||||
private function createsCycle(Ticket $ticket, Ticket $candidateParent): bool
|
||||
{
|
||||
$currentParent = $candidateParent;
|
||||
|
||||
while ($currentParent !== null) {
|
||||
if ($currentParent->id === $ticket->id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($currentParent->parent_ticket_id === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$currentParent = Ticket::query()->find($currentParent->parent_ticket_id);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user