Exemplo n.º 1
0
        public async Task <ActionResult> UpdateAsync(string orderId, ServiceRecipientsModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var order = await _orderRepository.GetOrderByIdAsync(orderId);

            if (order is null)
            {
                return(NotFound());
            }

            var primaryOrganisationId = User.GetPrimaryOrganisationId();

            if (primaryOrganisationId != order.OrganisationId)
            {
                return(Forbid());
            }

            var serviceRecipients = model.ServiceRecipients.Select(recipient => new ServiceRecipient
            {
                Name    = recipient.Name,
                OdsCode = recipient.OdsCode,
                Order   = order
            }).ToList();

            await _serviceRecipientRepository.UpdateAsync(order.OrderId, serviceRecipients);

            if (serviceRecipients.Count is 0)
            {
                order.CatalogueSolutionsViewed = false;
            }

            order.ServiceRecipientsViewed = true;
            order.SetLastUpdatedBy(User.GetUserId(), User.GetUserName());

            await _orderRepository.UpdateOrderAsync(order);

            return(NoContent());
        }