feat: implement multi-tenant cart functionality and add image validation helper

This commit is contained in:
ncoronel 2026-07-01 15:38:12 -03:00
parent 3285015841
commit 4943e68606
5 changed files with 69 additions and 17 deletions

View File

@ -48,6 +48,7 @@ class CartItemResource extends JsonResource
'cantidad' => $this->cantidad,
'precio_unitario' => $this->formatMoney($product?->precio),
'product_id' => $product?->id,
'product_variant_id' => $this->producto_variante_id,
'product' => $product === null ? null : [
'nombre' => $productName,
'imagen' => $imageUrl,

View File

@ -21,6 +21,10 @@ class ImageOrBase64Rule implements ValidationRule
if (is_string($value)) {
$payload = trim($value);
if (\Illuminate\Support\Str::isUuid($payload)) {
return;
}
if ($payload === '') {
$fail("The :attribute must not be empty.");
return;

View File

@ -13,7 +13,9 @@ return Application::configure(basePath: dirname(__DIR__))
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
//
$middleware->encryptCookies(except: [
'guest_token',
]);
})
->withExceptions(function (Exceptions $exceptions): void {
$exceptions->shouldRenderJsonWhen(

Binary file not shown.

View File

@ -72,7 +72,7 @@ class CartControllerTest extends TestCase
$this->assertDatabaseHas('carritos', [
'tenant_codigo' => 'acme',
'guest_token' => $response->getCookie('guest_token')?->getValue(),
'guest_token' => $response->getCookie('guest_token', false)?->getValue(),
'status' => 'active',
]);
@ -96,13 +96,22 @@ class CartControllerTest extends TestCase
'cantidad' => 2,
]);
$guestToken = $firstResponse->getCookie('guest_token')?->getValue();
$guestToken = $firstResponse->getCookie('guest_token', false)?->getValue();
$this->withCookie('guest_token', $guestToken)
->postJson('/api/tenants/acme/cart/items', [
$response = $this->call(
'POST',
'/api/tenants/acme/cart/items',
[],
['guest_token' => $guestToken],
[],
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
json_encode([
'product_variant_id' => $variant->id,
'cantidad' => 3,
])
);
$response
->assertOk()
->assertJsonPath('data.items.0.cantidad', 5)
->assertJsonPath('data.subtotal', '125.00');
@ -128,12 +137,21 @@ class CartControllerTest extends TestCase
'cantidad' => 2,
]);
$guestToken = $createResponse->getCookie('guest_token')?->getValue();
$guestToken = $createResponse->getCookie('guest_token', false)?->getValue();
$this->withCookie('guest_token', $guestToken)
->patchJson("/api/tenants/acme/cart/items/{$variant->id}", [
$response = $this->call(
'PATCH',
"/api/tenants/acme/cart/items/{$variant->id}",
[],
['guest_token' => $guestToken],
[],
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
json_encode([
'cantidad' => 5,
])
);
$response
->assertOk()
->assertJsonPath('data.items.0.cantidad', 5)
->assertJsonPath('data.subtotal', '75.00');
@ -157,10 +175,18 @@ class CartControllerTest extends TestCase
'cantidad' => 4,
]);
$guestToken = $createResponse->getCookie('guest_token')?->getValue();
$guestToken = $createResponse->getCookie('guest_token', false)?->getValue();
$this->withCookie('guest_token', $guestToken)
->deleteJson("/api/tenants/acme/cart/items/{$variant->id}")
$response = $this->call(
'DELETE',
"/api/tenants/acme/cart/items/{$variant->id}",
[],
['guest_token' => $guestToken],
[],
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json']
);
$response
->assertOk()
->assertJsonPath('data.items', [])
->assertJsonPath('data.subtotal', '0.00');
@ -249,20 +275,38 @@ class CartControllerTest extends TestCase
'cantidad' => 2,
]);
$guestToken = $response->getCookie('guest_token')?->getValue();
$guestToken = $response->getCookie('guest_token', false)?->getValue();
$this->withCookie('guest_token', $guestToken)
->postJson('/api/tenants/acme/cart/items', [
$response1 = $this->call(
'POST',
'/api/tenants/acme/cart/items',
[],
['guest_token' => $guestToken],
[],
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
json_encode([
'product_variant_id' => $variant->id,
'cantidad' => 1,
])
);
$response1
->assertUnprocessable()
->assertJsonValidationErrors(['cantidad']);
$this->withCookie('guest_token', $guestToken)
->patchJson("/api/tenants/acme/cart/items/{$variant->id}", [
$response2 = $this->call(
'PATCH',
"/api/tenants/acme/cart/items/{$variant->id}",
[],
['guest_token' => $guestToken],
[],
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
json_encode([
'cantidad' => 3,
])
);
$response2
->assertUnprocessable()
->assertJsonValidationErrors(['cantidad']);
}
@ -330,7 +374,8 @@ class CartControllerTest extends TestCase
'secondary_color' => '#222222',
'danger_color' => '#333333',
'success_color' => '#28a745',
'header_footer_bg_color' => '#444444',
'header_bg_color' => '#444444',
'footer_bg_color' => '#444444',
'header_logo_id' => $headerAttachment->id,
'footer_logo_id' => $footerAttachment->id,
]);