CreateOrUpdateRequest.php 2.1 KB

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