Registry Mirror
Skip to content

Conversation

@grandnode-dev

@grandnode-dev grandnode-dev commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Type: bugfix

Issue

The checkout claims stock, limited discounts, gift vouchers and loyalty points before the payment (CheckoutClaimService), but not reservation slots. PlaceOrderCommandHandler.UpdateProductReservation booked them only after the payment and after InsertOrder: it read the vacant slots and wrote them unconditionally.

  • Two customers with the same slot in their carts who confirm at the same time could both get an order for it.
  • If the slot (or, for a day range, every free resource) was gone meanwhile, throw new Exception("ShoppingCart.Reservation.NoFreeReservationsInThisPeriod") fired after the money was taken. The result was a paid order without a slot, and the customer saw the resource key.
  • A reservation cart line with a quantity above 1 booked one slot but was charged for the whole quantity.

To reproduce on develop: two customers put the same hour slot in their carts and both go to the confirm step. Then confirm both at the same moment (two scripted ConfirmOrder requests). Sometimes both validations pass before either order books the slot.

Solution

Booking (IProductReservationService.TryBook(product, shoppingCartItem, orderId))

  • Books the slots of one cart item under IResourceLock keyed per product (ProductReservation:{productId}). For an hour/minute product that is the slot named by ReservationId. For a date range it is the slots held for the cart item when it was added (CustomerReservationsHelper) if they are still vacant, else the first resource free for the whole period (FindGroupToBook + InRentalPeriod), read within the rental period (±1 day) and in date order, the way the cart sees them.
  • Returns null without writing anything if a slot is not vacant, belongs to another product, or the item names no slot.
  • The slots are written in one UpdateManyAsync; their EntityUpdated events are published after the lock is released.
  • CancelReservationsByOrderId now publishes EntityUpdated for each slot it frees (also on order cancel and delete).

Claim (CheckoutClaimService)

  • ClaimReservations runs after ClaimStock for each reservation line and books with details.OrderId, the id the order is inserted with. It reuses the products ClaimStock loaded.
  • CheckoutClaim.ReservationSlotsBooked is set before the first write, so Release frees the slots (CancelReservationsByOrderId(claim.OrderId)) even when a write failed partway.
  • CheckoutClaim.Refusal names why a refused claim was refused. A reservation sets ShoppingCart.Reservation.NoFreeReservationsInThisPeriod (date range) or ShoppingCart.Reservation.AlreadyReserved (slot); PlaceOrderCommandHandler shows it instead of the generic ShoppingCart.ChangedDuringCheckout, which stays for every other refusal.
  • UpdateProductReservation now only removes the customer's CustomerReservationsHelper rows. There is no second write after the payment and no temporary OrderGuid in OrderId.

Quantity

  • ShoppingCartReservationProductValidator refuses a reservation cart item with a quantity other than 1 (ShoppingCart.Reservation.QuantityMustBeOne). It runs on add to cart, on a quantity change and at order placement.

Departure from the plan: the plan suggested a conditional single update (Id == x && OrderId empty) that reports the matched count, added to Grand.Data if missing. IRepository<T> has no such method. The four existing claims (TryReserve, TryInsertDiscountUsageHistory, TryRedeem, TryRedeemLoyaltyPoints) all use IResourceLock plus read and write instead, and the checkout is the only code that books a ProductReservation. So I followed that pattern and did not add a method to the repository interface and its implementations. It works the same on MongoDB and LiteDB.

One new resource, ShoppingCart.Reservation.QuantityMustBeOne, in DefaultLanguage.xml and Upgrade/en_250.xml. No settings or schema changes.

Breaking changes

  • ProductReservationService and CheckoutClaimService constructors take one more dependency (IResourceLock / IProductReservationService). DI resolves both. Code that constructs them by hand must pass it.
  • IProductReservationService gains TryBook. Custom implementations must add it.
  • CheckoutClaim gains ReservationSlotsBooked and Refusal.
  • A reservation cart line with no slot to book (no ReservationId and no complete date range) now fails the checkout instead of producing an order without a slot. The cart validator already rejects such lines, so this matters only for crafted carts.
  • A reservation line with a quantity above 1 already in a cart now shows a warning and blocks the checkout until the quantity is 1.

Known limitation

