UserService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 getAll(): Collection
  16. {
  17. return User::query()->orderBy('created_at', 'desc')->get();
  18. }
  19. public function findById(int $id): ?User
  20. {
  21. return User::find($id);
  22. }
  23. public function create(array $data): User
  24. {
  25. return User::create($data);
  26. }
  27. public function update(int $id, array $data): ?User
  28. {
  29. $model = $this->findById($id);
  30. if (! $model) {
  31. return null;
  32. }
  33. $model->update($data);
  34. return $model->fresh();
  35. }
  36. public function delete(int $id): bool
  37. {
  38. $model = $this->findById($id);
  39. if (! $model) {
  40. return false;
  41. }
  42. return $model->delete();
  43. }
  44. //
  45. public function getUserTypes(): array
  46. {
  47. return UserTypeEnum::toArray();
  48. }
  49. public function me(): ?User
  50. {
  51. return User::with(['provider.profileMedia', 'client.profileMedia'])->find(Auth::id());
  52. }
  53. public function updateMe(array $data): User
  54. {
  55. try {
  56. DB::beginTransaction();
  57. $user = User::with(['client.profileMedia'])->findOrFail(Auth::id());
  58. Log::warning('[avatar-upload] UserService::updateMe iniciado', [
  59. 'user_id' => $user->id,
  60. 'client_id' => $user->client?->id,
  61. 'has_avatar_key' => isset($data['avatar']),
  62. 'avatar_is_file' => isset($data['avatar']) && $data['avatar'] instanceof UploadedFile,
  63. 'old_media_id' => $user->client?->profile_media_id,
  64. 'old_media_path' => $user->client?->profileMedia?->path,
  65. ]);
  66. $userFields = array_filter(
  67. array_intersect_key($data, array_flip(['name', 'email', 'phone', 'language'])),
  68. fn ($v) => $v !== null,
  69. );
  70. if (! empty($userFields)) {
  71. $user->update($userFields);
  72. }
  73. if (array_key_exists('document', $data)) {
  74. $client = $user->client ?? Client::create(['user_id' => $user->id]);
  75. $client->document = $data['document'];
  76. $client->save();
  77. }
  78. if (isset($data['avatar']) && $data['avatar'] instanceof UploadedFile) {
  79. $client = $user->client ?? Client::create(['user_id' => $user->id]);
  80. Log::warning('[avatar-upload] Iniciando replaceFile para avatar do cliente', [
  81. 'client_id' => $client->id,
  82. 'folder' => "client/avatar/{$client->id}",
  83. 'has_old_media' => $client->profileMedia !== null,
  84. 'old_media_id' => $client->profileMedia?->id,
  85. 'old_media_path' => $client->profileMedia?->path,
  86. ]);
  87. $media = $this->mediaService->replaceFile(
  88. newFile: $data['avatar'],
  89. folder: "client/avatar/{$client->id}",
  90. source: 'client',
  91. sourceId: $client->id,
  92. old: $client->profileMedia,
  93. );
  94. Log::warning('[avatar-upload] replaceFile concluído — salvando profile_media_id', [
  95. 'client_id' => $client->id,
  96. 'new_media_id' => $media->id,
  97. 'new_media_path' => $media->path,
  98. ]);
  99. $client->profile_media_id = $media->id;
  100. $client->save();
  101. Log::warning('[avatar-upload] Client salvo com novo profile_media_id', [
  102. 'client_id' => $client->id,
  103. 'profile_media_id' => $client->profile_media_id,
  104. ]);
  105. } else {
  106. Log::warning('[avatar-upload] Avatar NÃO processado no service', [
  107. 'isset_avatar' => isset($data['avatar']),
  108. 'is_upload_file' => isset($data['avatar']) ? ($data['avatar'] instanceof UploadedFile) : false,
  109. 'data_keys' => array_keys($data),
  110. ]);
  111. }
  112. $user->load('client');
  113. $registrationComplete = ! empty($user->name)
  114. && ! empty($user->client?->document)
  115. && Address::where('source', 'client')->where('source_id', $user->client->id)->exists();
  116. if ($user->registration_complete !== $registrationComplete) {
  117. $user->registration_complete = $registrationComplete;
  118. $user->save();
  119. }
  120. DB::commit();
  121. return $user->fresh(['client.profileMedia']);
  122. } catch (\Exception $e) {
  123. DB::rollBack();
  124. Log::error('[avatar-upload] ERRO em UserService::updateMe', [
  125. 'error' => $e->getMessage(),
  126. 'exception' => get_class($e),
  127. 'file' => $e->getFile(),
  128. 'line' => $e->getLine(),
  129. ]);
  130. throw $e;
  131. }
  132. }
  133. }