Files
backend/laravel/app/Http/Requests/Call/UpdateRequest.php

81 lines
2.3 KiB
PHP

<?php
namespace App\Http\Requests\Call;
use App\Models\Category;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
/**
* @property mixed $call
* @property mixed $category_id
* @property mixed $subcategory_id
* @OA\Schema(
* * schema="UpdateCallDataRequest",
* * title="Update Call Data",
* * description="OperatorActivity",
* * @OA\Property( property="category_id", type="integer", format="int64", title="category_id", description="category id of caller request"),
* * @OA\Property( property="subcategory_id", type="integer", format="int64", title="subcategory_id", description="subcategory id of caller request"),
* * @OA\Property( property="description", type="string", title="description", description="operator description"),
* * ),
*/
class UpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
$operator_telephone_id = auth()->user()->telephone_id;
if ($operator_telephone_id == $this->call->telephone_id) {
return true;
}
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'category_id' => 'required|integer',
'subcategory_id' => 'required|integer',
'description' => 'string',
];
}
public function after(): array
{
return [
function (Validator $validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add(
'category',
__('messages.combination_of_category_and_subcategory_does_not_exist')
);
}
}
];
}
private function somethingElseIsInvalid(): bool
{
$category_id = $this->category_id;
$subcategory_id = $this->subcategory_id;
if ($category_id && $subcategory_id) {
return !Category::query()->where([
['category_id', $category_id],
['subcategory_id', $subcategory_id]
])->exists();
}
return false;
}
}