ClientService.php 5.0 KB

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