79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
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 MoveProjectTicketRequest 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');
|
|
|
|
return [
|
|
'ticket_status_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists('ticket_statuses', 'id')->where('project_id', $project->id),
|
|
],
|
|
'redirect_to' => ['required', 'string', Rule::in(['backlog', 'board'])],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<int, callable(Validator): void>
|
|
*/
|
|
public function after(): array
|
|
{
|
|
return [
|
|
function (Validator $validator): void {
|
|
if ($validator->errors()->isNotEmpty()) {
|
|
return;
|
|
}
|
|
|
|
/** @var Ticket $ticket */
|
|
$ticket = $this->route('ticket');
|
|
$validated = $validator->validated();
|
|
$ticketStatusId = (int) Arr::get($validated, 'ticket_status_id');
|
|
|
|
if ($ticket->ticket_status_id === $ticketStatusId) {
|
|
return;
|
|
}
|
|
|
|
$ticket->loadMissing('ticketStatus');
|
|
|
|
$isAllowed = $ticket->ticketStatus?->allowedTargetStatuses()
|
|
->whereKey($ticketStatusId)
|
|
->exists() ?? false;
|
|
|
|
if (! $isAllowed) {
|
|
$validator->errors()->add(
|
|
'ticket_status_id',
|
|
'Choose a status this ticket is allowed to move to.',
|
|
);
|
|
}
|
|
},
|
|
];
|
|
}
|
|
}
|