|
|
@@ -69,37 +69,52 @@ class ClientService
|
|
|
$user->save();
|
|
|
}
|
|
|
|
|
|
- $client = new Client();
|
|
|
- $client->user_id = $user->id;
|
|
|
- $client->document = $data['document'] ?? null;
|
|
|
- $client->save();
|
|
|
+ $client = Client::firstOrCreate(
|
|
|
+ ['user_id' => $user->id],
|
|
|
+ ['document' => $data['document'] ?? null]
|
|
|
+ );
|
|
|
+ if (!empty($data['document'])) {
|
|
|
+ $client->document = $data['document'];
|
|
|
+ $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;
|
|
|
+ $addressData = [
|
|
|
+ 'zip_code' => $data['zip_code'] ?? null,
|
|
|
+ 'address' => $data['address'] ?? null,
|
|
|
+ 'number' => $data['number'] ?? null,
|
|
|
+ 'district' => $data['district'] ?? null,
|
|
|
+ 'has_complement' => $data['has_complement'] ?? false,
|
|
|
+ 'complement' => $data['complement'] ?? null,
|
|
|
+ 'nickname' => $data['nickname'] ?? null,
|
|
|
+ 'instructions' => $data['instructions'] ?? null,
|
|
|
+ 'address_type' => $data['address_type'] ?? 'home',
|
|
|
+ 'latitude' => $data['latitude'] ?? null,
|
|
|
+ '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;
|
|
|
+ $addressData['state_id'] = $state->id;
|
|
|
+ $addressData['city_id'] = $city?->id;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $address->save();
|
|
|
+ Address::updateOrCreate(
|
|
|
+ ['source' => 'client', 'source_id' => $client->id],
|
|
|
+ $addressData
|
|
|
+ );
|
|
|
+
|
|
|
+ $registrationComplete = !empty($user->name)
|
|
|
+ && !empty($client->document)
|
|
|
+ && Address::where('source', 'client')->where('source_id', $client->id)->exists();
|
|
|
+
|
|
|
+ if ($registrationComplete !== $user->registration_complete) {
|
|
|
+ $user->registration_complete = $registrationComplete;
|
|
|
+ $user->save();
|
|
|
+ }
|
|
|
|
|
|
$result = $this->authService->loginWithEmail(
|
|
|
email: $user->email,
|