feat(catalog): add max_units_per_user field to catalog items and implement purchase limit validation

This commit is contained in:
2026-08-06 16:37:48 -03:00
parent 20dd246cd8
commit 5ad61eb217
13 changed files with 378 additions and 7 deletions

View File

@@ -32,6 +32,7 @@ class CatalogItemControllerTest extends TestCase
'slug' => 'shirt',
'nombre' => 'Shirt',
'precio' => 100,
'max_units_per_user' => 4,
'attribute_codes' => [$attribute->codigo],
'images' => [$image, $image],
'variants' => [
@@ -46,6 +47,7 @@ class CatalogItemControllerTest extends TestCase
$response
->assertCreated()
->assertJsonPath('data.nombre', 'Shirt')
->assertJsonPath('data.max_units_per_user', 4)
->assertJsonCount(2, 'data.images')
->assertJsonCount(1, 'data.variants')
->assertJsonCount(1, 'data.variants.0.images');
@@ -54,6 +56,7 @@ class CatalogItemControllerTest extends TestCase
$variant = $item->variants()->firstOrFail();
$this->assertSame([0, 1], $item->attachments()->get()->pluck('pivot.orden')->all());
$this->assertSame(4, $item->max_units_per_user);
$this->assertSame([0], $variant->attachments()->get()->pluck('pivot.orden')->all());
$this->assertDatabaseHas('catalog_items_attachments', [
'catalog_item_id' => $item->id,
@@ -77,6 +80,21 @@ class CatalogItemControllerTest extends TestCase
$this->assertDatabaseMissing('catalog_items', ['slug' => 'invalid-image']);
}
public function test_it_rejects_a_non_positive_user_purchase_limit(): void
{
$tenant = $this->createTenant('purchase-limit-validation');
$this->postJson("/api/tenants/{$tenant->codigo}/catalog-items", [
'slug' => 'invalid-purchase-limit',
'nombre' => 'Invalid purchase limit',
'precio' => 100,
'real_stock' => 10,
'max_units_per_user' => 0,
])->assertUnprocessable()->assertJsonValidationErrors('max_units_per_user');
$this->assertDatabaseMissing('catalog_items', ['slug' => 'invalid-purchase-limit']);
}
private function createTenant(string $code = 'catalog-controller'): Tenant
{
$headerLogo = $this->createAttachment("{$code}-header");