Bladeren bron

feat: :sparkles: feat (localizacao) WIP criando sistema de localizacao e calculo de distancias

iniciado sistema de localizacoes e calculos de distancias usando API do google e bibliotecas do capacitor

fase:dev | origin:escopo
Gustavo Zanatta 1 maand geleden
bovenliggende
commit
63f7dc8fbc

+ 19 - 20
app/Http/Requests/RegisterClientRequest.php

@@ -3,31 +3,30 @@
 namespace App\Http\Requests;
 
 use Illuminate\Foundation\Http\FormRequest;
-use Illuminate\Validation\Rule;
 
 class RegisterClientRequest extends FormRequest
 {
   public function rules(): array
   {
-    $rules = [
-      'email' => 'sometimes|email',
-      'phone' => 'sometimes|string|nullable',
-      'name' => 'required|string|max:255',
-      'code' => 'required|string|max:6',
-      'document' => [
-        'sometimes',
-        'string',
-      ],
-      'zip_code' => 'sometimes|string|max:20',
-      'address' => 'sometimes|string|max:255',
-      'has_complement' => 'sometimes|boolean',
-      'nickname' => 'sometimes|string|max:255',
-      'instructions' => 'sometimes|string|max:255',
-      'address_type' => 'sometimes|string|max:255',
-      'city' => 'sometimes|string|max:255',
-      'state' => 'sometimes|string|max:255',
+    return [
+      'email'         => 'sometimes|email|nullable',
+      'phone'         => 'sometimes|string|nullable',
+      'name'          => 'sometimes|string|max:255|nullable',
+      'code'          => 'required|string|max:6',
+      'document'      => 'sometimes|string|nullable',
+      'zip_code'      => 'sometimes|string|max:20|nullable',
+      'address'       => 'sometimes|string|max:255|nullable',
+      'number'        => 'sometimes|string|max:20|nullable',
+      'district'      => 'sometimes|string|max:255|nullable',
+      'complement'    => 'sometimes|string|max:255|nullable',
+      'has_complement' => 'sometimes|boolean|nullable',
+      'nickname'      => 'sometimes|string|max:255|nullable',
+      'instructions'  => 'sometimes|string|max:500|nullable',
+      'address_type'  => 'sometimes|string|in:home,commercial,other|nullable',
+      'city'          => 'sometimes|string|max:255|nullable',
+      'state'         => 'sometimes|string|max:10|nullable',
+      'latitude'      => 'sometimes|numeric|nullable',
+      'longitude'     => 'sometimes|numeric|nullable',
     ];
-
-    return $rules;
   }
 }

+ 4 - 0
app/Models/Address.php

@@ -25,11 +25,15 @@ class Address extends Model
         'state_id',
         'address_type',
         'is_primary',
+        'latitude',
+        'longitude',
     ];
 
     protected $casts = [
         'has_complement' => 'boolean',
         'is_primary' => 'boolean',
+        'latitude' => 'float',
+        'longitude' => 'float',
         'created_at' => 'datetime',
         'updated_at' => 'datetime',
         'deleted_at' => 'datetime',

+ 26 - 15
app/Services/ClientService.php

@@ -49,26 +49,29 @@ class ClientService
   {
     try {
       DB::beginTransaction();
-      $user = User::when($data['email'], function ($q) use ($data) {
+
+      $user = User::when(!empty($data['email']), function ($q) use ($data) {
           $q->where('email', $data['email']);
         })
-        ->when($data['phone'], function ($q) use ($data) {
+        ->when(!empty($data['phone']), function ($q) use ($data) {
           $q->where('phone', $data['phone']);
         })
         ->where('type', 'CLIENT')
         ->where('code', $data['code'])
-        ->where('validated_code', false)
         ->first();
 
-      if(!$user) {
+      if (!$user) {
         throw new \Exception(__('messages.user_not_found_or_code_not_validated'));
       }
-      $user->name = $data['name'];
-      $user->save();
+
+      if (!empty($data['name'])) {
+        $user->name = $data['name'];
+        $user->save();
+      }
 
       $client = new Client();
       $client->user_id = $user->id;
-      $client->document = $data['document'];
+      $client->document = $data['document'] ?? null;
       $client->save();
       $client->refresh();
 
@@ -77,17 +80,25 @@ class ClientService
       $address->source_id = $client->id;
       $address->zip_code = $data['zip_code'] ?? null;
       $address->address = $data['address'] ?? null;
-      $address->has_complement = $data['has_complement'] ?? 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'] ?? null;
-
-
-      $state = State::where('code', $data['state'])->first();
-      $city = City::where('name', $data['city'])->where('state_id', $state->id)->first();
+      $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->state_id = $state->id;
-      $address->city_id = $city->id;
       $address->save();
 
       $result = $this->authService->loginWithEmail(

+ 26 - 0
database/migrations/2026_05_06_092145_add_coordinates_to_addresses_table.php

@@ -0,0 +1,26 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('addresses', function (Blueprint $table) {
+            $table->decimal('latitude', 10, 8)->nullable()->after('district');
+            $table->decimal('longitude', 11, 8)->nullable()->after('latitude');
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('addresses', function (Blueprint $table) {
+            $table->dropColumn(['latitude', 'longitude']);
+        });
+    }
+};

+ 1 - 0
lang/en/messages.php

@@ -11,4 +11,5 @@ return [
     'import_error' => 'Error importing',
     'buyer_not_allowed' => 'Buyer not allowed',
     'code_sent' => 'Verification code sent successfully',
+    'user_not_found_or_code_not_validated' => 'User not found or invalid code.',
 ];

+ 1 - 0
lang/es/messages.php

@@ -11,4 +11,5 @@ return [
     'import_error' => 'Error al importar',
     'buyer_not_allowed' => 'Comprador no permitido',
     'code_sent' => 'Código de verificación enviado exitosamente',
+    'user_not_found_or_code_not_validated' => 'Usuario no encontrado o código inválido.',
 ];

+ 1 - 0
lang/pt/messages.php

@@ -11,4 +11,5 @@ return [
     'import_error' => 'Erro ao importar',
     'buyer_not_allowed' => 'Compra não permitida, tente outro ingresso ou comprador',
     'code_sent' => 'Código de verificação enviado com sucesso',
+    'user_not_found_or_code_not_validated' => 'Usuário não encontrado ou código inválido.',
 ];