Squashed commit of the following:
commitcd99a6489bMerge:ebcbbb1e63a036Author: ncoronel <ncoronel@quo.ar> Date: Wed Aug 26 10:12:44 2026 -0300 Merge branch 'fix/simplify_stock_reservations' of https://gitea.quo.ar/tbianchini/shopit-back into fix/simplify_stock_reservations commitebcbbb1774Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:33:39 2026 -0300 fix(cart): reconcile expired cart mutations commit3dd378ed06Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:15 2026 -0300 docs(stock): describe associated entity expiration commit4f705175bdAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:14 2026 -0300 test(stock): cover propagated reservation expiration commit82570fe941Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:09:52 2026 -0300 refactor(stock): propagate reservation expiration commitd2a2b323e0Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 docs(stock): document terminal reservation recovery commitec67623b7bAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 test(stock): cover terminal reservation lifecycle commitf6f138e180Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:41 2026 -0300 refactor(stock): make expired reservations terminal commitd612b7a118Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:30 2026 -0300 docs(stock): describe unified expiration ownership commit49f42d4507Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:23 2026 -0300 test(stock): cover authoritative reservation expiration commit1881cc1d4bAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:17 2026 -0300 refactor(stock): make reservation expiration authoritative commit00ec37d8a0Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:34 2026 -0300 docs(stock): document centralized reservation flow commitd9f374c718Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:29 2026 -0300 test(stock): cover aggregate reservation lifecycle commitbbfdf8f342Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:23 2026 -0300 refactor(stock): centralize reservation aggregate commit8a0f29bdaeAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:33:39 2026 -0300 fix(cart): reconcile expired cart mutations commit198ed400a9Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:15 2026 -0300 docs(stock): describe associated entity expiration commit23d83166e6Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:14 2026 -0300 test(stock): cover propagated reservation expiration commitcb090402d0Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:09:52 2026 -0300 refactor(stock): propagate reservation expiration commitc120b6f7c0Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 docs(stock): document terminal reservation recovery commit1106fa28b4Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 test(stock): cover terminal reservation lifecycle commit896fb81bcfAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:41 2026 -0300 refactor(stock): make expired reservations terminal commit8545fbc645Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:30 2026 -0300 docs(stock): describe unified expiration ownership commitdd6dcb708aAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:23 2026 -0300 test(stock): cover authoritative reservation expiration commit624b246ff5Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:17 2026 -0300 refactor(stock): make reservation expiration authoritative commit007b95b904Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:34 2026 -0300 docs(stock): document centralized reservation flow commit0b3c8c93beAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:29 2026 -0300 test(stock): cover aggregate reservation lifecycle commitede718e448Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:23 2026 -0300 refactor(stock): centralize reservation aggregate commite63a03601aAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:33:39 2026 -0300 fix(cart): reconcile expired cart mutations commit389373ad8cAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:15 2026 -0300 docs(stock): describe associated entity expiration commitace02a3133Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:10:14 2026 -0300 test(stock): cover propagated reservation expiration commitd69a6210f6Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 16:09:52 2026 -0300 refactor(stock): propagate reservation expiration commit3c0b43fea3Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 docs(stock): document terminal reservation recovery commit9b0008626bAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:47 2026 -0300 test(stock): cover terminal reservation lifecycle commit61861e331aAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:58:41 2026 -0300 refactor(stock): make expired reservations terminal commit2cc9d7dd97Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:30 2026 -0300 docs(stock): describe unified expiration ownership commitf982bcead1Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:23 2026 -0300 test(stock): cover authoritative reservation expiration commit7a85e1731dAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:28:17 2026 -0300 refactor(stock): make reservation expiration authoritative commit6db99e775aAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:34 2026 -0300 docs(stock): document centralized reservation flow commite5f7ba3615Author: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:29 2026 -0300 test(stock): cover aggregate reservation lifecycle commit24bfef431bAuthor: ncoronel <ncoronel@quo.ar> Date: Tue Aug 25 15:05:23 2026 -0300 refactor(stock): centralize reservation aggregate
This commit is contained in:
@@ -5,10 +5,12 @@ 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\Cart\Models\Cart;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Catalog\Services\CatalogService;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Models\PurchaseItem;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
@@ -28,6 +30,81 @@ class CartControllerTest extends TestCase
|
||||
]));
|
||||
$this->assertFalse(Schema::hasColumn('carrito_items', 'buyable_type'));
|
||||
$this->assertFalse(Schema::hasColumn('carrito_items', 'buyable_id'));
|
||||
$this->assertTrue(Schema::hasColumns('stock_reservations', [
|
||||
'status',
|
||||
'expires_at',
|
||||
'committed_at',
|
||||
'released_at',
|
||||
'expired_at',
|
||||
'release_reason',
|
||||
]));
|
||||
$this->assertFalse(Schema::hasColumn('stock_reservations', 'quantity'));
|
||||
$this->assertTrue(Schema::hasColumns('stock_reservation_lines', [
|
||||
'stock_reservation_id',
|
||||
'inventory_id',
|
||||
'quantity',
|
||||
'tracks_inventory',
|
||||
]));
|
||||
$this->assertTrue(Schema::hasColumn('carritos', 'current_stock_reservation_id'));
|
||||
$this->assertTrue(Schema::hasColumn('compras', 'stock_reservation_id'));
|
||||
$this->assertFalse(Schema::hasColumn('compras', 'expires_at'));
|
||||
}
|
||||
|
||||
public function test_it_aggregates_shared_inventory_into_one_cart_reservation_line(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$firstItem = $this->createDirectItem($tenant, 10, '10.00');
|
||||
$secondItem = app(CatalogService::class)->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'type' => 'bundle',
|
||||
'slug' => 'second-shared-item',
|
||||
'nombre' => 'Second shared item',
|
||||
'precio' => '20.00',
|
||||
'components' => [[
|
||||
'catalog_item_id' => $firstItem->id,
|
||||
'quantity' => 1,
|
||||
]],
|
||||
]);
|
||||
|
||||
$firstResponse = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $firstItem->id,
|
||||
'cantidad' => 2,
|
||||
])->assertOk();
|
||||
$guestToken = $firstResponse->getCookie('guest_token', false)?->getValue();
|
||||
|
||||
$this->call(
|
||||
'POST',
|
||||
'/api/tenants/acme/cart/items',
|
||||
[],
|
||||
['guest_token' => $guestToken],
|
||||
[],
|
||||
['HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'],
|
||||
json_encode([
|
||||
'catalog_item_id' => $secondItem->id,
|
||||
'cantidad' => 3,
|
||||
]),
|
||||
)->assertOk();
|
||||
|
||||
$reservationId = (int) $firstResponse->json('data.id');
|
||||
$reservationId = (int) Cart::query()
|
||||
->findOrFail($reservationId)
|
||||
->current_stock_reservation_id;
|
||||
|
||||
$this->assertDatabaseCount('stock_reservations', 1);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'id' => $reservationId,
|
||||
'status' => 'active',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'stock_reservation_id' => $reservationId,
|
||||
'inventory_id' => $firstItem->inventory_id,
|
||||
'quantity' => 5,
|
||||
]);
|
||||
$this->assertDatabaseCount('stock_reservation_lines', 1);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $firstItem->inventory_id,
|
||||
'reserved_stock' => 5,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_adds_a_catalog_item_without_a_variant(): void
|
||||
@@ -74,9 +151,11 @@ class CartControllerTest extends TestCase
|
||||
'id' => $item->inventory_id,
|
||||
'reserved_stock' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'quantity' => 2,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'status' => 'active',
|
||||
]);
|
||||
}
|
||||
@@ -96,38 +175,44 @@ class CartControllerTest extends TestCase
|
||||
])->assertOk();
|
||||
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'quantity' => 2,
|
||||
'status' => 'active',
|
||||
'expires_at' => $now->copy()->addMinutes(45)->toDateTimeString(),
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'quantity' => 2,
|
||||
]);
|
||||
|
||||
$this->travelBack();
|
||||
}
|
||||
|
||||
public function test_it_expires_abandoned_cart_reservations_and_removes_empty_carts(): void
|
||||
public function test_it_expires_a_cart_reservation_and_automatically_replaces_the_cart(): void
|
||||
{
|
||||
config()->set('catalog.stock_reservation_expiration_minutes', 30);
|
||||
$tenant = $this->createTenant('acme');
|
||||
$user = User::factory()->create();
|
||||
$item = $this->createDirectItem($tenant, 10, '49.90');
|
||||
|
||||
$response = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
])->assertOk();
|
||||
$response = $this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
])->assertOk();
|
||||
$cartId = $response->json('data.id');
|
||||
$cartItemId = $response->json('data.items.0.id');
|
||||
$cart = Cart::query()->findOrFail($cartId);
|
||||
$reservationId = $cart->current_stock_reservation_id;
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->travel(31)->minutes();
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
->expectsOutput('Expired cart items: 1')
|
||||
->expectsOutput('Expired cart reservations: 1')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
@@ -135,28 +220,141 @@ class CartControllerTest extends TestCase
|
||||
'real_stock' => 10,
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
$this->assertDatabaseMissing('carrito_items', ['id' => $cartItemId]);
|
||||
$this->assertSoftDeleted('carritos', [
|
||||
$this->assertDatabaseHas('carrito_items', ['id' => $cartItemId]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cartId,
|
||||
'status' => 'expired',
|
||||
'status' => Cart::STATUS_EXPIRED,
|
||||
'current_stock_reservation_id' => $reservationId,
|
||||
'deleted_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'cart_item_id' => null,
|
||||
'purchase_id' => null,
|
||||
'quantity' => 0,
|
||||
'id' => $reservationId,
|
||||
'status' => 'expired',
|
||||
'expires_at' => null,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $item->inventory_id,
|
||||
'quantity' => 2,
|
||||
]);
|
||||
|
||||
$currentCart = $this->getJson('/api/tenants/acme/cart')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Cart::STATUS_ACTIVE)
|
||||
->assertJsonPath('data.items', [])
|
||||
->assertJsonMissingPath('data.stock_reservation')
|
||||
->assertJsonMissingPath('data.current_stock_reservation_id');
|
||||
$newCartId = $currentCart->json('data.id');
|
||||
|
||||
$this->assertNotSame($cartId, $newCartId);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $cartId,
|
||||
'status' => Cart::STATUS_ABANDONED,
|
||||
'current_stock_reservation_id' => $reservationId,
|
||||
]);
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $newCartId,
|
||||
'status' => Cart::STATUS_ACTIVE,
|
||||
'current_stock_reservation_id' => null,
|
||||
]);
|
||||
|
||||
$this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 1,
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.id', $newCartId)
|
||||
->assertJsonPath('data.items.0.cantidad', 1);
|
||||
|
||||
$this->artisan('reservations:expire')
|
||||
->expectsOutput('Expired purchases: 0')
|
||||
->expectsOutput('Expired cart items: 0')
|
||||
->expectsOutput('Expired cart reservations: 0')
|
||||
->assertSuccessful();
|
||||
|
||||
$this->travelBack();
|
||||
}
|
||||
|
||||
public function test_it_replaces_an_overdue_cart_before_the_expiration_job_runs(): void
|
||||
{
|
||||
config()->set('catalog.stock_reservation_expiration_minutes', 30);
|
||||
$tenant = $this->createTenant('acme');
|
||||
$user = User::factory()->create();
|
||||
$item = $this->createDirectItem($tenant, 10, '49.90');
|
||||
|
||||
$original = $this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
])->assertOk();
|
||||
$originalCartId = $original->json('data.id');
|
||||
|
||||
$this->travel(31)->minutes();
|
||||
|
||||
$replacement = $this->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 1,
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.status', Cart::STATUS_ACTIVE)
|
||||
->assertJsonPath('data.items.0.cantidad', 1);
|
||||
|
||||
$this->assertNotSame($originalCartId, $replacement->json('data.id'));
|
||||
$this->assertDatabaseHas('carritos', [
|
||||
'id' => $originalCartId,
|
||||
'status' => Cart::STATUS_ABANDONED,
|
||||
]);
|
||||
$this->assertDatabaseHas('inventories', [
|
||||
'id' => $item->inventory_id,
|
||||
'reserved_stock' => 1,
|
||||
]);
|
||||
|
||||
$this->travelBack();
|
||||
}
|
||||
|
||||
public function test_expired_cart_mutations_return_the_expiration_error_instead_of_not_found(): void
|
||||
{
|
||||
config()->set('catalog.stock_reservation_expiration_minutes', 30);
|
||||
$tenant = $this->createTenant('acme');
|
||||
$user = User::factory()->create();
|
||||
[$item, $firstVariant] = $this->createVariantItem($tenant, 10, '49.90');
|
||||
$secondInventory = Inventory::query()->create(['real_stock' => 10]);
|
||||
$secondVariant = $item->variants()->create(['inventory_id' => $secondInventory->id]);
|
||||
|
||||
$cartItemId = $this->actingAs($user, 'sanctum')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'variant_id' => $firstVariant->id,
|
||||
'cantidad' => 2,
|
||||
])
|
||||
->assertOk()
|
||||
->json('data.items.0.id');
|
||||
|
||||
$this->travel(31)->minutes();
|
||||
|
||||
$expectedError = [
|
||||
'code' => 'stock_reservation.expired',
|
||||
'message' => __('api.cart.reservation_expired'),
|
||||
];
|
||||
|
||||
$this->patchJson("/api/tenants/acme/cart/items/{$cartItemId}", [
|
||||
'cantidad' => 3,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertExactJson($expectedError);
|
||||
|
||||
$this->patchJson("/api/tenants/acme/cart/items/{$cartItemId}", [
|
||||
'cantidad' => 2,
|
||||
'variant_id' => $secondVariant->id,
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertExactJson($expectedError);
|
||||
|
||||
$this->deleteJson("/api/tenants/acme/cart/items/{$cartItemId}")
|
||||
->assertUnprocessable()
|
||||
->assertExactJson($expectedError);
|
||||
|
||||
$this->travelBack();
|
||||
}
|
||||
|
||||
public function test_it_filters_item_images_when_the_tenant_disables_them(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
@@ -212,10 +410,11 @@ class CartControllerTest extends TestCase
|
||||
'id' => $variant->inventory_id,
|
||||
'reserved_stock' => 5,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => $response->json('data.items.0.id'),
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 5,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'status' => 'active',
|
||||
]);
|
||||
}
|
||||
@@ -229,6 +428,7 @@ class CartControllerTest extends TestCase
|
||||
$this->createPurchaseItem($tenant, $user, $item, 1);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->withHeader('Accept-Language', 'es')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
@@ -237,6 +437,7 @@ class CartControllerTest extends TestCase
|
||||
->assertJsonPath('data.items.0.cantidad', 2);
|
||||
|
||||
$this->actingAs($user, 'sanctum')
|
||||
->withHeader('Accept-Language', 'es')
|
||||
->postJson('/api/tenants/acme/cart/items', [
|
||||
'catalog_item_id' => $item->id,
|
||||
'cantidad' => 2,
|
||||
@@ -381,10 +582,12 @@ class CartControllerTest extends TestCase
|
||||
'reserved_stock' => 0,
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservations', [
|
||||
'cart_item_id' => null,
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 0,
|
||||
'status' => 'released',
|
||||
'release_reason' => 'cart_empty',
|
||||
]);
|
||||
$this->assertDatabaseHas('stock_reservation_lines', [
|
||||
'inventory_id' => $variant->inventory_id,
|
||||
'quantity' => 5,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user