| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace App\Services;
- use App\Models\Address;
- use App\Models\City;
- use App\Models\Client;
- use App\Models\State;
- use App\Models\User;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class ClientService
- {
- public function __construct(
- private readonly AuthService $authService,
- ) {}
- public function getAll(): Collection
- {
- return Client::with(['user'])->get();
- }
- public function findById(int $id): ?Client
- {
- return Client::with(['user'])->find($id);
- }
- public function create(array $data): Client
- {
- return Client::create($data);
- }
- public function update(array $data, int $id)
- {
- $client = Client::findOrFail($id);
- $client->update($data);
- return $client;
- }
- public function delete(int $id): bool
- {
- $client = Client::findOrFail($id);
- return $client->delete();
- }
- public function register(array $data): ?array
- {
- try {
- DB::beginTransaction();
- $user = User::when(!empty($data['email']), function ($q) use ($data) {
- $q->where('email', $data['email']);
- })
- ->when(!empty($data['phone']), function ($q) use ($data) {
- $q->where('phone', $data['phone']);
- })
- ->where('type', 'CLIENT')
- ->where('code', $data['code'])
- ->first();
- if (!$user) {
- throw new \Exception(__('messages.user_not_found_or_code_not_validated'));
- }
- if (!empty($data['name'])) {
- $user->name = $data['name'];
- $user->save();
- }
- $client = new Client();
- $client->user_id = $user->id;
- $client->document = $data['document'] ?? null;
- $client->save();
- $client->refresh();
- $address = new Address();
- $address->source = 'client';
- $address->source_id = $client->id;
- $address->zip_code = $data['zip_code'] ?? null;
- $address->address = $data['address'] ?? null;
- $address->number = $data['number'] ?? null;
- $address->district = $data['district'] ?? null;
- $address->has_complement = $data['has_complement'] ?? false;
- $address->complement = $data['complement'] ?? null;
- $address->nickname = $data['nickname'] ?? null;
- $address->instructions = $data['instructions'] ?? null;
- $address->address_type = $data['address_type'] ?? 'home';
- $address->latitude = $data['latitude'] ?? null;
- $address->longitude = $data['longitude'] ?? null;
- if (!empty($data['state']) && !empty($data['city'])) {
- $state = State::where('code', $data['state'])->first();
- if ($state) {
- $city = City::where('name', $data['city'])->where('state_id', $state->id)->first();
- $address->state_id = $state->id;
- $address->city_id = $city?->id;
- }
- }
- $address->save();
- $result = $this->authService->loginWithEmail(
- email: $user->email,
- code: $user->code,
- );
- DB::commit();
- return $result;
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error("Error registering client: " . $e->getMessage());
- throw $e;
- }
- }
- }
|