public async Task HandleAsync(CreateOrderCommand command, CancellationToken cancellationToken)
        {
            var transactionId = DocumentIdHelper.GetEntityId <OrderTransaction>(DocumentStore, command.TransactionId);

            var(totalCost, getTotalCostStepResult) = await GetAndSaveOrderTotalAsync(transactionId, command, cancellationToken).ConfigureAwait(false);

            if (getTotalCostStepResult == StepResult.Abort)
            {
                // There should be nothing to rolled back, but this marks the transaction as failed so it won't be rescheduled.
                await RollbackAsync(transactionId, cancellationToken).ConfigureAwait(false);

                return;
            }
            else if (getTotalCostStepResult == StepResult.Retry)
            {
                return;
            }

            await ConsumeLoyaltyPointsAsync(transactionId, totalCost, command.UserId, cancellationToken).ConfigureAwait(false);
            await ReserveItemsAsync(transactionId, command, cancellationToken).ConfigureAwait(false);

            var transactionDocument = await GetTransactionByIdAsync(transactionId, cancellationToken).ConfigureAwait(false);

            if (transactionDocument.InventoryReservationStepDetails.StepStatus == StepStatus.Completed &&
                transactionDocument.LoyaltyPointsConsumptionStepDetails.StepStatus == StepStatus.Completed)
            {
                await CreateDeliveryRequestAsync(transactionId, command, cancellationToken).ConfigureAwait(false);
            }

            transactionDocument = await GetTransactionByIdAsync(transactionId, cancellationToken).ConfigureAwait(false);

            if (transactionDocument.TransactionStatus == TransactionStatus.PermanentFailure)
            {
                await RollbackAsync(transactionId, cancellationToken).ConfigureAwait(false);

                return;
            }

            if (transactionDocument.DeliveryCreationStepDetails.StepStatus == StepStatus.Completed &&
                transactionDocument.InventoryReservationStepDetails.StepStatus == StepStatus.Completed &&
                transactionDocument.LoyaltyPointsConsumptionStepDetails.StepStatus == StepStatus.Completed)
            {
                await CompleteTransactionAsync(transactionId, cancellationToken).ConfigureAwait(false);
            }
        }
Exemplo n.º 2
0
        public static Operations.DataStructures.Delivery ToServiceContract(IDocumentStore documentStore, Entities.Delivery deliveryEntity)
        {
            if (documentStore == null)
            {
                throw new ArgumentNullException(nameof(documentStore));
            }

            if (deliveryEntity == null)
            {
                return(null);
            }

            var id = DocumentIdHelper.GetEntityId <Entities.Delivery>(documentStore, deliveryEntity.Id);

            return(new Operations.DataStructures.Delivery(
                       id,
                       AddressMapper.ToServiceContract(deliveryEntity.Address),
                       DeliveryStatusMapper.ToServiceContract(deliveryEntity.Status)));
        }