ProviderService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Address;
  4. use App\Models\City;
  5. use App\Models\Provider;
  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 ProviderService
  12. {
  13. public function __construct(
  14. private readonly AuthService $authService,
  15. ) {}
  16. public function getAll(): Collection
  17. {
  18. $providers = Provider::query()
  19. ->with(['user', 'profileMedia'])
  20. ->orderBy("created_at", "desc")
  21. ->get();
  22. return $providers;
  23. }
  24. public function findById(int $id): ?Provider
  25. {
  26. return Provider::with(['user', 'profileMedia'])->find($id);
  27. }
  28. public function create(array $data): Provider
  29. {
  30. return Provider::create($data);
  31. }
  32. public function update(int $id, array $data): ?Provider
  33. {
  34. $model = $this->findById($id);
  35. if (!$model) {
  36. return null;
  37. }
  38. $model->update($data);
  39. return $model->fresh(['user', 'profileMedia']);
  40. }
  41. public function delete(int $id): bool
  42. {
  43. $model = $this->findById($id);
  44. if (!$model) {
  45. return false;
  46. }
  47. return $model->delete();
  48. }
  49. public function register(array $data): ?array
  50. {
  51. try {
  52. DB::beginTransaction();
  53. $user = User::when($data['email'], function ($q) use ($data) {
  54. $q->where('email', $data['email']);
  55. })
  56. ->when($data['phone'], function ($q) use ($data) {
  57. $q->where('phone', $data['phone']);
  58. })
  59. ->where('type', 'PROVIDER')
  60. ->where('code', $data['code'])
  61. ->where('validated_code', false)
  62. ->first();
  63. if(!$user) {
  64. throw new \Exception(__('messages.user_not_found_or_code_not_validated'));
  65. }
  66. $user->name = $data['name'];
  67. $user->save();
  68. $provider = new Provider();
  69. $provider->user_id = $user->id;
  70. $provider->rg = $data['document'];
  71. $provider->document = $data['document'];
  72. $provider->save();
  73. $provider->refresh();
  74. $address = new Address();
  75. $address->source = 'client';
  76. $address->source_id = $provider->id;
  77. $address->zip_code = $data['zip_code'] ?? null;
  78. $address->address = $data['address'] ?? null;
  79. $address->has_complement = $data['has_complement'] ?? null;
  80. $address->nickname = $data['nickname'] ?? null;
  81. $address->instructions = $data['instructions'] ?? null;
  82. $address->address_type = $data['address_type'] ?? null;
  83. $state = State::where('code', $data['state'])->first();
  84. $city = City::where('name', $data['city'])->where('state_id', $state->id)->first();
  85. $address->state_id = $state->id;
  86. $address->city_id = $city->id;
  87. $address->save();
  88. $result = $this->authService->loginWithEmail(
  89. email: $user->email,
  90. code: $user->code,
  91. );
  92. DB::commit();
  93. return $result;
  94. } catch (\Exception $e) {
  95. DB::rollBack();
  96. Log::error("Error registering client: " . $e->getMessage());
  97. throw $e;
  98. }
  99. }
  100. }