CreateOrUpdateRequest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Http\Requests\API\ApprovalFlow;
  3. use App\Models\Enums\ApprovalFlowObjectType;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Validation\Rules\Enum;
  7. class CreateOrUpdateRequest extends FormRequest
  8. {
  9. /**
  10. * Determine if the user is authorized to make this request.
  11. */
  12. public function authorize(): bool
  13. {
  14. return true;
  15. }
  16. /**
  17. * Get the validation rules that apply to the request.
  18. *
  19. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  20. */
  21. public function rules(): array
  22. {
  23. $rules = [
  24. "name" => "required|max:50",
  25. "nodes" => [
  26. 'required',
  27. 'array',
  28. 'min:1',
  29. ],
  30. "nodes.*.name" => "required",
  31. "nodes.*.approval_users" => "required|array",
  32. "nodes.*.level" => "required|numeric",
  33. ];
  34. if ($this->method() == "POST") {
  35. $rules = [
  36. ...$rules,
  37. "object_type" => [
  38. 'required',
  39. new Enum(ApprovalFlowObjectType::class),
  40. ],
  41. "object_id" => [
  42. 'required',
  43. function ($attribute, $value, $fail) {
  44. $exist = ApprovalFlowObjectType::from($this->get("object_type"))
  45. ->modelBuilderAllowed($value)
  46. ->where("company_id", Auth::user()->company_id)
  47. ->where('id', $value)
  48. ->count();
  49. if (! $exist) {
  50. $fail('Resources without permission to access.');
  51. }
  52. }
  53. ],
  54. ];
  55. }
  56. return $rules;
  57. }
  58. }