ClientService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\UserTypeEnum;
  4. use App\Models\Address;
  5. use App\Models\City;
  6. use App\Models\Client;
  7. use App\Models\State;
  8. use App\Models\User;
  9. use App\Services\Pagarme\PagarmeCustomerService;
  10. use Illuminate\Database\Eloquent\Collection;
  11. use Illuminate\Http\UploadedFile;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. class ClientService
  15. {
  16. public function __construct(
  17. private readonly AuthService $authService,
  18. private readonly PagarmeCustomerService $pagarmeCustomerService,
  19. private readonly MediaService $mediaService,
  20. ) {}
  21. public function getAll(): Collection
  22. {
  23. return Client::with(['user', 'profileMedia'])
  24. ->join('users', 'clients.user_id', '=', 'users.id')
  25. ->orderBy('users.name')
  26. ->select('clients.*')
  27. ->get();
  28. }
  29. public function findById(int $id): ?Client
  30. {
  31. return Client::with(['user', 'profileMedia'])->find($id);
  32. }
  33. public function create(array $data): Client
  34. {
  35. return Client::create($data);
  36. }
  37. public function update(array $data, int $id)
  38. {
  39. $client = Client::with(['profileMedia'])->findOrFail($id);
  40. if (data_get($data, 'avatar') !== null && data_get($data, 'avatar') instanceof UploadedFile) {
  41. $media = $this->mediaService->replaceFile(
  42. newFile: data_get($data, 'avatar'),
  43. folder: "client/avatar/{$client->id}",
  44. source: 'client',
  45. sourceId: $client->id,
  46. old: $client->profileMedia,
  47. );
  48. $data['profile_media_id'] = $media->id;
  49. unset($data['avatar']);
  50. }
  51. $client->update($data);
  52. return $client->fresh(['user', 'profileMedia']);
  53. }
  54. public function delete(int $id): bool
  55. {
  56. $client = Client::findOrFail($id);
  57. return $client->delete();
  58. }
  59. //
  60. public function register(array $data): ?array
  61. {
  62. try {
  63. DB::beginTransaction();
  64. $user = User::where('email', data_get($data, 'email'))
  65. ->where('type', UserTypeEnum::CLIENT->value)
  66. ->where('code', data_get($data, 'code'))
  67. ->first();
  68. if (! $user) {
  69. throw new \Exception(__('messages.user_not_found_or_code_not_validated'));
  70. }
  71. if (! empty(data_get($data, 'name'))) {
  72. $user->name = data_get($data, 'name');
  73. }
  74. $user->phone = data_get($data, 'phone');
  75. $user->save();
  76. $client = Client::firstOrCreate(
  77. ['user_id' => $user->id], ['document' => data_get($data, 'document')]
  78. );
  79. if (! empty(data_get($data, 'document'))) {
  80. $client->document = data_get($data, 'document');
  81. $client->save();
  82. }
  83. $client->refresh();
  84. if (data_get($data, 'avatar') !== null && data_get($data, 'avatar') instanceof UploadedFile) {
  85. $client->load('profileMedia');
  86. $media = $this->mediaService->replaceFile(
  87. newFile: data_get($data, 'avatar'),
  88. folder: "client/avatar/{$client->id}",
  89. source: 'client',
  90. sourceId: $client->id,
  91. old: $client->profileMedia,
  92. );
  93. $client->profile_media_id = $media->id;
  94. $client->save();
  95. }
  96. $addressData = [
  97. 'zip_code' => data_get($data, 'zip_code'),
  98. 'address' => data_get($data, 'address'),
  99. 'number' => data_get($data, 'number'),
  100. 'district' => data_get($data, 'district'),
  101. 'has_complement' => data_get($data, 'has_complement', false),
  102. 'complement' => data_get($data, 'complement'),
  103. 'nickname' => data_get($data, 'nickname'),
  104. 'instructions' => data_get($data, 'instructions'),
  105. 'address_type' => data_get($data, 'address_type', 'home'),
  106. 'is_primary' => true,
  107. 'latitude' => data_get($data, 'latitude'),
  108. 'longitude' => data_get($data, 'longitude'),
  109. ];
  110. if (! empty(data_get($data, 'state')) && ! empty(data_get($data, 'city'))) {
  111. $state = State::where('code', data_get($data, 'state'))->first();
  112. if ($state) {
  113. $city = City::where('name', data_get($data, 'city'))->where('state_id', $state->id)->first();
  114. $addressData['state_id'] = $state->id;
  115. $addressData['city_id'] = $city?->id;
  116. }
  117. }
  118. Address::updateOrCreate(
  119. ['source' => 'client', 'source_id' => $client->id],
  120. $addressData
  121. );
  122. $this->pagarmeCustomerService->createCustomerForClient($client, $data);
  123. $registrationComplete = ! empty($user->name)
  124. && ! empty($client->document)
  125. && Address::where('source', 'client')->where('source_id', $client->id)->exists();
  126. if ($registrationComplete !== $user->registration_complete) {
  127. $user->registration_complete = $registrationComplete;
  128. $user->save();
  129. }
  130. $result = $this->authService->loginWithEmail(
  131. email: $user->email,
  132. code: $user->code,
  133. type: UserTypeEnum::CLIENT->value,
  134. );
  135. DB::commit();
  136. return $result;
  137. } catch (\Exception $e) {
  138. DB::rollBack();
  139. Log::error('Erro ao cadastrar cliente: '.$e->getMessage());
  140. throw $e;
  141. }
  142. }
  143. }