117 lines
3.8 KiB
PHP
117 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Actions\Tickets\Support\AcceptanceCriteriaChecklistMapper;
|
|
use App\Models\Project;
|
|
use App\Models\Ticket;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class StoreProjectTicketRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user() !== null;
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$acceptanceCriteria = $this->input('acceptance_criteria');
|
|
|
|
if ($acceptanceCriteria !== null && ! is_array($acceptanceCriteria)) {
|
|
return;
|
|
}
|
|
|
|
$this->merge([
|
|
'acceptance_criteria' => AcceptanceCriteriaChecklistMapper::normalize(
|
|
$acceptanceCriteria,
|
|
),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
/** @var Project $project */
|
|
$project = $this->route('project');
|
|
|
|
return [
|
|
'acceptance_criteria' => ['nullable', 'array'],
|
|
'acceptance_criteria.*' => ['array'],
|
|
'acceptance_criteria.*.id' => ['nullable', 'integer'],
|
|
'acceptance_criteria.*.text' => ['required', 'string'],
|
|
'acceptance_criteria.*.checked' => ['required', 'boolean'],
|
|
'acceptance_criteria.*.indent' => ['required', 'integer', 'min:0', 'max:'.AcceptanceCriteriaChecklistMapper::MAX_INDENT_LEVEL],
|
|
'assignee_user_id' => ['nullable', 'integer', Rule::exists('users', 'id')],
|
|
'description' => ['nullable', 'string'],
|
|
'parent_ticket_id' => [
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists('tickets', 'id')->where('project_id', $project->id),
|
|
],
|
|
'ticket_priority_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists('ticket_priorities', 'id')->where('project_id', $project->id),
|
|
],
|
|
'ticket_status_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists('ticket_statuses', 'id')->where('project_id', $project->id),
|
|
],
|
|
'ticket_type_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists('ticket_types', 'id')->where('project_id', $project->id),
|
|
],
|
|
'story_points' => ['nullable', 'integer', 'min:0'],
|
|
'title' => ['required', 'string', 'max:255'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<int, callable(Validator): void>
|
|
*/
|
|
public function after(): array
|
|
{
|
|
return [
|
|
function (Validator $validator): void {
|
|
/** @var array<string, mixed> $validated */
|
|
$validated = $this->safe()->all();
|
|
|
|
/** @var Project $project */
|
|
$project = $this->route('project');
|
|
|
|
$parentTicketId = Arr::get($validated, 'parent_ticket_id');
|
|
|
|
if ($parentTicketId === null) {
|
|
return;
|
|
}
|
|
|
|
$parentTicket = Ticket::query()
|
|
->where('project_id', $project->id)
|
|
->with('ticketType')
|
|
->find($parentTicketId);
|
|
|
|
if ($parentTicket !== null && ! $parentTicket->ticketType?->is_parent) {
|
|
$validator->errors()->add('parent_ticket_id', 'Only tickets with a parent-enabled type can be selected as a parent.');
|
|
}
|
|
},
|
|
];
|
|
}
|
|
}
|