Conversation
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.
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type: bugfix
Issue
The checkout claims stock, limited discounts, gift vouchers and loyalty points before the payment (
CheckoutClaimService), but not reservation slots.PlaceOrderCommandHandler.UpdateProductReservationbooked them only after the payment and afterInsertOrder: it read the vacant slots and wrote them unconditionally.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.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 scriptedConfirmOrderrequests). Sometimes both validations pass before either order books the slot.Solution
Booking (
IProductReservationService.TryBook(product, shoppingCartItem, orderId))IResourceLockkeyed per product (ProductReservation:{productId}). For an hour/minute product that is the slot named byReservationId. 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.nullwithout writing anything if a slot is not vacant, belongs to another product, or the item names no slot.UpdateManyAsync; theirEntityUpdatedevents are published after the lock is released.CancelReservationsByOrderIdnow publishesEntityUpdatedfor each slot it frees (also on order cancel and delete).Claim (
CheckoutClaimService)ClaimReservationsruns afterClaimStockfor each reservation line and books withdetails.OrderId, the id the order is inserted with. It reuses the productsClaimStockloaded.CheckoutClaim.ReservationSlotsBookedis set before the first write, soReleasefrees the slots (CancelReservationsByOrderId(claim.OrderId)) even when a write failed partway.CheckoutClaim.Refusalnames why a refused claim was refused. A reservation setsShoppingCart.Reservation.NoFreeReservationsInThisPeriod(date range) orShoppingCart.Reservation.AlreadyReserved(slot);PlaceOrderCommandHandlershows it instead of the genericShoppingCart.ChangedDuringCheckout, which stays for every other refusal.UpdateProductReservationnow only removes the customer'sCustomerReservationsHelperrows. There is no second write after the payment and no temporaryOrderGuidinOrderId.Quantity
ShoppingCartReservationProductValidatorrefuses 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 toGrand.Dataif missing.IRepository<T>has no such method. The four existing claims (TryReserve,TryInsertDiscountUsageHistory,TryRedeem,TryRedeemLoyaltyPoints) all useIResourceLockplus read and write instead, and the checkout is the only code that books aProductReservation. 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, inDefaultLanguage.xmlandUpgrade/en_250.xml. No settings or schema changes.Breaking changes
ProductReservationServiceandCheckoutClaimServiceconstructors take one more dependency (IResourceLock/IProductReservationService). DI resolves both. Code that constructs them by hand must pass it.IProductReservationServicegainsTryBook. Custom implementations must add it.CheckoutClaimgainsReservationSlotsBookedandRefusal.ReservationIdand 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.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/12posted 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 ondevelopalready and is not changed here.Testing
dotnet test src/Tests/Grand.Business.Catalog.Tests(ProductReservationServiceTests, 25) coversTryBook: 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 onCancelReservationsByOrderId.dotnet test src/Tests/Grand.Business.Checkout.Tests(1138) coversCheckoutClaimServiceTests: 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 coversPlaceOrderCommandHandlerTests(no slot written after the payment; a named refusal is shown) and the quantity rule inShoppingCartValidatorsTests.OrderIdis the order's_id, and the customer's reservation helpers are gone.en_250migration does not run, so the message shows the resource key there; a new install has the text.)