|
|
@@ -2,9 +2,12 @@
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
+use App\Enums\UserTypeEnum;
|
|
|
use App\Models\Address;
|
|
|
use App\Models\City;
|
|
|
use App\Models\Provider;
|
|
|
+use App\Models\ProviderServicesType;
|
|
|
+use App\Models\ProviderWorkingDay;
|
|
|
use App\Models\State;
|
|
|
use App\Models\User;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
@@ -21,8 +24,9 @@ class ProviderService
|
|
|
{
|
|
|
$providers = Provider::query()
|
|
|
->with(['user', 'profileMedia'])
|
|
|
- ->orderBy("created_at", "desc")
|
|
|
+ ->orderBy('created_at', 'desc')
|
|
|
->get();
|
|
|
+
|
|
|
return $providers;
|
|
|
}
|
|
|
|
|
|
@@ -59,51 +63,69 @@ class ProviderService
|
|
|
return $model->delete();
|
|
|
}
|
|
|
|
|
|
- public function register(array $data): ?array
|
|
|
+ public function register(array $data): ?array
|
|
|
{
|
|
|
try {
|
|
|
DB::beginTransaction();
|
|
|
- $user = User::when($data['email'], function ($q) use ($data) {
|
|
|
- $q->where('email', $data['email']);
|
|
|
- })
|
|
|
- ->when($data['phone'], function ($q) use ($data) {
|
|
|
- $q->where('phone', $data['phone']);
|
|
|
+
|
|
|
+ $email = $data['email'] ?? null;
|
|
|
+ $phone = $data['phone'] ?? null;
|
|
|
+ $code = $data['code'] ?? null;
|
|
|
+
|
|
|
+ $user = User::query()
|
|
|
+ ->where('type', UserTypeEnum::PROVIDER->value)
|
|
|
+ ->where('code', $code)
|
|
|
+ ->where(function ($query) use ($email, $phone) {
|
|
|
+ if (!empty($email)) {
|
|
|
+ $query->orWhere('email', $email);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($phone)) {
|
|
|
+ $query->orWhere('phone', $phone);
|
|
|
+ }
|
|
|
})
|
|
|
- ->where('type', 'PROVIDER')
|
|
|
- ->where('code', $data['code'])
|
|
|
- ->where('validated_code', false)
|
|
|
+ ->latest('id')
|
|
|
->first();
|
|
|
|
|
|
- if(!$user) {
|
|
|
+ if (!$user) {
|
|
|
throw new \Exception(__('messages.user_not_found_or_code_not_validated'));
|
|
|
}
|
|
|
+
|
|
|
$user->name = $data['name'];
|
|
|
+
|
|
|
+ if (empty($user->email) && !empty($email)) {
|
|
|
+ $user->email = $email;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($user->phone) && !empty($phone)) {
|
|
|
+ $user->phone = $phone;
|
|
|
+ }
|
|
|
+
|
|
|
$user->save();
|
|
|
|
|
|
$provider = new Provider();
|
|
|
$provider->user_id = $user->id;
|
|
|
- $provider->rg = $data['document'];
|
|
|
- $provider->document = $data['document'];
|
|
|
+ $provider->rg = $data['rg'] ?? null;
|
|
|
+ $provider->document = $this->sanitizeDigits($data['document'] ?? null);
|
|
|
+ $provider->birth_date = $data['birth_date'] ?? null;
|
|
|
+ $provider->daily_price_8h = $data['daily_price_8h'] ?? null;
|
|
|
+ $provider->daily_price_6h = $data['daily_price_6h'] ?? null;
|
|
|
+ $provider->daily_price_4h = $data['daily_price_4h'] ?? null;
|
|
|
+ $provider->daily_price_2h = $data['daily_price_2h'] ?? null;
|
|
|
+ $provider->is_approved = false;
|
|
|
+ $provider->selfie_media_base64 = $data['selfie_base64'] ?? null;
|
|
|
+ $provider->document_front_media_base64 = $data['document_front_base64'] ?? null;
|
|
|
+ $provider->document_back_media_base64 = $data['document_back_base64'] ?? null;
|
|
|
$provider->save();
|
|
|
$provider->refresh();
|
|
|
|
|
|
- $address = new Address();
|
|
|
- $address->source = 'client';
|
|
|
- $address->source_id = $provider->id;
|
|
|
- $address->zip_code = $data['zip_code'] ?? null;
|
|
|
- $address->address = $data['address'] ?? null;
|
|
|
- $address->has_complement = $data['has_complement'] ?? null;
|
|
|
- $address->nickname = $data['nickname'] ?? null;
|
|
|
- $address->instructions = $data['instructions'] ?? null;
|
|
|
- $address->address_type = $data['address_type'] ?? null;
|
|
|
-
|
|
|
+ $this->createProviderAddress($provider->id, $data);
|
|
|
+ $this->createProviderServicesTypes($provider->id, $data);
|
|
|
+ $this->createProviderWorkingDays($provider->id, $data);
|
|
|
|
|
|
- $state = State::where('code', $data['state'])->first();
|
|
|
- $city = City::where('name', $data['city'])->where('state_id', $state->id)->first();
|
|
|
-
|
|
|
- $address->state_id = $state->id;
|
|
|
- $address->city_id = $city->id;
|
|
|
- $address->save();
|
|
|
+ if (empty($user->email) || empty($user->code)) {
|
|
|
+ throw new \Exception(__('messages.user_not_found_or_code_not_validated'));
|
|
|
+ }
|
|
|
|
|
|
$result = $this->authService->loginWithEmail(
|
|
|
email: $user->email,
|
|
|
@@ -114,8 +136,100 @@ class ProviderService
|
|
|
return $result;
|
|
|
} catch (\Exception $e) {
|
|
|
DB::rollBack();
|
|
|
- Log::error("Error registering client: " . $e->getMessage());
|
|
|
+ Log::error('Error registering provider: ' . $e->getMessage(), [
|
|
|
+ 'data' => $data,
|
|
|
+ ]);
|
|
|
throw $e;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private function createProviderAddress(int $providerId, array $data): void
|
|
|
+ {
|
|
|
+ $state = null;
|
|
|
+ $city = null;
|
|
|
+
|
|
|
+ if (!empty($data['state'])) {
|
|
|
+ $state = State::query()
|
|
|
+ ->whereRaw('LOWER(code) = ?', [mb_strtolower($data['state'])])
|
|
|
+ ->first();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($data['city'])) {
|
|
|
+ $cityQuery = City::query()
|
|
|
+ ->whereRaw('LOWER(name) = ?', [mb_strtolower($data['city'])]);
|
|
|
+
|
|
|
+ if ($state) {
|
|
|
+ $cityQuery->where('state_id', $state->id);
|
|
|
+ }
|
|
|
+
|
|
|
+ $city = $cityQuery->first();
|
|
|
+ }
|
|
|
+
|
|
|
+ $address = new Address();
|
|
|
+ $address->source = 'provider';
|
|
|
+ $address->source_id = $providerId;
|
|
|
+ $address->zip_code = $this->sanitizeDigits($data['zip_code'] ?? null);
|
|
|
+ $address->address = $data['address'] ?? null;
|
|
|
+ $address->has_complement = (bool) ($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->state_id = $state?->id;
|
|
|
+ $address->city_id = $city?->id;
|
|
|
+ $address->save();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function createProviderServicesTypes(int $providerId, array $data): void
|
|
|
+ {
|
|
|
+ $serviceTypeIds = $data['services_types_ids'] ?? $data['service_types_ids'] ?? [];
|
|
|
+
|
|
|
+ $uniqueIds = array_values(array_unique(array_map('intval', $serviceTypeIds)));
|
|
|
+
|
|
|
+ foreach ($uniqueIds as $serviceTypeId) {
|
|
|
+ ProviderServicesType::create([
|
|
|
+ 'provider_id' => $providerId,
|
|
|
+ 'service_type_id' => $serviceTypeId,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function createProviderWorkingDays(int $providerId, array $data): void
|
|
|
+ {
|
|
|
+ $workingDays = $data['working_days'] ?? [];
|
|
|
+ $seen = [];
|
|
|
+
|
|
|
+ foreach ($workingDays as $workingDay) {
|
|
|
+ $day = (int) ($workingDay['day'] ?? -1);
|
|
|
+ $period = $workingDay['period'] ?? null;
|
|
|
+
|
|
|
+ if ($day < 0 || $day > 6 || !in_array($period, ['morning', 'afternoon'], true)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $uniqueKey = $day . '-' . $period;
|
|
|
+ if (isset($seen[$uniqueKey])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $seen[$uniqueKey] = true;
|
|
|
+
|
|
|
+ ProviderWorkingDay::create([
|
|
|
+ 'provider_id' => $providerId,
|
|
|
+ 'day' => $day,
|
|
|
+ 'period' => $period,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function sanitizeDigits(?string $value): ?string
|
|
|
+ {
|
|
|
+ if ($value === null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ $digits = preg_replace('/\D+/', '', $value);
|
|
|
+
|
|
|
+ return $digits === '' ? null : $digits;
|
|
|
+ }
|
|
|
}
|