Skip to content

Add item reservation across checkout, payment, and order status - #11

Open
ivanmilevtues wants to merge 2 commits into
mainfrom
demo/item-reservation
Open

ivanmilevtues wants to merge 2 commits into
mainfrom
demo/item-reservation

Conversation

@ivanmilevtues

Copy link
Copy Markdown
Member

Item reservation across checkout, payment, and order status

Checkout now reserves every requested item atomically for ten minutes. Timely payment completes the reservation; cancellation, failed payment, or expiry releases it exactly once. Successful payment after release produces a visible Payment review needed state and blocks fulfillment.

This is a new lecture/demo example based on main, independent of PR #8. Customer-facing wording and new components use item reservation. Existing internal StockConfirmed wire names remain for compatibility.

System changes

  • Catalog owns persisted reservation state and quantities, serializes concurrent requests, and prevents stale catalog edits from overwriting allocations.
  • Ordering waits for Catalog's completion outcome before marking an order paid. Cancellation is a request, with buyer ownership checked at the API boundary.
  • Reservation expiry and durable outbox workers recover after restarts. RabbitMQ publishing uses confirmations; failed handlers requeue rather than being acknowledged and lost.
  • WebApp displays deadlines and lifecycle outcomes, and offers a cancellation confirmation form. Payment simulation has configurable delay and failure scenarios.
  • Shared event contracts make the new service boundaries explicit for the automatically generated CodeBoarding review. No generated graph files were edited.

Demo

ItemReservationDemo=late-payment aspire run shows a 15-second reservation followed by successful simulated payment after 25 seconds. Other scenarios cover completion, cancellation, failed payment, and expiry.

Demo instructions and architecture decisions · Presenter walkthrough

Validation

  • Full eShop.Web.slnf build passed.
  • 17 real-PostgreSQL reservation tests passed: competing checkouts, partial availability, duplicate messages, cancellation before reservation, expiry/payment races, reallocation, persisted recovery, outbox retry, stale catalog edits, and migrations.
  • Ordering unit tests and the existing application, basket, catalog, webhook, and AppHost suites passed; the ordering functional suite passed after updating its expected response for cancellation of a nonexistent order (now a client error).
  • Full browser checkout has not been validated; a browser smoke attempt stopped at an existing product-link selector before checkout.

Review decisions

This remains a demo with a simulated payment provider. Late payment is surfaced for human review; automated refund/void handling is deliberately outside this feature. Production adoption needs a coordinated migration for in-flight orders, bounded message retries/dead-letter handling, clock synchronization, and outbox/tombstone retention. The guide explains those trade-offs rather than treating green tests as approval to ship.

@chatgpt-codex-connector

Copy link
Copy Markdown

💡 Codex Review

// Existing catalog editing must not overwrite a quantity read before a reservation.
builder.Property(ci => ci.AvailableStock).IsConcurrencyToken();

P1 Badge Protect reservations against stale catalog update payloads

When a client reads a product before checkout reserves it and submits that product afterward, this concurrency token does not prevent overwriting the allocation. CatalogApi.UpdateItem loads the current database row and then calls CurrentValues.SetValues(productToUpdate), so EF compares against the freshly loaded stock, not the stock originally read by the client. For example, a stale payload containing AvailableStock = 1 successfully restores availability after the last unit has been reserved, allowing another checkout to reserve it. Exclude availability from ordinary product updates or validate a client-supplied version before replacing it.


// Retain the item identity until release/completion; deleting it must not lose held units.
entity.HasOne<CatalogItem>().WithMany().HasForeignKey(x => x.ProductId).OnDelete(DeleteBehavior.Restrict);

P2 Badge Release product references when reservations become terminal

After a product participates in any successful reservation, this restrictive foreign key prevents deleting it even after payment completes or the reservation releases its quantities. Neither terminal transition removes the ReservedItem rows, and a repository-wide search found no cleanup path. Consequently, the existing CatalogApi.DeleteItemById endpoint now throws a foreign-key violation instead of deleting such products. Retain the restriction for active allocations, but remove terminal item references or otherwise allow terminal reservation history to survive product deletion.


