CreateOrUpdateRequest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Http\Requests\API\Approval;
  3. use App\Models\ApprovalFlow;
  4. use App\Models\Enums\ApprovalObjectType;
  5. use App\Models\Enums\FileObjectType;
  6. use App\Models\Enums\ObjectApprovalStatus;
  7. use App\Models\File;
  8. use Illuminate\Foundation\Http\FormRequest;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Validation\Rules\Enum;
  11. class CreateOrUpdateRequest extends FormRequest
  12. {
  13. /**
  14. * Determine if the user is authorized to make this request.
  15. */
  16. public function authorize(): bool
  17. {
  18. return true;
  19. }
  20. /**
  21. * Get the validation rules that apply to the request.
  22. *
  23. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  24. */
  25. public function rules(): array
  26. {
  27. $rule = [];
  28. if ($this->method() == "POST") {
  29. $rule = [
  30. ...$rule,
  31. 'object_type' => [
  32. 'required',
  33. new Enum(ApprovalObjectType::class),
  34. ],
  35. 'object_id' => [
  36. 'required',
  37. function ($attribute, $value, $fail) {
  38. $exist = ApprovalObjectType::from($this->get("object_type"))
  39. ->modelBuilderAllowed($value)
  40. ->where('id', $value)
  41. ->count();
  42. if (! $exist) {
  43. $fail('Resources without permission to access.');
  44. }
  45. }
  46. ],
  47. 'file_ids' => [
  48. 'array',
  49. function ($attribute, $value, $fail) {
  50. $count = File::query()
  51. ->whereIn('id', $value)
  52. ->where("object_type", FileObjectType::CONTAINER)
  53. ->where("object_id", $this->get("object_id"))
  54. ->whereIn("approval_status", [ObjectApprovalStatus::WAIT, ObjectApprovalStatus::REJECTED])
  55. ->count();
  56. if ($count != count($value)) {
  57. $fail('Please select the file to be approved.');
  58. }
  59. }
  60. ],
  61. 'approvalFlow_id' =>[
  62. 'required',
  63. function ($attribute, $value, $fail) {
  64. $exist = ApprovalFlow::query()
  65. ->where('id', $value)
  66. ->where('status', 1)
  67. ->count();
  68. if (! $exist) {
  69. $fail('Resources without permission to access.');
  70. }
  71. }
  72. ]
  73. ];
  74. }
  75. return $rule;
  76. }
  77. }