UserService.php 2.9 KB

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