ClientService.php 4.3 KB

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