var transactions = await context.Set<IntegrationEventLogEntry>().AsNoTracking()
.Where(x => x.State != EventStateEnum.Published && x.CreationTime < cutoff)
.Select(x => x.TransactionId).Distinct().Take(100).ToArrayAsync(stoppingToken);
foreach (var transaction in transactions)
await events.PublishEventsThroughEventBusAsync(transaction);

P2 Badge Make basket clearing safe before replaying checkout events

If Ordering exits after publishing OrderStartedIntegrationEvent but before marking its log entry published, this worker republishes it after restart. The existing Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs unconditionally calls DeleteBasketAsync(@event.UserId) without deduplicating the event or identifying the checked-out basket. If the customer has started another basket before recovery, replay deletes those new items. This worker retries all integration events, not just reservation commands, so basket clearing needs an event-id or basket-version guard before enabling this replay path.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

CodeBoarding review

Status: 13 changed components

See the full change in CodeBoarding.

graph LR
    n_Mobile_and_Desktop_Client["Mobile and Desktop Client"]
    n_Order_Management_Service["Order Management Service"]
    n_Catalog_and_Identity_Services["Catalog and Identity Services"]
    n_Web_and_Hybrid_Frontend["Web and Hybrid Frontend"]
    n_Webhook_Notification_Service["Webhook Notification Service"]
    n_Integration_Event_Bus["Integration Event Bus"]
    n_Shopping_Basket_Service["Shopping Basket Service"]
    n_Host_and_Service_Defaults["Host and Service Defaults"]
    n_Payment_Background_Processor["Payment Background Processor"]
    n_Order_Management_Service -- "persists integration event logs in" --> n_Catalog_and_Identity_Services
    n_Order_Management_Service -- "publishes order events to" --> n_Integration_Event_Bus
    n_Order_Management_Service -- "configures defaults using" --> n_Host_and_Service_Defaults
    n_Catalog_and_Identity_Services -- "dispatches item reservation contracts to" --> n_Order_Management_Service
    n_Catalog_and_Identity_Services -- "publishes catalog events to" --> n_Integration_Event_Bus
    n_Web_and_Hybrid_Frontend -- "subscribes to events through" --> n_Integration_Event_Bus
    n_Web_and_Hybrid_Frontend -- "applies configuration and telemetry from" --> n_Host_and_Service_Defaults
    n_Webhook_Notification_Service -- "migrates database using" --> n_Catalog_and_Identity_Services
    n_Webhook_Notification_Service -- "subscribes to domain events through" --> n_Integration_Event_Bus
    n_Webhook_Notification_Service -- "consumes service defaults from" --> n_Host_and_Service_Defaults
    n_Integration_Event_Bus -- "dispatches integration events to" --> n_Order_Management_Service
    n_Integration_Event_Bus -- "pushes status events to" --> n_Web_and_Hybrid_Frontend
    n_Integration_Event_Bus -- "triggers webhook handlers via" --> n_Webhook_Notification_Service
    n_Integration_Event_Bus -- "notifies basket of order creation via" --> n_Shopping_Basket_Service
    n_Integration_Event_Bus -- "routes confirmed orders to" --> n_Payment_Background_Processor
    n_Integration_Event_Bus -- "delivers order events to" --> n_Catalog_and_Identity_Services
    n_Shopping_Basket_Service -- "subscribes to event bus through" --> n_Integration_Event_Bus
    n_Shopping_Basket_Service -- "configures authentication using" --> n_Host_and_Service_Defaults
    n_Payment_Background_Processor -- "publishes payment results through" --> n_Integration_Event_Bus
    classDef added fill:#1f883d,stroke:#0b5d23,color:#ffffff;
    classDef modified fill:#bf8700,stroke:#7d4e00,color:#ffffff;
    classDef deleted fill:#cf222e,stroke:#82071e,color:#ffffff,stroke-dasharray:5 3;
    class n_Order_Management_Service,n_Catalog_and_Identity_Services,n_Web_and_Hybrid_Frontend,n_Integration_Event_Bus,n_Host_and_Service_Defaults,n_Payment_Background_Processor modified;
    linkStyle 3 stroke:#0b5d23,stroke-width:2px;
