refactor(catalog): Complete catalog refactor to simplify its data model and its querying.

source commits: refactor/catalog
This commit is contained in:
2026-07-21 09:23:19 -03:00
parent d3182fbd13
commit 29a2ca19a3
116 changed files with 5141 additions and 5217 deletions

View File

@@ -7,9 +7,10 @@ use App\Domains\Attachable\Models\Attachment;
use App\Domains\Auth\Models\User;
use App\Domains\Cart\Models\Cart;
use App\Domains\Catalog\Enums\InventoryPolicy;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\Product;
use App\Domains\Catalog\Models\ProductVariant;
use App\Domains\Catalog\Models\Inventory;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Services\CheckoutService;
use App\Domains\Tenant\Models\Tenant;
@@ -32,27 +33,25 @@ class StorePurchaseTest extends TestCase
'nombre' => 'Test Category',
]);
$product = Product::query()->create([
'tenant_codigo' => 'sonder',
'categoria_id' => $category->id,
$inventory = Inventory::query()->create(['real_stock' => 10]);
$catalogItem = CatalogItem::query()->create([
'tenant_code' => 'sonder',
'category_id' => $category->id,
'slug' => 'test-product',
'nombre' => 'Test Product',
'descripcion' => 'Test',
'precio' => '50.00',
]);
$variant = ProductVariant::query()->create([
'producto_id' => $product->id,
'slug' => 'test-variant',
'nombre' => 'Test Variant',
'stock' => 10,
'precio' => '50.00',
$variant = Variant::query()->create([
'catalog_item_id' => $catalogItem->id,
'inventory_id' => $inventory->id,
]);
$cartResponse = $this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/cart/items', [
'buyable_type' => 'variant',
'buyable_id' => $variant->id,
'catalog_item_id' => $catalogItem->id,
'variant_id' => $variant->id,
'cantidad' => 2,
])
->assertOk();
@@ -112,14 +111,14 @@ class StorePurchaseTest extends TestCase
]);
$this->assertDatabaseHas('carrito_items', [
'cart_id' => $cartId,
'buyable_type' => ProductVariant::class,
'buyable_id' => $variant->id,
'catalog_item_id' => $catalogItem->id,
'variant_id' => $variant->id,
'cantidad' => 2,
]);
$this->assertDatabaseHas('productos_variantes', [
'id' => $variant->id,
'stock_real' => 10,
'stock_reservado' => 2,
$this->assertDatabaseHas('inventories', [
'id' => $inventory->id,
'real_stock' => 10,
'reserved_stock' => 2,
]);
}
@@ -133,8 +132,8 @@ class StorePurchaseTest extends TestCase
$cartId = $this->actingAs($user, 'sanctum')
->postJson('/api/tenants/sonder/cart/items', [
'buyable_type' => 'variant',
'buyable_id' => $variant->id,
'catalog_item_id' => $variant->catalog_item_id,
'variant_id' => $variant->id,
'cantidad' => 2,
])
->assertOk()
@@ -187,9 +186,9 @@ class StorePurchaseTest extends TestCase
->assertJsonPath('data.items.0.quantity', 2)
->assertJsonPath('data.items.0.unit_price', '50.00')
->assertJsonPath('data.items.0.line_total', '100.00')
->assertJsonPath('data.items.0.product.id', $variant->product->id)
->assertJsonPath('data.items.0.product.nombre', $variant->product->nombre)
->assertJsonPath('data.items.0.product.slug', $variant->product->slug)
->assertJsonPath('data.items.0.product.id', $variant->catalogItem->id)
->assertJsonPath('data.items.0.product.nombre', $variant->catalogItem->nombre)
->assertJsonPath('data.items.0.product.slug', $variant->catalogItem->slug)
->assertJsonPath('data.items.0.product.imagen', null)
->assertJsonPath('data.items.0.variant.id', $variant->id)
->assertJsonPath('data.items.0.variant.attributes', [])
@@ -245,11 +244,11 @@ class StorePurchaseTest extends TestCase
$checkoutService->confirmPurchase($purchase);
$purchase->refresh()->markAsPaid();
$this->assertDatabaseHas('productos_variantes', [
'id' => $variant->id,
'stock_real' => 8,
'stock_reservado' => 0,
'cantidad_vendida' => 2,
$this->assertDatabaseHas('inventories', [
'id' => $variant->inventory_id,
'real_stock' => 8,
'reserved_stock' => 0,
'sold_units' => 2,
]);
$this->assertSoftDeleted('carritos', [
@@ -265,10 +264,10 @@ class StorePurchaseTest extends TestCase
->assertJsonPath('data.items.0.quantity', 2)
->assertJsonPath('data.items.0.unit_price', '50.00')
->assertJsonPath('data.items.0.line_total', '100.00')
->assertJsonPath('data.items.0.product.id', $variant->product->id)
->assertJsonPath('data.items.0.product.imagen', null)
->assertJsonPath('data.items.0.variant.id', $variant->id)
->assertJsonPath('data.items.0.variant.attributes', [])
->assertJsonPath('data.items.0.source_catalog_item_id', $variant->catalog_item_id)
->assertJsonPath('data.items.0.source_variant_id', $variant->id)
->assertJsonPath('data.items.0.item_details.imagen', null)
->assertJsonPath('data.items.0.item_details.attributes', [])
->assertJsonPath('data.subtotal', '100.00')
->assertJsonPath('data.total', '100.00');
}
@@ -283,8 +282,13 @@ class StorePurchaseTest extends TestCase
$purchase = $this->createCheckoutPurchase($user, 'sonder', $variant, 2);
$purchase->items()->create([
'buyable_type' => ProductVariant::class,
'buyable_id' => $variant->id,
'source_catalog_item_id' => $variant->catalog_item_id,
'source_variant_id' => $variant->id,
'nombre' => $variant->catalogItem->nombre,
'descripcion' => $variant->catalogItem->descripcion,
'slug' => $variant->catalogItem->slug,
'item_nombre' => $variant->getName(),
'variant_attributes' => [],
'cantidad' => 1,
'precio_unitario' => '50.00',
'discount_total' => null,
@@ -330,8 +334,8 @@ class StorePurchaseTest extends TestCase
$cartId = $this->actingAs($owner, 'sanctum')
->postJson('/api/tenants/sonder/cart/items', [
'buyable_type' => 'variant',
'buyable_id' => $variant->id,
'catalog_item_id' => $variant->catalog_item_id,
'variant_id' => $variant->id,
'cantidad' => 1,
])
->assertOk()
@@ -357,8 +361,8 @@ class StorePurchaseTest extends TestCase
$cartId = $this->actingAs($user, 'sanctum')
->postJson('/api/tenants/globex/cart/items', [
'buyable_type' => 'variant',
'buyable_id' => $variant->id,
'catalog_item_id' => $variant->catalog_item_id,
'variant_id' => $variant->id,
'cantidad' => 1,
])
->assertOk()
@@ -438,12 +442,11 @@ class StorePurchaseTest extends TestCase
$checkoutService->confirmPurchase($purchase);
$checkoutService->confirmPurchase($purchase);
$this->assertDatabaseHas('productos_variantes', [
'id' => $variant->id,
'inventory_policy' => InventoryPolicy::Unlimited->value,
'stock_real' => 0,
'stock_reservado' => 0,
'cantidad_vendida' => 25,
$this->assertDatabaseHas('inventories', [
'id' => $variant->inventory_id,
'real_stock' => 0,
'reserved_stock' => 0,
'sold_units' => 25,
]);
$this->assertDatabaseCount('compra_items', 1);
}
@@ -454,7 +457,7 @@ class StorePurchaseTest extends TestCase
string $price,
string $slugPrefix = 'shirt',
InventoryPolicy $inventoryPolicy = InventoryPolicy::Tracked,
): ProductVariant {
): Variant {
$tenant = Tenant::query()->where('codigo', $tenantCode)->first();
if (! $tenant) {
$this->createTenant($tenantCode, ucfirst($tenantCode), "{$tenantCode}.com");
@@ -465,30 +468,27 @@ class StorePurchaseTest extends TestCase
'nombre' => "{$slugPrefix} category {$tenantCode}",
]);
$product = Product::query()->create([
'tenant_codigo' => $tenantCode,
'categoria_id' => $category->id,
'slug' => "{$slugPrefix}-{$tenantCode}-".Product::query()->count(),
$inventory = Inventory::query()->create(['real_stock' => $stock]);
$catalogItem = CatalogItem::query()->create([
'tenant_code' => $tenantCode,
'category_id' => $category->id,
'slug' => "{$slugPrefix}-{$tenantCode}-".CatalogItem::query()->count(),
'nombre' => ucfirst($slugPrefix)." {$tenantCode}",
'descripcion' => 'Test product',
'precio' => $price,
'inventory_policy' => $inventoryPolicy,
]);
return ProductVariant::query()->create([
'producto_id' => $product->id,
'inventory_policy' => $inventoryPolicy->value,
'slug' => "{$slugPrefix}-variant-".ProductVariant::query()->count(),
'nombre' => ucfirst($slugPrefix).' Variant',
'stock' => $stock,
'descripcion' => 'Test variant',
'precio' => $price,
])->load('product');
return Variant::query()->create([
'catalog_item_id' => $catalogItem->id,
'inventory_id' => $inventory->id,
])->load(['catalogItem', 'inventory']);
}
protected function createCheckoutPurchase(
User $user,
string $tenantCode,
ProductVariant $variant,
Variant $variant,
int $quantity,
): Purchase {
$tenant = Tenant::query()->where('codigo', $tenantCode)->firstOrFail();
@@ -498,7 +498,7 @@ class StorePurchaseTest extends TestCase
'status' => 'active',
]);
$cart->addItem(ProductVariant::class, $variant->id, $quantity);
$cart->addItem($variant->catalog_item_id, $variant->id, $quantity);
return app(CheckoutService::class)->startCheckout($tenant, $user->id, [
'cart_id' => $cart->id,