Files
radicall/app/Http/Requests/Admin/ShiftRequest.php
T

62 lines
1.9 KiB
PHP

<?php
namespace App\Http\Requests\Admin;
use App\Enums\ShiftDayEnum;
use App\Enums\UserEnum;
use App\Services\Shift\ShiftScheduleService;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class ShiftRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'user_id' => ['nullable', Rule::exists('users', 'id')->where('level', UserEnum::Doctor->value)],
'day_of_week' => ['nullable', 'required_without:date', 'string', Rule::in(ShiftDayEnum::values())],
'date' => ['nullable', 'required_without:day_of_week', 'date'],
'start_time' => ['required', 'date_format:H:i'],
'end_time' => ['required', 'date_format:H:i', 'different:start_time'],
'is_active' => ['required', 'boolean'],
];
}
public function prepareForValidation()
{
if ($this->filled('date')) {
$this->merge(['day_of_week' => null]);
}
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator) {
if ($validator->errors()->isNotEmpty()) {
return;
}
$scheduleService = app(ShiftScheduleService::class);
$ignoreId = $this->route('shift')?->id;
if ($scheduleService->hasConflict($this->validated(), $ignoreId)) {
$validator->errors()->add('start_time', 'این شیفت با شیفت دیگری از این پزشک تداخل دارد.');
}
});
}
}