1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Http\Requests\API\Approval;
- use App\Models\ApprovalFlow;
- use App\Models\Enums\ApprovalObjectType;
- use App\Models\Enums\FileObjectType;
- use App\Models\Enums\ObjectApprovalStatus;
- use App\Models\File;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Validation\Rules\Enum;
- class CreateOrUpdateRequest 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
- {
- $rule = [];
- if ($this->method() == "POST") {
- $rule = [
- ...$rule,
- 'object_type' => [
- 'required',
- new Enum(ApprovalObjectType::class),
- ],
- 'object_id' => [
- 'required',
- function ($attribute, $value, $fail) {
- $exist = ApprovalObjectType::from($this->get("object_type"))
- ->modelBuilderAllowed($value)
- ->where("company_id", Auth::user()->company_id)
- ->where('id', $value)
- ->count();
- if (! $exist) {
- $fail('Resources without permission to access.');
- }
- }
- ],
- 'file_ids' => [
- 'array',
- function ($attribute, $value, $fail) {
- $count = File::query()
- ->where("company_id", Auth::user()->company_id)
- ->whereIn('id', $value)
- ->where("object_type", FileObjectType::CONTAINER)
- ->where("object_id", $this->get("object_id"))
- ->where("approval_status", ObjectApprovalStatus::WAIT)
- ->count();
- if ($count != count($value)) {
- $fail('Please select the file to be approved.');
- }
- }
- ],
- 'approvalFlow_id' =>[
- 'required',
- function ($attribute, $value, $fail) {
- $exist = ApprovalFlow::query()
- ->where("company_id", Auth::user()->company_id)
- ->where('id', $value)
- ->where('status', 1)
- ->count();
- if (! $exist) {
- $fail('Resources without permission to access.');
- }
- }
- ]
- ];
- }
- return $rule;
- }
- }
|