ClientService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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($data['email'], function ($q) use ($data) {
  44. $q->where('email', $data['email']);
  45. })
  46. ->when($data['phone'], function ($q) use ($data) {
  47. $q->where('phone', $data['phone']);
  48. })
  49. ->where('type', 'CLIENT')
  50. ->where('code', $data['code'])
  51. ->where('validated_code', false)
  52. ->first();
  53. if(!$user) {
  54. throw new \Exception(__('messages.user_not_found_or_code_not_validated'));
  55. }
  56. $user->name = $data['name'];
  57. $user->save();
  58. $client = new Client();
  59. $client->user_id = $user->id;
  60. $client->document = $data['document'];
  61. $client->save();
  62. $client->refresh();
  63. $address = new Address();
  64. $address->source = 'client';
  65. $address->source_id = $client->id;
  66. $address->zip_code = $data['zip_code'] ?? null;
  67. $address->address = $data['address'] ?? null;
  68. $address->has_complement = $data['has_complement'] ?? null;
  69. $address->nickname = $data['nickname'] ?? null;
  70. $address->instructions = $data['instructions'] ?? null;
  71. $address->address_type = $data['address_type'] ?? null;
  72. $state = State::where('code', $data['state'])->first();
  73. $city = City::where('name', $data['city'])->where('state_id', $state->id)->first();
  74. $address->state_id = $state->id;
  75. $address->city_id = $city->id;
  76. $address->save();
  77. $result = $this->authService->loginWithEmail(
  78. email: $user->email,
  79. code: $user->code,
  80. );
  81. DB::commit();
  82. return $result;
  83. } catch (\Exception $e) {
  84. DB::rollBack();
  85. Log::error("Error registering client: " . $e->getMessage());
  86. throw $e;
  87. }
  88. }
  89. }