Example #1
0
        public async Task <IActionResult> UpdateOrderStatus([FromRoute(Name = "orderId")] int orderId, [FromBody] DtoUpdateOrderStatusRequest request)
        {
            var newStatus = OrderStatusFromString(request.StatusName);

            if (newStatus == OrderStatus.Unknown)
            {
                return(BadRequest());
            }

            var orderResult = await orders.FetchOrder(orderId)
                              .Ensure(o => o.HasValue, "Order exists")
                              .OnSuccess(o => o.Value)
                              .ConfigureAwait(false);

            if (orderResult.IsFailure)
            {
                return(NotFound());
            }

            var order = orderResult.Value;

            if (newStatus == OrderStatus.Complete && order.Total > 0)
            {
                await billingManager.FinaliseOrderPayment(order).ConfigureAwait(false);
            }

            return(await orders.UpdateOrder(new OrderPatch
            {
                ResourceId = order.OrderId,
                OrderStatus = new PatchOperation <int>
                {
                    Value = (int)OrderStatusFromString(request.StatusName),
                    Operation = OperationKind.Update
                }
            })
                   .OnSuccess(() => messagePublisher.Publish(
                                  $"{constants.OrderEventExchangeName}.{order.VenueId}",
                                  PublishStrategy.Fanout,
                                  new Message <DtoOrderStatusMessage>
            {
                Type = newStatus == OrderStatus.Rejected ? constants.MessageOrderCancelled : constants.MessageOrderUpdated,
                Data = new DtoOrderStatusMessage
                {
                    OrderId = orderId,
                    OrderStatus = request.StatusName,
                    ServingType = (ServingType)order.ServingType
                }
            }
                                  ))
                   .OnSuccess(() =>
            {
                var servingType = (ServingType)order.ServingType;
                var requestedTableService = servingType != ServingType.BarService && !string.IsNullOrWhiteSpace(order.Table);
                string notification = newStatus switch
                {
                    OrderStatus.Ordered => "We've received your order. Your order will be prepared shortly.",
                    OrderStatus.InProgress => "Your order's being prepared. It'll be ready to collect shortly.",
                    OrderStatus.Ready => requestedTableService ? "Your order will be with you shortly. Thanks for using Koasta!" : "Your order's ready to collect! Show your receipt at the bar to pick it up.",
                    _ => null,
                };
                if (notification == null)
                {
                    return;
                }

                messagePublisher.DirectPublish(
                    constants.NotificationQueueName,
                    constants.NotificationExchangeRoutingKey,
                    constants.NotificationExchangeName,
                    constants.NotificationExchangeQueueName,
                    new Message <DtoNotificationMessage>
                {
                    Type = constants.MessageGeneric,
                    Data = new DtoNotificationMessage
                    {
                        NotificationType = constants.NotificationTypeOrderUpdate,
                        UserId = order.UserId,
                        Message = notification,
                    }
                }
                    );
            })
                   .OnBoth(x => x.IsFailure ? StatusCode(500) : StatusCode(200))
                   .ConfigureAwait(false));
        }