CreateOrUpdateRequest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Http\Requests\API\Task;
  3. use App\Http\Requests\CustomFieldRuleHelper;
  4. use App\Http\Requests\NamingRuleHelper;
  5. use App\Http\Requests\RuleHelper;
  6. use App\Models\Enums\TaskACL;
  7. use App\Models\User;
  8. use Illuminate\Foundation\Http\FormRequest;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Validation\Rule;
  11. use Illuminate\Validation\Rules\Enum;
  12. class CreateOrUpdateRequest extends FormRequest
  13. {
  14. use RuleHelper, CustomFieldRuleHelper, NamingRuleHelper;
  15. /**
  16. * Determine if the user is authorized to make this request.
  17. */
  18. public function authorize(): bool
  19. {
  20. return true;
  21. }
  22. /**
  23. * Get the validation rules that apply to the request.
  24. *
  25. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  26. */
  27. public function rules(): array
  28. {
  29. $rules = [
  30. 'project_id' => [
  31. 'required',
  32. Rule::exists('projects', 'id')->where($this->userCompanyWhere()),
  33. ],
  34. 'requirement_id' => [
  35. 'nullable',
  36. Rule::exists('requirements', 'id')->where($this->userCompanyWhere()),
  37. ],
  38. 'naming_rule_id' => [
  39. Rule::when($this->get('naming_rule_id') > 0, [
  40. Rule::exists('naming_rules', 'id')->whereIn('company_id', [
  41. 0, Auth::user()->company_id,
  42. ]),
  43. ])
  44. ],
  45. 'assign' => [
  46. 'nullable',
  47. Rule::exists('users', 'id')->where($this->userCompanyWhere()),
  48. ],
  49. 'name' => 'required|max:255',
  50. 'parent_id' => [
  51. Rule::when($this->get('parent_id') > 0, [
  52. Rule::exists('tasks', 'id')->where($this->userCompanyWhere())->where("parent_id", 0),
  53. ])
  54. ],
  55. 'begin' => 'date',
  56. 'end' => 'date',
  57. 'acl' => [
  58. new Enum(TaskACL::class),
  59. ],
  60. 'whitelist' => [
  61. 'array',
  62. 'nullable',
  63. function ($attribute, $value, $fail) {
  64. $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  65. if ($userCount != count($value)) {
  66. $fail('The selected user is invalid.');
  67. }
  68. }
  69. ],
  70. 'mailto' => [
  71. 'array',
  72. 'nullable',
  73. function ($attribute, $value, $fail) {
  74. $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  75. if ($userCount != count($value)) {
  76. $fail('The selected user is invalid.');
  77. }
  78. }
  79. ],
  80. ];
  81. $taskRules = $this->customFieldRuleByGroup("task", ['doc_type', 'task_type', 'doc_stage', "state", "suitability"]);
  82. if ($this->has("naming_rule_id") && $this->get("naming_rule_id") > 0) {
  83. $this->namingRuleCheck($this->get("naming_rule_id"));
  84. $namingRules = $this->customFieldRuleByGroup($this->get("naming_rule_id"));
  85. $rules = [...$rules, ... $namingRules];
  86. }
  87. return [...$rules, ...$taskRules];
  88. }
  89. }