ClientService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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::when(! empty(data_get($data, 'email')), function ($q) use ($data) {
  65. $q->where('email', data_get($data, 'email'));
  66. })
  67. ->when(! empty(data_get($data, 'phone')), function ($q) use ($data) {
  68. $q->where('phone', data_get($data, 'phone'));
  69. })
  70. ->where('type', 'CLIENT')
  71. ->where('code', data_get($data, 'code'))
  72. ->first();
  73. if (! $user) {
  74. throw new \Exception(__('messages.user_not_found_or_code_not_validated'));
  75. }
  76. if (! empty(data_get($data, 'name'))) {
  77. $user->name = data_get($data, 'name');
  78. $user->save();
  79. }
  80. $client = Client::firstOrCreate(
  81. ['user_id' => $user->id], ['document' => data_get($data, 'document')]
  82. );
  83. if (! empty(data_get($data, 'document'))) {
  84. $client->document = data_get($data, 'document');
  85. $client->save();
  86. }
  87. $client->refresh();
  88. if (data_get($data, 'avatar') !== null && data_get($data, 'avatar') instanceof UploadedFile) {
  89. $client->load('profileMedia');
  90. $media = $this->mediaService->replaceFile(
  91. newFile: data_get($data, 'avatar'),
  92. folder: "client/avatar/{$client->id}",
  93. source: 'client',
  94. sourceId: $client->id,
  95. old: $client->profileMedia,
  96. );
  97. $client->profile_media_id = $media->id;
  98. $client->save();
  99. }
  100. $addressData = [
  101. 'zip_code' => data_get($data, 'zip_code'),
  102. 'address' => data_get($data, 'address'),
  103. 'number' => data_get($data, 'number'),
  104. 'district' => data_get($data, 'district'),
  105. 'has_complement' => data_get($data, 'has_complement', false),
  106. 'complement' => data_get($data, 'complement'),
  107. 'nickname' => data_get($data, 'nickname'),
  108. 'instructions' => data_get($data, 'instructions'),
  109. 'address_type' => data_get($data, 'address_type', 'home'),
  110. 'is_primary' => true,
  111. 'latitude' => data_get($data, 'latitude'),
  112. 'longitude' => data_get($data, 'longitude'),
  113. ];
  114. if (! empty(data_get($data, 'state')) && ! empty(data_get($data, 'city'))) {
  115. $state = State::where('code', data_get($data, 'state'))->first();
  116. if ($state) {
  117. $city = City::where('name', data_get($data, 'city'))->where('state_id', $state->id)->first();
  118. $addressData['state_id'] = $state->id;
  119. $addressData['city_id'] = $city?->id;
  120. }
  121. }
  122. Address::updateOrCreate(
  123. ['source' => 'client', 'source_id' => $client->id],
  124. $addressData
  125. );
  126. $this->pagarmeCustomerService->createCustomerForClient($client, $data);
  127. $registrationComplete = ! empty($user->name)
  128. && ! empty($client->document)
  129. && Address::where('source', 'client')->where('source_id', $client->id)->exists();
  130. if ($registrationComplete !== $user->registration_complete) {
  131. $user->registration_complete = $registrationComplete;
  132. $user->save();
  133. }
  134. $result = $this->authService->loginWithEmail(
  135. email: $user->email,
  136. code: $user->code,
  137. type: UserTypeEnum::CLIENT->value,
  138. );
  139. DB::commit();
  140. return $result;
  141. } catch (\Exception $e) {
  142. DB::rollBack();
  143. Log::error('Erro ao cadastrar cliente: '.$e->getMessage());
  144. throw $e;
  145. }
  146. }
  147. }