Loading

download artifacts · run 45651302005

@CodeBoarding CodeBoarding deleted a comment from github-actions Bot Nov 25, 2026
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

CodeBoarding review

Status: 13 changed components

See the full change in CodeBoarding.

graph LR
    n_Mobile_and_Desktop_Client["Mobile and Desktop Client"]
    n_Order_Management_Service["Order Management Service"]
    n_Catalog_and_Identity_Services["Catalog and Identity Services"]
    n_Web_and_Hybrid_Frontend["Web and Hybrid Frontend"]
    n_Webhook_Notification_Service["Webhook Notification Service"]
    n_Integration_Event_Bus["Integration Event Bus"]
    n_Shopping_Basket_Service["Shopping Basket Service"]
    n_Host_and_Service_Defaults["Host and Service Defaults"]
    n_Payment_Background_Processor["Payment Background Processor"]
    n_Order_Management_Service -- "persists integration event logs in" --> n_Catalog_and_Identity_Services
    n_Order_Management_Service -- "publishes order events to" --> n_Integration_Event_Bus
    n_Order_Management_Service -- "configures defaults using" --> n_Host_and_Service_Defaults
    n_Catalog_and_Identity_Services -- "dispatches reservation events conforming to" --> n_Order_Management_Service
    n_Catalog_and_Identity_Services -- "publishes catalog events to" --> n_Integration_Event_Bus
    n_Web_and_Hybrid_Frontend -- "subscribes to events through" --> n_Integration_Event_Bus
    n_Web_and_Hybrid_Frontend -- "applies configuration and telemetry from" --> n_Host_and_Service_Defaults
    n_Webhook_Notification_Service -- "migrates database using" --> n_Catalog_and_Identity_Services
    n_Webhook_Notification_Service -- "subscribes to domain events through" --> n_Integration_Event_Bus
    n_Webhook_Notification_Service -- "consumes service defaults from" --> n_Host_and_Service_Defaults
    n_Integration_Event_Bus -- "dispatches integration events to" --> n_Order_Management_Service
    n_Integration_Event_Bus -- "pushes status events to" --> n_Web_and_Hybrid_Frontend
    n_Integration_Event_Bus -- "triggers webhook handlers via" --> n_Webhook_Notification_Service
    n_Integration_Event_Bus -- "notifies basket of order creation via" --> n_Shopping_Basket_Service
    n_Integration_Event_Bus -- "routes confirmed orders to" --> n_Payment_Background_Processor
    n_Integration_Event_Bus -- "delivers order events to" --> n_Catalog_and_Identity_Services
    n_Shopping_Basket_Service -- "subscribes to event bus through" --> n_Integration_Event_Bus
    n_Shopping_Basket_Service -- "configures authentication using" --> n_Host_and_Service_Defaults
    n_Payment_Background_Processor -- "publishes payment results through" --> n_Integration_Event_Bus
    classDef added fill:#1f883d,stroke:#0b5d23,color:#ffffff;
    classDef modified fill:#bf8700,stroke:#7d4e00,color:#ffffff;
    classDef deleted fill:#cf222e,stroke:#82071e,color:#ffffff,stroke-dasharray:5 3;
    class n_Order_Management_Service,n_Catalog_and_Identity_Services,n_Web_and_Hybrid_Frontend,n_Integration_Event_Bus,n_Host_and_Service_Defaults,n_Payment_Background_Processor modified;
    linkStyle 3 stroke:#0b5d23,stroke-width:2px;
Loading

download artifacts · run 46281273629

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.

1 participant