Path: redb-route/demo/SerialNumbersDemo/SerialNumbers.Core/Services/SerialRequestService.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/demo/SerialNumbersDemo/SerialNumbers.Core/Services/SerialRequestService.cs ---- using System.Globalization; using redb.Core; using redb.Core.Models.Entities; using redb.Route.Abstractions; using SerialNumbers.Core.Integration.Xml; using SerialNumbers.Domain.Entities; using SerialNumbers.Domain.Services; namespace SerialNumbers.Core.Services; /// /// The steps of a serial number request. Each one is a ProcessWithRedb step inside the request /// route's .Transacted() block, so they either all commit or leave no trace. /// public static class SerialRequestService { /// /// Records the message and the request and takes the decision, or, for the release of a request on /// hold, decides that request again. Sets the header the route /// branches on. /// public static async Task RegisterAsync(IRedbService redb, IExchange exchange, CancellationToken ct) { if (exchange.In.Headers.TryGetValue(SerialHeaders.ReleaseOf, out var releaseOf) && releaseOf is not null) { await ResumeAsync(redb, exchange, Convert.ToInt64(releaseOf, CultureInfo.InvariantCulture), ct); return; } var xml = (SerialNumberRequestXml)exchange.In.Body!; var partner = exchange.In.GetHeader(SerialHeaders.Partner)!; var product = await ProductAsync(redb, xml.Gtin); var earlier = await redb.Query() .Where(r => r.PartnerCode == partner && r.RequestId == xml.RequestId) .Take(1) .ToListAsync(); var isDuplicate = earlier.Count > 0; var allocatedThisYear = await AllocatedThisYearAsync(redb, product, ct); var decision = SerialRequestPolicy.Decide(product?.Props, xml.Quantity, allocatedThisYear, isDuplicate); var messageId = await redb.SaveAsync(IntakeRecorder.Create(exchange, decision.Status, error: decision.RejectionReason), ct); var request = new RedbObject { name = $"{partner}/{xml.RequestId}", // The partner's request id is unique per partner. A concurrent twin fails on this key, // rolls back and is answered as a duplicate when the file is picked up again. ValueUnique = isDuplicate ? null : $"{partner}/{xml.RequestId}", Props = new SerialNumberRequest { RequestId = xml.RequestId, PartnerCode = partner, Gtin = xml.Gtin, Quantity = xml.Quantity, Status = decision.Status, RejectionReason = decision.RejectionReason, Message = new RedbObject { id = messageId }, }, }; await redb.SaveAsync(request, ct); exchange.Properties[SerialProperties.Request] = request; exchange.In.Headers[SerialHeaders.RequestId] = xml.RequestId; exchange.In.Headers[SerialHeaders.Decision] = decision.Status; } /// Accepted: reserves the serial numbers and writes the response. public static async Task AllocateAsync(IRedbService redb, IExchange exchange, CancellationToken ct) { var request = RequestOf(exchange); var allocationId = await SerialNumberAllocator.AllocateAsync(redb, request.Props, request.Id, DateTime.UtcNow.Year, ct); var response = ResponseTo(request); response.Props.Accepted = true; response.Props.AllocationId = allocationId; await redb.SaveAsync(response, ct); exchange.Properties[SerialProperties.Response] = response; } /// Rejected: writes a response carrying the reason. public static async Task RejectAsync(IRedbService redb, IExchange exchange, CancellationToken ct) { var request = RequestOf(exchange); var response = ResponseTo(request); response.Props.Accepted = false; response.Props.RejectionReason = request.Props.RejectionReason; await redb.SaveAsync(response, ct); exchange.Properties[SerialProperties.Response] = response; } /// Puts the response into the outbox, in the same transaction as everything above. public static async Task QueueResponseAsync(IRedbService redb, IExchange exchange, CancellationToken ct) { var response = (RedbObject)exchange.Properties[SerialProperties.Response]!; await OutboxWriter.EnqueueAsync(redb, response.Props.PartnerCode, response.Id, ct); } /// /// The release of a request on hold: the same request object is decided again, against the /// product and the quota as they are now. No new request, no new message record. /// private static async Task ResumeAsync(IRedbService redb, IExchange exchange, long requestObjectId, CancellationToken ct) { var request = await redb.LoadAsync(requestObjectId, cancellationToken: ct) ?? throw new InvalidOperationException($"Request object {requestObjectId} was not found."); exchange.In.Headers[SerialHeaders.RequestId] = request.Props.RequestId; if (request.Props.Status != MessageStatuses.OnHold) { exchange.In.Headers[SerialHeaders.Decision] = ReleaseOutcomes.AlreadyDecided; return; } var product = await ProductAsync(redb, request.Props.Gtin); var allocatedThisYear = await AllocatedThisYearAsync(redb, product, ct); var decision = SerialRequestPolicy.Decide(product?.Props, request.Props.Quantity, allocatedThisYear, isDuplicate: false); if (decision.Status != MessageStatuses.OnHold) { request.Props.Status = decision.Status; request.Props.RejectionReason = decision.RejectionReason; await redb.SaveAsync(request, ct); // Saved after the request, so the message record ends with the new status. var messageId = request.Props.Message?.Id ?? throw new InvalidOperationException($"Request object {requestObjectId} has no inbound message."); var message = await redb.LoadAsync(messageId, cancellationToken: ct) ?? throw new InvalidOperationException($"Inbound message object {messageId} was not found."); message.Props.Status = decision.Status; message.Props.Error = decision.RejectionReason; await redb.SaveAsync(message, ct); } exchange.Properties[SerialProperties.Request] = request; exchange.In.Headers[SerialHeaders.Decision] = decision.Status; } /// The GTIN is the product's unique key: a system-field lookup, no Props scan. private static Task?> ProductAsync(IRedbService redb, string gtin) => redb.Query() .WhereRedb(o => o.ValueUnique == gtin) .FirstOrDefaultAsync(); /// /// What was allocated this year, for an active product only. The read takes the range lock that keeps /// two requests from passing the quota together; a request that waits or is rejected does not need it. /// private static async Task AllocatedThisYearAsync(IRedbService redb, RedbObject? product, CancellationToken ct) => product?.Props.Status == ProductStatuses.Active ? await SerialNumberAllocator.AllocatedThisYearAsync(redb, product.Props.Gtin, DateTime.UtcNow.Year, ct) : 0; private static RedbObject RequestOf(IExchange exchange) => (RedbObject)exchange.Properties[SerialProperties.Request]!; private static RedbObject ResponseTo(RedbObject request) => new() { name = $"{request.Props.PartnerCode}/{request.Props.RequestId}", Props = new SerialNumberResponse { RequestId = request.Props.RequestId, PartnerCode = request.Props.PartnerCode, Gtin = request.Props.Gtin, Quantity = request.Props.Quantity, Request = new RedbObject { id = request.Id }, }, }; }