ClientService.php 3.5 KB

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