CreateOrUpdateRequest.php 2.0 KB

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