Forráskód Böngészése

fix: allow setting visible_franchisee on product create/update

ProductRequest::rules() never declared visible_franchisee, so it was
silently dropped before reaching the model — every product defaulted
to invisible, and the franchisee's product dropdown (filtered by
visible_franchisee=true) always came back empty.
alvesantos 2 napja
szülő
commit
dac00b4007

+ 5 - 4
app/Http/Requests/ProductRequest.php

@@ -9,10 +9,11 @@ class ProductRequest extends FormRequest
     public function rules(): array
     {
         $rules = [
-            'name'        => 'sometimes|string|max:255',
-            'description' => 'sometimes|nullable|string|max:2000',
-            'price_sale'  => 'sometimes|numeric|min:0',
-            'quantity'    => 'sometimes|integer|min:0',
+            'name'               => 'sometimes|string|max:255',
+            'description'        => 'sometimes|nullable|string|max:2000',
+            'price_sale'         => 'sometimes|numeric|min:0',
+            'quantity'           => 'sometimes|integer|min:0',
+            'visible_franchisee' => 'sometimes|boolean',
         ];
 
         if ($this->isMethod('POST')) {

+ 1 - 0
app/Http/Resources/ProductResource.php

@@ -23,6 +23,7 @@ public function toArray(Request $request): array
             'description' => $this->description,
             'price_sale'  => $this->price_sale,
             'quantity'    => $this->quantity,
+            'visible_franchisee' => $this->visible_franchisee,
             'created_at'  => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
             'updated_at'  => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
         ];

+ 1 - 0
app/Models/Product.php

@@ -62,6 +62,7 @@ class Product extends Model
     protected $casts = [
         'created_at' => 'datetime',
         'updated_at' => 'datetime',
+        'visible_franchisee' => 'boolean',
         // Add your casts here (e.g., 'is_active' => 'boolean')
     ];
 

+ 60 - 0
tests/Unit/ProductVisibleFranchiseeTest.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace Tests\Unit;
+
+use App\Http\Requests\ProductRequest;
+use App\Models\Product;
+use App\Services\ProductService;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Validator;
+use Tests\TestCase;
+
+/**
+ * Regressão: produtos não apareciam pra franqueada porque visible_franchisee
+ * nunca estava nas rules() do ProductRequest — o toggle "Visível para
+ * Franqueado" do cadastro de produto era descartado antes de chegar no
+ * banco, então todo produto ficava com visible_franchisee=false (default da
+ * coluna), e o filtro `visible_franchisee=true` usado na tela de Proposta
+ * Comercial da franqueada nunca retornava nada.
+ */
+class ProductVisibleFranchiseeTest extends TestCase
+{
+    use RefreshDatabase;
+
+    public function test_request_accepts_visible_franchisee(): void
+    {
+        $request = ProductRequest::create('/product', 'POST', [
+            'name'               => 'Produto Teste',
+            'price_sale'         => 10,
+            'quantity'           => 1,
+            'visible_franchisee' => true,
+        ]);
+
+        $validator = Validator::make($request->all(), $request->rules());
+
+        $this->assertFalse($validator->fails());
+    }
+
+    public function test_service_persists_visible_franchisee_and_select_filter_finds_it(): void
+    {
+        $service = new ProductService();
+
+        $service->create([
+            'name'               => 'Visível pro Franqueado',
+            'price_sale'         => 10,
+            'quantity'           => 1,
+            'visible_franchisee' => true,
+        ]);
+
+        $service->create([
+            'name'               => 'Só Franqueadora',
+            'price_sale'         => 10,
+            'quantity'           => 1,
+            'visible_franchisee' => false,
+        ]);
+
+        $visibleForFranchisee = Product::query()->where('visible_franchisee', true)->pluck('name');
+
+        $this->assertSame(['Visível pro Franqueado'], $visibleForFranchisee->all());
+    }
+}