UserService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. Log::info('[avatar-upload] UserService::updateMe iniciado', [
  54. 'user_id' => $user->id,
  55. 'client_id' => $user->client?->id,
  56. 'has_avatar_key' => isset($data['avatar']),
  57. 'avatar_is_file' => isset($data['avatar']) && $data['avatar'] instanceof UploadedFile,
  58. 'old_media_id' => $user->client?->profile_media_id,
  59. 'old_media_path' => $user->client?->profileMedia?->path,
  60. ]);
  61. $userFields = array_filter(
  62. array_intersect_key($data, array_flip(['name', 'email', 'phone', 'language'])),
  63. fn ($v) => $v !== null,
  64. );
  65. if (! empty($userFields)) {
  66. $user->update($userFields);
  67. }
  68. if (array_key_exists('document', $data)) {
  69. $client = $user->client ?? Client::create(['user_id' => $user->id]);
  70. $client->document = $data['document'];
  71. $client->save();
  72. }
  73. if (isset($data['avatar']) && $data['avatar'] instanceof UploadedFile) {
  74. $client = $user->client ?? Client::create(['user_id' => $user->id]);
  75. Log::info('[avatar-upload] Iniciando replaceFile para avatar do cliente', [
  76. 'client_id' => $client->id,
  77. 'folder' => "client/avatar/{$client->id}",
  78. 'has_old_media'=> $client->profileMedia !== null,
  79. 'old_media_id' => $client->profileMedia?->id,
  80. 'old_media_path'=> $client->profileMedia?->path,
  81. ]);
  82. $media = $this->mediaService->replaceFile(
  83. newFile: $data['avatar'],
  84. folder: "client/avatar/{$client->id}",
  85. source: 'client',
  86. sourceId: $client->id,
  87. old: $client->profileMedia,
  88. );
  89. Log::info('[avatar-upload] replaceFile concluído — salvando profile_media_id', [
  90. 'client_id' => $client->id,
  91. 'new_media_id' => $media->id,
  92. 'new_media_path' => $media->path,
  93. ]);
  94. $client->profile_media_id = $media->id;
  95. $client->save();
  96. Log::info('[avatar-upload] Client salvo com novo profile_media_id', [
  97. 'client_id' => $client->id,
  98. 'profile_media_id'=> $client->profile_media_id,
  99. ]);
  100. } else {
  101. Log::warning('[avatar-upload] Avatar NÃO processado no service', [
  102. 'isset_avatar' => isset($data['avatar']),
  103. 'is_upload_file' => isset($data['avatar']) ? ($data['avatar'] instanceof UploadedFile) : false,
  104. 'data_keys' => array_keys($data),
  105. ]);
  106. }
  107. $user->load('client');
  108. $registrationComplete = ! empty($user->name)
  109. && ! empty($user->client?->document)
  110. && Address::where('source', 'client')->where('source_id', $user->client->id)->exists();
  111. if ($user->registration_complete !== $registrationComplete) {
  112. $user->registration_complete = $registrationComplete;
  113. $user->save();
  114. }
  115. DB::commit();
  116. return $user->fresh(['client.profileMedia']);
  117. } catch (\Exception $e) {
  118. DB::rollBack();
  119. Log::error('[avatar-upload] ERRO em UserService::updateMe', [
  120. 'error' => $e->getMessage(),
  121. 'exception' => get_class($e),
  122. 'file' => $e->getFile(),
  123. 'line' => $e->getLine(),
  124. ]);
  125. throw $e;
  126. }
  127. }
  128. public function getUserTypes(): array
  129. {
  130. return UserTypeEnum::toArray();
  131. }
  132. }