UserService.php 6.1 KB

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