74 lines
2.3 KiB
PHP
74 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\TicketStatus;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class UpdateTicketStatusTransitionsRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user() !== null;
|
|
}
|
|
|
|
/**
|
|
* 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');
|
|
$projectTemplateId = $project->project_template_id;
|
|
$ownershipColumn = $projectTemplateId === null ? 'project_id' : 'project_template_id';
|
|
$ownershipValue = $projectTemplateId ?? $project->id;
|
|
|
|
return [
|
|
'allowed_ticket_status_ids' => ['nullable', 'array'],
|
|
'allowed_ticket_status_ids.*' => [
|
|
'integer',
|
|
Rule::exists('ticket_statuses', 'id')->where(
|
|
fn ($query) => $query->where($ownershipColumn, $ownershipValue),
|
|
),
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<int, callable(Validator): void>
|
|
*/
|
|
public function after(): array
|
|
{
|
|
return [
|
|
function (Validator $validator): void {
|
|
if ($validator->errors()->isNotEmpty()) {
|
|
return;
|
|
}
|
|
|
|
/** @var TicketStatus $ticketStatus */
|
|
$ticketStatus = $this->route('ticketStatus');
|
|
$validated = $validator->validated();
|
|
$allowedTicketStatusIds = collect(Arr::get($validated, 'allowed_ticket_status_ids', []))
|
|
->map(fn (int|string $ticketStatusId): int => (int) $ticketStatusId);
|
|
|
|
if ($allowedTicketStatusIds->contains($ticketStatus->id)) {
|
|
$validator->errors()->add(
|
|
'allowed_ticket_status_ids',
|
|
'A status cannot allow moves to itself.',
|
|
);
|
|
}
|
|
},
|
|
];
|
|
}
|
|
}
|