UserDependentService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\UserDependentStatusEnum;
  4. use App\Models\UserDependent;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Illuminate\Http\UploadedFile;
  8. use Illuminate\Pagination\LengthAwarePaginator;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Storage;
  11. class UserDependentService
  12. {
  13. public function getAllByUser(int $userId): Collection
  14. {
  15. return UserDependent::where('responsible_user_id', $userId)
  16. ->orderBy('name')
  17. ->get();
  18. }
  19. /**
  20. * Dependentes aprovados para preencher select, com os campos mínimos.
  21. *
  22. * @return \Illuminate\Support\Collection<int, array<string, mixed>>
  23. */
  24. public function getApprovedByUserForSelect(int $userId): \Illuminate\Support\Collection
  25. {
  26. return UserDependent::where('responsible_user_id', $userId)
  27. ->where('status', UserDependentStatusEnum::APPROVED)
  28. ->orderBy('name')
  29. ->get(['id', 'name', 'status'])
  30. ->map(fn(UserDependent $dependent) => [
  31. 'id' => $dependent->id,
  32. 'name' => $dependent->name,
  33. 'status' => $dependent->status,
  34. ]);
  35. }
  36. public function getAllPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
  37. {
  38. $query = $this->baseQuery($filters)
  39. ->with(['responsibleUser.position', 'responsibleUser.sector'])
  40. ->orderBy('created_at', 'asc');
  41. if (!empty($filters['status'])) {
  42. $query->where('status', $filters['status']);
  43. }
  44. return $query->paginate($perPage);
  45. }
  46. /**
  47. * Status existentes no conjunto filtrado, desconsiderando o próprio filtro de status.
  48. *
  49. * @return array<int, string>
  50. */
  51. public function getStatusOptions(array $filters = []): array
  52. {
  53. return $this->baseQuery($filters)
  54. ->toBase()
  55. ->distinct()
  56. ->pluck('status')
  57. ->filter()
  58. ->values()
  59. ->all();
  60. }
  61. private function baseQuery(array $filters): Builder
  62. {
  63. $query = UserDependent::query();
  64. if (!empty($filters['search'])) {
  65. $term = '%' . mb_strtolower($filters['search']) . '%';
  66. $query->where(function ($q) use ($term) {
  67. $q->whereRaw('UNACCENT(LOWER(user_dependents.name)) LIKE UNACCENT(?)', [$term])
  68. ->orWhereRaw("TO_CHAR(user_dependents.created_at, 'DD/MM/YYYY HH24:MI:SS') LIKE ?", [$term])
  69. ->orWhereHas('responsibleUser', function ($u) use ($term) {
  70. $u->whereRaw('UNACCENT(LOWER(users.name)) LIKE UNACCENT(?)', [$term]);
  71. });
  72. });
  73. }
  74. return $query;
  75. }
  76. public function findById(int $id): ?UserDependent
  77. {
  78. return UserDependent::find($id);
  79. }
  80. public function create(array $data, ?UploadedFile $document = null): UserDependent
  81. {
  82. unset($data['document']);
  83. $data['status'] = UserDependentStatusEnum::PENDING->value;
  84. return DB::transaction(function () use ($data, $document): UserDependent {
  85. $dependent = UserDependent::create($data);
  86. if ($document) {
  87. $dependent->update([
  88. 'document_path' => $document->store($this->documentDirectory($dependent), 's3'),
  89. 'document_name' => $document->getClientOriginalName(),
  90. ]);
  91. }
  92. return $dependent->fresh();
  93. });
  94. }
  95. private function documentDirectory(UserDependent $dependent): string
  96. {
  97. return "dependents/{$dependent->responsible_user_id}/dependent/{$dependent->id}";
  98. }
  99. public function approve(int $id): ?UserDependent
  100. {
  101. $model = $this->findById($id);
  102. if (!$model) {
  103. return null;
  104. }
  105. $model->update(['status' => UserDependentStatusEnum::APPROVED]);
  106. return $model->fresh();
  107. }
  108. public function refuse(int $id): ?UserDependent
  109. {
  110. $model = $this->findById($id);
  111. if (!$model) {
  112. return null;
  113. }
  114. $model->update(['status' => UserDependentStatusEnum::REFUSED]);
  115. return $model->fresh();
  116. }
  117. public function update(int $id, array $data): ?UserDependent
  118. {
  119. $model = $this->findById($id);
  120. if (!$model) {
  121. return null;
  122. }
  123. unset($data['document'], $data['document_path'], $data['document_name']);
  124. $model->update($data);
  125. return $model->fresh();
  126. }
  127. public function delete(int $id): bool
  128. {
  129. $model = $this->findById($id);
  130. if (!$model) {
  131. return false;
  132. }
  133. if ($model->document_path) {
  134. Storage::disk('s3')->deleteDirectory($this->documentDirectory($model));
  135. }
  136. return $model->delete();
  137. }
  138. }