UserService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\UserTypeEnum;
  4. use App\Models\Address;
  5. use App\Models\Client;
  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\DB;
  11. use Illuminate\Support\Facades\Log;
  12. class UserService
  13. {
  14. public function __construct(private readonly MediaService $mediaService) {}
  15. public function me(): ?User
  16. {
  17. return User::with(['provider.profileMedia', 'client.profileMedia'])->find(Auth::id());
  18. }
  19. public function getAll(): Collection
  20. {
  21. return User::query()->orderBy('created_at', 'desc')->get();
  22. }
  23. public function findById(int $id): ?User
  24. {
  25. return User::find($id);
  26. }
  27. public function create(array $data): User
  28. {
  29. return User::create($data);
  30. }
  31. public function update(int $id, array $data): ?User
  32. {
  33. $model = $this->findById($id);
  34. if (! $model) {
  35. return null;
  36. }
  37. $model->update($data);
  38. return $model->fresh();
  39. }
  40. public function delete(int $id): bool
  41. {
  42. $model = $this->findById($id);
  43. if (! $model) {
  44. return false;
  45. }
  46. return $model->delete();
  47. }
  48. public function updateMe(array $data): User
  49. {
  50. try {
  51. DB::beginTransaction();
  52. $user = User::with(['client.profileMedia'])->findOrFail(Auth::id());
  53. $userFields = array_filter(
  54. array_intersect_key($data, array_flip(['name', 'email', 'phone', 'language'])),
  55. fn ($v) => $v !== null,
  56. );
  57. if (! empty($userFields)) {
  58. $user->update($userFields);
  59. }
  60. if (array_key_exists('document', $data)) {
  61. $client = $user->client ?? Client::create(['user_id' => $user->id]);
  62. $client->document = $data['document'];
  63. $client->save();
  64. }
  65. if (isset($data['avatar']) && $data['avatar'] instanceof UploadedFile) {
  66. $client = $user->client ?? Client::create(['user_id' => $user->id]);
  67. $media = $this->mediaService->replaceFile(
  68. newFile: $data['avatar'],
  69. folder: "client/avatar/{$client->id}",
  70. source: 'client',
  71. sourceId: $client->id,
  72. old: $client->profileMedia,
  73. );
  74. $client->profile_media_id = $media->id;
  75. $client->save();
  76. }
  77. $user->load('client');
  78. $registrationComplete = ! empty($user->name)
  79. && ! empty($user->client?->document)
  80. && Address::where('source', 'client')->where('source_id', $user->client->id)->exists();
  81. if ($user->registration_complete !== $registrationComplete) {
  82. $user->registration_complete = $registrationComplete;
  83. $user->save();
  84. }
  85. DB::commit();
  86. return $user->fresh(['client.profileMedia']);
  87. } catch (\Exception $e) {
  88. DB::rollBack();
  89. Log::error('Erro ao atualizar perfil.', ['error' => $e->getMessage()]);
  90. throw $e;
  91. }
  92. }
  93. public function getUserTypes(): array
  94. {
  95. return UserTypeEnum::toArray();
  96. }
  97. }