ClientService.php 3.7 KB

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