ClientService.php 5.1 KB

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