UserService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Services;
  3. use App\Models\UnitUser;
  4. use App\Models\User;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Illuminate\Http\UploadedFile;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Storage;
  9. class UserService
  10. {
  11. public function authUser(): ?User
  12. {
  13. return Auth::user()->load('units');
  14. }
  15. public function getAll(): Collection
  16. {
  17. return User::with(['state', 'units', 'userType'])->orderBy('name')->get();
  18. }
  19. public function getAllByUnit(): Collection
  20. {
  21. $unitId = request()->input('active_unit_id') ?? Auth::user()->units->first()?->id;
  22. if (!$unitId) {
  23. return User::with(['state', 'units', 'userType'])->whereNull('id')->get();
  24. }
  25. return User::with(['state', 'units', 'userType'])
  26. ->whereHas('units', fn($q) => $q->where('units.id', $unitId))
  27. ->orderBy('name')
  28. ->get();
  29. }
  30. public function findById(int $id): ?User
  31. {
  32. return User::with(['state', 'units', 'userType'])->find($id);
  33. }
  34. public function create(array $data): User
  35. {
  36. // Suporta unit_ids[] (múltiplas) ou unit_id (retrocompatibilidade)
  37. $unitIds = $data['unit_ids'] ?? null;
  38. if (empty($unitIds) && isset($data['unit_id'])) {
  39. $unitIds = array_filter([$data['unit_id']]);
  40. }
  41. if (empty($unitIds)) {
  42. $fallback = Auth::user()->units->first()?->id;
  43. $unitIds = $fallback ? [$fallback] : [];
  44. }
  45. unset($data['unit_ids'], $data['unit_id']);
  46. $data = $this->handleAvatar($data);
  47. $user = User::create($data);
  48. if (!empty($unitIds)) {
  49. $user->units()->sync($unitIds);
  50. }
  51. return $user->load('state', 'units');
  52. }
  53. public function update(int $id, array $data): ?User
  54. {
  55. $model = $this->findById($id);
  56. if (!$model) {
  57. return null;
  58. }
  59. // Suporta unit_ids[] (múltiplas) com fallback para unit_id (retrocompatibilidade)
  60. $hasUnitIds = array_key_exists('unit_ids', $data);
  61. $unitIds = $data['unit_ids'] ?? null;
  62. $hasUnitId = array_key_exists('unit_id', $data);
  63. $unitId = $data['unit_id'] ?? null;
  64. unset($data['unit_ids'], $data['unit_id']);
  65. $data = $this->handleAvatar($data, $model->avatar_url);
  66. $model->update($data);
  67. if ($hasUnitIds) {
  68. $model->units()->sync(array_filter($unitIds ?? []));
  69. } elseif ($hasUnitId) {
  70. $model->units()->sync($unitId ? [$unitId] : []);
  71. }
  72. return $model->fresh(['state', 'units']);
  73. }
  74. public function delete(int $id): bool
  75. {
  76. $model = $this->findById($id);
  77. if (!$model) {
  78. return false;
  79. }
  80. if ($model->avatar_url) {
  81. Storage::delete($model->avatar_url);
  82. }
  83. return $model->delete();
  84. }
  85. public function getUserTypes(): \Illuminate\Database\Eloquent\Collection
  86. {
  87. return \App\Models\UserType::orderBy('label')->get();
  88. }
  89. private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
  90. {
  91. if (!isset($data['avatar'])) {
  92. return $data;
  93. }
  94. if ($data['avatar'] instanceof UploadedFile) {
  95. if ($oldAvatarPath) {
  96. Storage::delete($oldAvatarPath);
  97. }
  98. $data['avatar_url'] = $data['avatar']->store('users/avatars');
  99. } elseif (is_null($data['avatar'])) {
  100. if ($oldAvatarPath) {
  101. Storage::delete($oldAvatarPath);
  102. }
  103. $data['avatar_url'] = null;
  104. }
  105. unset($data['avatar']);
  106. return $data;
  107. }
  108. }