ClientService.php 5.8 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 (isset($data['avatar']) && $data['avatar'] instanceof UploadedFile) {
  41. $media = $this->mediaService->replaceFile(
  42. newFile: $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['email']), function ($q) use ($data) {
  65. $q->where('email', $data['email']);
  66. })
  67. ->when(! empty($data['phone']), function ($q) use ($data) {
  68. $q->where('phone', $data['phone']);
  69. })
  70. ->where('type', 'CLIENT')
  71. ->where('code', $data['code'])
  72. ->first();
  73. if (! $user) {
  74. throw new \Exception(__('messages.user_not_found_or_code_not_validated'));
  75. }
  76. if (! empty($data['name'])) {
  77. $user->name = $data['name'];
  78. $user->save();
  79. }
  80. $client = Client::firstOrCreate(
  81. ['user_id' => $user->id], ['document' => $data['document'] ?? null]
  82. );
  83. if (! empty($data['document'])) {
  84. $client->document = $data['document'];
  85. $client->save();
  86. }
  87. $client->refresh();
  88. if (isset($data['avatar']) && $data['avatar'] instanceof UploadedFile) {
  89. $client->load('profileMedia');
  90. $media = $this->mediaService->replaceFile(
  91. newFile: $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['zip_code'] ?? null,
  102. 'address' => $data['address'] ?? null,
  103. 'number' => $data['number'] ?? null,
  104. 'district' => $data['district'] ?? null,
  105. 'has_complement' => $data['has_complement'] ?? false,
  106. 'complement' => $data['complement'] ?? null,
  107. 'nickname' => $data['nickname'] ?? null,
  108. 'instructions' => $data['instructions'] ?? null,
  109. 'address_type' => $data['address_type'] ?? 'home',
  110. 'is_primary' => true,
  111. 'latitude' => $data['latitude'] ?? null,
  112. 'longitude' => $data['longitude'] ?? null,
  113. ];
  114. if (! empty($data['state']) && ! empty($data['city'])) {
  115. $state = State::where('code', $data['state'])->first();
  116. if ($state) {
  117. $city = City::where('name', $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('Error registering client: '.$e->getMessage());
  144. throw $e;
  145. }
  146. }
  147. }