ClientService.php 5.2 KB

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