Holding on to the slots held in the cart has no effect yet when the server is not on UTC. That is the date shift described in R7 of the reservations roadmap: the date-picker value has no kind, the cart item is stored as local time converted to UTC, and the checkout counts the nights from the UTC date (12/10–12/12 posted on a CET server → 2026-12-09T23:00Z, nights 9 and 10). The booking then falls back to the first free resource, as before this PR. The shift itself is on develop already and is not changed here.

Testing

  1. Unit tests: dotnet test src/Tests/Grand.Business.Catalog.Tests (ProductReservationServiceTests, 25) covers TryBook: vacant slot booked; taken slot, slot of another product, the same slot twice and an item with nothing to book are refused; a day range books one resource, moves to the next resource when one day is taken, books the held resource, falls back when a held slot is taken, takes the resource first by date, and is refused when no resource is free; events per slot on booking and on CancelReservationsByOrderId. dotnet test src/Tests/Grand.Business.Checkout.Tests (1138) covers CheckoutClaimServiceTests: slots claimed; a slot gone refuses the claim, gives the stock back and names the reason; a booking that throws partway is given back; a stock refusal names no reason; release frees the slots and leaves them alone when none were booked. It also covers PlaceOrderCommandHandlerTests (no slot written after the payment; a named refusal is shown) and the quantity rule in ShoppingCartValidatorsTests.
  2. E2E with Playwright on the shared reservations database, restored from its snapshot (server in CET, Cash on delivery):
    • Hour slot order: the slot's OrderId is the order's _id, and the customer's reservation helpers are gone.
    • Race: two customers with the same hour slot confirm at the same moment. Exactly one order is placed and the slot belongs to it. The other customer stays on the checkout with "Already reserved", and the payment transaction carries that error, so the claim refused it and nothing was paid.
    • Quantity: add to cart with quantity 2 and a quantity change to 3 are refused, and the cart item keeps quantity 1. (On a database already stamped 2.5 the en_250 migration does not run, so the message shows the resource key there; a new install has the text.)
    • Cancel in the admin: three orders cancelled, all their slots vacant again.
    • Day range on the tent: reproduces the R7 shift above (the order got nights 9–10 for a 10–12 range), which is unchanged by this PR.

The slots of a reservation cart item were booked after the payment and
after the order insert, from a read of vacant slots and an unconditional
write. Two checkouts could book the same slot, and a slot gone meanwhile
threw after the money was taken.

CheckoutClaimService now books the slots with the stock, under a lock
per product, with the order's id; a slot no longer vacant refuses the
claim (ShoppingCart.ChangedDuringCheckout) before any payment, and a
claim without an inserted order frees the slots again. Order placement
only removes the cart's reservation helpers.
Copilot AI balanced review requested due to automatic review settings November 29, 2026 12:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

private Mock<IInventoryManageService> _inventoryManageServiceMock;
private Mock<ILoyaltyPointsService> _loyaltyPointsServiceMock;
private Mock<IProductService> _productServiceMock;
private Mock<IProductReservationService> _productReservationServiceMock;
support added 3 commits November 29, 2026 14:45
The claim gave back reservation slots only when it had stored the booked
list, which happens after the write returns; a write that failed partway
left slots carrying an order id no order holds. The claim now marks the
slots before the first write, so Release frees them whatever happened.

A refused reservation now tells the buyer why (no free booking in the
period, or the slot is already reserved) instead of the generic
"changed during checkout", and the products the stock claim loaded are
reused instead of being read again.
TryBook searched every vacant slot of the product again, unordered, so
it could pick another resource than the one held when the item was
added. It now books the held slots when they are still vacant and falls
back to the first free resource, read within the rental period and in
date order like the cart. The slots are written in one update and their
events published after the lock is released.

Giving slots back by order id now publishes an event per slot as well.
A reservation cart item books one slot, or one resource for its period,
but the order charged its whole quantity. The reservation validator now
refuses any other quantity, on add to cart, on a quantity change and at
order placement (ShoppingCart.Reservation.QuantityMustBeOne).
@grandnode-dev
grandnode-dev merged commit 9c8adc2 into develop Nov 29, 2026
8 of 9 checks passed
@grandnode-dev
grandnode-dev deleted the fix/reservation-checkout-claim branch November 29, 2026 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants