refactor(catalog): Complete catalog refactor to simplify its data model and its querying.
source commits: refactor/catalog
This commit is contained in:
@@ -4,107 +4,70 @@ namespace Tests\Feature\Cart;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Models\Attribute;
|
||||
use App\Domains\Catalog\Models\Category;
|
||||
use App\Domains\Catalog\Models\Product;
|
||||
use App\Domains\Catalog\Models\ProductAttribute;
|
||||
use App\Domains\Catalog\Models\ProductVariant;
|
||||
use App\Domains\Catalog\Models\ProductVariantDefinition;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CartControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_returns_an_empty_guest_cart_when_cart_does_not_exist(): void
|
||||
public function test_cart_items_have_explicit_catalog_and_optional_variant_keys(): void
|
||||
{
|
||||
$this->createTenant('acme', 'Acme', 'acme.com');
|
||||
|
||||
$this->getJson('/api/tenants/acme/cart')
|
||||
->assertOk()
|
||||
->assertJson([
|
||||
'data' => [
|
||||
'id' => null,
|
||||
'tenant_codigo' => 'acme',
|
||||
'status' => 'active',
|
||||
'items' => [],
|
||||
'subtotal' => '0.00',
|
||||
],
|
||||
]);
|
||||
$this->assertTrue(Schema::hasColumns('carrito_items', [
|
||||
'catalog_item_id',
|
||||
'variant_id',
|
||||
]));
|
||||
$this->assertFalse(Schema::hasColumn('carrito_items', 'buyable_type'));
|
||||
$this->assertFalse(Schema::hasColumn('carrito_items', 'buyable_id'));
|
||||
}
|
||||
|
||||
public function test_it_creates_a_guest_cart_and_returns_the_cart_snapshot(): void
|
||||
public function test_it_adds_a_catalog_item_without_a_variant(): void
|
||||
{
|
||||
$variant = $this->createVariantForTenant('acme', 10, '49.90');
|
||||
$attribute = Attribute::query()->create([
|
||||
'tenant_codigo' => 'acme',
|
||||
'codigo' => 'color',
|
||||
'nombre' => 'Color',
|
||||
'type' => 'string',
|
||||
]);
|
||||
|
||||
$productAttribute = ProductAttribute::query()->create([
|
||||
'product_id' => $variant->producto_id,
|
||||
'attribute_id' => $attribute->id,
|
||||
]);
|
||||
|
||||
ProductVariantDefinition::query()->create([
|
||||
'producto_variante_id' => $variant->id,
|
||||
'products_attribute_id' => $productAttribute->id,
|
||||
'value' => 'Red',
|
||||
]);
|
||||
$tenant = $this->createTenant('acme');
|
||||
$item = $this->createDirectItem($tenant, 10, '49.90');
|
||||
|
||||
$response = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertOk()
|
||||
->assertCookie('guest_token')
|
||||
->assertJsonPath('data.tenant_codigo', 'acme')
|
||||
->assertJsonPath('data.items.0.catalog_item_id', $item->id)
|
||||
->assertJsonPath('data.items.0.variant_id', null)
|
||||
->assertJsonPath('data.items.0.cantidad', 2)
|
||||
->assertJsonPath('data.items.0.precio_unitario', '49.90')
|
||||
->assertJsonPath('data.items.0.buyable_type', 'variant')
|
||||
->assertJsonPath('data.items.0.buyable_id', $variant->id)
|
||||
->assertJsonPath('data.items.0.product.nombre', 'Shirt acme (Color: Red)')
|
||||
->assertJsonPath('data.items.0.product.imagen', null)
|
||||
->assertJsonPath('data.items.0.product.nombre', 'Item acme')
|
||||
->assertJsonPath('data.subtotal', '99.80');
|
||||
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'tenant_codigo' => 'acme',
|
||||
'guest_token' => $response->getCookie('guest_token', false)?->getValue(),
|
||||
'status' => 'active',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
'buyable_type' => ProductVariant::class,
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => null,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'stock_real' => 10,
|
||||
'stock_reservado' => 2,
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $item->inventory_id,
|
||||
'reserved_stock' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_merges_quantities_when_the_same_guest_adds_the_same_variant_twice(): void
|
||||
public function test_it_adds_a_specific_variant_and_merges_repeated_additions(): void
|
||||
{
|
||||
$variant = $this->createVariantForTenant('acme', 12, '25.00');
|
||||
$tenant = $this->createTenant('acme');
|
||||
[$item, $variant] = $this->createVariantItem($tenant, 12, '25.00');
|
||||
|
||||
$firstResponse = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
])->assertOk();
|
||||
|
||||
$guestToken = $firstResponse->getCookie('guest_token', false)?->getValue();
|
||||
|
||||
@@ -116,279 +79,38 @@ class CartControllerTest extends TestCase
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
|
||||
json_encode([
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 3,
|
||||
])
|
||||
]),
|
||||
);
|
||||
|
||||
$response
|
||||
->assertOk()
|
||||
->assertJsonPath('data.items.0.catalog_item_id', $item->id)
|
||||
->assertJsonPath('data.items.0.variant_id', $variant->id)
|
||||
->assertJsonPath('data.items.0.cantidad', 5)
|
||||
->assertJsonPath('data.subtotal', '125.00');
|
||||
|
||||
$this->assertDatabaseCount('carritos', 1);
|
||||
$this->assertDatabaseCount('carrito_items', 1);
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
'buyable_type' => ProductVariant::class,
|
||||
'buyable_id' => $variant->id,
|
||||
'cantidad' => 5,
|
||||
]);
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'stock_real' => 12,
|
||||
'stock_reservado' => 5,
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 5,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_updates_item_quantity_and_adjusts_stock(): void
|
||||
public function test_it_updates_and_removes_an_item_using_its_selected_inventory(): void
|
||||
{
|
||||
$variant = $this->createVariantForTenant('acme', 10, '15.00');
|
||||
|
||||
$tenant = $this->createTenant('acme');
|
||||
[$item, $variant] = $this->createVariantItem($tenant, 10, '15.00');
|
||||
$createResponse = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $variant->id,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
|
||||
$guestToken = $createResponse->getCookie('guest_token', false)?->getValue();
|
||||
$cartItemId = $createResponse->json('data.items.0.id');
|
||||
|
||||
$response = $this->call(
|
||||
'PATCH',
|
||||
"/api/tenants/acme/cart/items/{$cartItemId}",
|
||||
[],
|
||||
['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');
|
||||
|
||||
$this->assertDatabaseHas('carrito_items', [
|
||||
'buyable_type' => ProductVariant::class,
|
||||
'buyable_id' => $variant->id,
|
||||
'cantidad' => 5,
|
||||
]);
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'stock_real' => 10,
|
||||
'stock_reservado' => 5,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_removes_an_item_and_restores_stock(): void
|
||||
{
|
||||
$variant = $this->createVariantForTenant('acme', 10, '15.00');
|
||||
|
||||
$createResponse = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'cantidad' => 4,
|
||||
]);
|
||||
|
||||
$guestToken = $createResponse->getCookie('guest_token', false)?->getValue();
|
||||
$cartItemId = $createResponse->json('data.items.0.id');
|
||||
|
||||
$response = $this->call(
|
||||
'DELETE',
|
||||
"/api/tenants/acme/cart/items/{$cartItemId}",
|
||||
[],
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json']
|
||||
);
|
||||
|
||||
$response
|
||||
->assertOk()
|
||||
->assertJsonPath('data.items', [])
|
||||
->assertJsonPath('data.subtotal', '0.00');
|
||||
|
||||
$this->assertDatabaseCount('carrito_items', 0);
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'stock_real' => 10,
|
||||
'stock_reservado' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_authenticated_users_reuse_the_same_cart_per_tenant_and_get_a_new_one_for_another_tenant(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$acmeVariantA = $this->createVariantForTenant('acme', 10, '10.00');
|
||||
$acmeVariantB = $this->createVariantForTenant('acme', 8, '20.00', 'hoodie');
|
||||
$globexVariant = $this->createVariantForTenant('globex', 6, '30.00');
|
||||
|
||||
$this->actingAs($user)
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $acmeVariantA->id,
|
||||
'cantidad' => 1,
|
||||
])
|
||||
->assertOk();
|
||||
|
||||
$this->actingAs($user)
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $acmeVariantB->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.subtotal', '50.00');
|
||||
|
||||
$this->actingAs($user)
|
||||
->postJson('/api/tenants/globex/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $globexVariant->id,
|
||||
'cantidad' => 1,
|
||||
])
|
||||
->assertOk();
|
||||
|
||||
$this->assertDatabaseCount('carritos', 2);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'tenant_codigo' => 'acme',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'tenant_codigo' => 'globex',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_rejects_variants_from_another_tenant(): void
|
||||
{
|
||||
$this->createVariantForTenant('acme', 10, '10.00');
|
||||
$otherVariant = $this->createVariantForTenant('globex', 10, '20.00');
|
||||
|
||||
$this->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $otherVariant->id,
|
||||
'cantidad' => 1,
|
||||
])->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_it_returns_not_found_when_the_cart_item_does_not_exist_for_update_or_delete(): void
|
||||
{
|
||||
$variant = $this->createVariantForTenant('acme', 10, '10.00');
|
||||
|
||||
$this->patchJson("/api/tenants/acme/cart/items/{$variant->id}", [
|
||||
'cantidad' => 2,
|
||||
])->assertNotFound();
|
||||
|
||||
$this->deleteJson("/api/tenants/acme/cart/items/{$variant->id}")
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
public function test_it_validates_quantity_and_stock_constraints(): void
|
||||
{
|
||||
$variant = $this->createVariantForTenant('acme', 2, '10.00');
|
||||
|
||||
$this->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'cantidad' => 0,
|
||||
])->assertUnprocessable()->assertJsonValidationErrors(['cantidad']);
|
||||
|
||||
$response = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'cantidad' => 2,
|
||||
]);
|
||||
|
||||
$guestToken = $response->getCookie('guest_token', false)?->getValue();
|
||||
$cartItemId = $response->json('data.items.0.id');
|
||||
|
||||
$response1 = $this->call(
|
||||
'POST',
|
||||
'/api/tenants/acme/cart/items',
|
||||
[],
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
|
||||
json_encode([
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'cantidad' => 1,
|
||||
])
|
||||
);
|
||||
|
||||
$response1
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['cantidad']);
|
||||
|
||||
$response2 = $this->call(
|
||||
'PATCH',
|
||||
"/api/tenants/acme/cart/items/{$cartItemId}",
|
||||
[],
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
|
||||
json_encode([
|
||||
'cantidad' => 3,
|
||||
])
|
||||
);
|
||||
|
||||
$response2
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['cantidad' => 'El máximo que se puede agregar es 2.']);
|
||||
}
|
||||
|
||||
public function test_it_only_accepts_buyable_identity_when_adding_an_item(): void
|
||||
{
|
||||
$variant = $this->createVariantForTenant('acme', 10, '10.00');
|
||||
|
||||
$this->postJson('/api/tenants/acme/cart/items', [
|
||||
'product_variant_id' => $variant->id,
|
||||
'cantidad' => 1,
|
||||
])->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['buyable_type', 'buyable_id']);
|
||||
|
||||
$createResponse = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'cantidad' => 1,
|
||||
])->assertOk();
|
||||
|
||||
$cartItemId = $createResponse->json('data.items.0.id');
|
||||
|
||||
$this->patchJson("/api/tenants/acme/cart/items/{$cartItemId}", [
|
||||
'cantidad' => 2,
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
])->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['buyable_type', 'buyable_id']);
|
||||
}
|
||||
|
||||
public function test_unlimited_inventory_can_be_reserved_updated_and_released_without_real_stock(): void
|
||||
{
|
||||
$variant = $this->createVariantForTenant(
|
||||
'acme',
|
||||
0,
|
||||
'10.00',
|
||||
'unlimited',
|
||||
InventoryPolicy::Unlimited,
|
||||
);
|
||||
|
||||
$response = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'buyable_type' => 'variant',
|
||||
'buyable_id' => $variant->id,
|
||||
'cantidad' => 100,
|
||||
])->assertOk();
|
||||
|
||||
$guestToken = $response->getCookie('guest_token', false)?->getValue();
|
||||
$cartItemId = $response->json('data.items.0.id');
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'stock_real' => 0,
|
||||
'stock_reservado' => 100,
|
||||
]);
|
||||
|
||||
$this->call(
|
||||
'PATCH',
|
||||
"/api/tenants/acme/cart/items/{$cartItemId}",
|
||||
@@ -396,13 +118,14 @@ class CartControllerTest extends TestCase
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
|
||||
json_encode(['cantidad' => 150]),
|
||||
)->assertOk();
|
||||
json_encode(['cantidad' => 5]),
|
||||
)
|
||||
->assertOk()
|
||||
->assertJsonPath('data.items.0.cantidad', 5);
|
||||
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'stock_real' => 0,
|
||||
'stock_reservado' => 150,
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 5,
|
||||
]);
|
||||
|
||||
$this->call(
|
||||
@@ -412,84 +135,143 @@ class CartControllerTest extends TestCase
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json'],
|
||||
)->assertOk();
|
||||
)
|
||||
->assertOk()
|
||||
->assertJsonPath('data.items', []);
|
||||
|
||||
$this->assertDatabaseHas('productos_variantes', [
|
||||
'id' => $variant->id,
|
||||
'stock_real' => 0,
|
||||
'stock_reservado' => 0,
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createVariantForTenant(
|
||||
string $tenantCode,
|
||||
public function test_it_requires_a_variant_when_the_item_has_variant_inventory(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
[$item] = $this->createVariantItem($tenant, 10, '10.00');
|
||||
|
||||
$this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 1,
|
||||
])->assertUnprocessable()->assertJsonValidationErrors('variant_id');
|
||||
}
|
||||
|
||||
public function test_it_rejects_a_variant_from_another_item_or_tenant(): void
|
||||
{
|
||||
$acme = $this->createTenant('acme');
|
||||
$globex = $this->createTenant('globex');
|
||||
[$acmeItem] = $this->createVariantItem($acme, 10, '10.00');
|
||||
[, $globexVariant] = $this->createVariantItem($globex, 10, '20.00');
|
||||
|
||||
$this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $acmeItem->id,
|
||||
'variant_id' => $globexVariant->id,
|
||||
'cantidad' => 1,
|
||||
])->assertUnprocessable()->assertJsonValidationErrors('variant_id');
|
||||
}
|
||||
|
||||
public function test_unlimited_inventory_can_be_reserved_and_released(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$item = $this->createDirectItem(
|
||||
$tenant,
|
||||
0,
|
||||
'10.00',
|
||||
InventoryPolicy::Unlimited,
|
||||
);
|
||||
|
||||
$response = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 100,
|
||||
])->assertOk();
|
||||
|
||||
$guestToken = $response->getCookie('guest_token', false)?->getValue();
|
||||
$cartItemId = $response->json('data.items.0.id');
|
||||
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $item->inventory_id,
|
||||
'real_stock' => 0,
|
||||
'reserved_stock' => 100,
|
||||
]);
|
||||
|
||||
$this->call(
|
||||
'DELETE',
|
||||
"/api/tenants/acme/cart/items/{$cartItemId}",
|
||||
[],
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json'],
|
||||
)
|
||||
->assertOk();
|
||||
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $item->inventory_id,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createDirectItem(
|
||||
Tenant $tenant,
|
||||
int $stock,
|
||||
string $price,
|
||||
string $slugPrefix = 'shirt',
|
||||
InventoryPolicy $inventoryPolicy = InventoryPolicy::Tracked,
|
||||
): ProductVariant {
|
||||
$tenant = Tenant::query()->where('codigo', $tenantCode)->first();
|
||||
if (! $tenant) {
|
||||
$this->createTenant($tenantCode, ucfirst($tenantCode), "{$tenantCode}.com");
|
||||
}
|
||||
InventoryPolicy $policy = InventoryPolicy::Tracked,
|
||||
): CatalogItem {
|
||||
$inventory = Inventory::query()->create(['real_stock' => $stock]);
|
||||
|
||||
$category = Category::query()->create([
|
||||
'tenant_code' => $tenantCode,
|
||||
'nombre' => "{$slugPrefix} category {$tenantCode}",
|
||||
]);
|
||||
|
||||
$product = Product::query()->create([
|
||||
'tenant_codigo' => $tenantCode,
|
||||
'categoria_id' => $category->id,
|
||||
'slug' => "{$slugPrefix}-{$tenantCode}-".Product::query()->count(),
|
||||
'nombre' => ucfirst($slugPrefix)." {$tenantCode}",
|
||||
'descripcion' => 'Test product',
|
||||
return CatalogItem::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'inventory_id' => $inventory->id,
|
||||
'slug' => 'item-'.$tenant->codigo.'-'.CatalogItem::query()->count(),
|
||||
'nombre' => 'Item '.$tenant->codigo,
|
||||
'precio' => $price,
|
||||
'inventory_policy' => $policy,
|
||||
]);
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
|
||||
/** @return array{CatalogItem, Variant} */
|
||||
private function createVariantItem(Tenant $tenant, int $stock, string $price): array
|
||||
{
|
||||
$hdrKey = (string) Str::uuid();
|
||||
$ftrKey = (string) Str::uuid();
|
||||
$item = CatalogItem::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'inventory_id' => null,
|
||||
'slug' => 'variant-item-'.$tenant->codigo.'-'.CatalogItem::query()->count(),
|
||||
'nombre' => 'Variant item '.$tenant->codigo,
|
||||
'precio' => $price,
|
||||
'inventory_policy' => InventoryPolicy::Tracked,
|
||||
]);
|
||||
$inventory = Inventory::query()->create(['real_stock' => $stock]);
|
||||
$variant = $item->variants()->create(['inventory_id' => $inventory->id]);
|
||||
|
||||
$headerAttachment = Attachment::create([
|
||||
'key' => $hdrKey,
|
||||
'path' => 'tenants/'.$hdrKey.'.png',
|
||||
'filename' => 'logo_header.png',
|
||||
return [$item, $variant];
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment("{$code}-header");
|
||||
$footerLogo = $this->createAttachment("{$code}-footer");
|
||||
|
||||
return Tenant::query()->create([
|
||||
'codigo' => $code,
|
||||
'nombre' => ucfirst($code),
|
||||
'dominio' => "{$code}.local",
|
||||
'primary_color' => '#000000',
|
||||
'secondary_color' => '#000000',
|
||||
'danger_color' => '#000000',
|
||||
'success_color' => '#000000',
|
||||
'header_bg_color' => '#000000',
|
||||
'footer_bg_color' => '#000000',
|
||||
'header_logo_id' => $headerLogo->id,
|
||||
'footer_logo_id' => $footerLogo->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createAttachment(string $name): Attachment
|
||||
{
|
||||
return Attachment::query()->create([
|
||||
'path' => "test/{$name}.png",
|
||||
'filename' => "{$name}.png",
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
$footerAttachment = Attachment::create([
|
||||
'key' => $ftrKey,
|
||||
'path' => 'tenants/'.$ftrKey.'.png',
|
||||
'filename' => 'logo_footer.png',
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
|
||||
return Tenant::create([
|
||||
'codigo' => $codigo,
|
||||
'nombre' => $nombre,
|
||||
'dominio' => $dominio,
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'success_color' => '#28a745',
|
||||
'header_bg_color' => '#444444',
|
||||
'footer_bg_color' => '#444444',
|
||||
'header_logo_id' => $headerAttachment->id,
|
||||
'footer_logo_id' => $footerAttachment->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user