/// <summary>
        ///     Sets the Trilogie fields on the ATG Order, incld. error message, status and order ID.
        /// </summary>
        /// <param name="trilogieReq">Request containing the results of submitting the order to Trilogie.</param>
        /// <param name="atgOrder">ATG Order object containing line items with shipFrom values.</param>
        public static async Task SetTrilogieFieldsOnATGOrder(TrilogieRequest trilogieReq, AtgOrderRes atgOrder)
        {
            atgOrder.trilogieErrorMessage = trilogieReq.TrilogieErrorMessage;
            atgOrder.trilogieOrderId      = trilogieReq.TrilogieOrderId;
            atgOrder.trilogieStatus       = trilogieReq.TrilogieStatus.ToString();
#if DEBUG
            // Sets to false if order failed in Trilogie
            atgOrder.processSourcing = trilogieReq.TrilogieStatus == TrilogieStatus.Pass;
#endif
        }
        /// <summary>
        ///     Sets the Trilogie fields on the Manual Order, incld. error message, status and order ID.
        /// </summary>
        /// <param name="trilogieReq">Request containing the results of submitting the order to Trilogie.</param>
        /// <param name="manualOrder">Manual Order object that matches the ATG Order from the request.</param>
        public static async Task SetTrilogieFieldsOnManualOrder(TrilogieRequest trilogieReq, ManualOrder manualOrder)
        {
            manualOrder.trilogieErrorMessage = trilogieReq.TrilogieErrorMessage;
            manualOrder.trilogieOrderId      = trilogieReq.TrilogieOrderId;
            manualOrder.trilogieStatus       = trilogieReq.TrilogieStatus.ToString();
#if DEBUG
            // Sets to false if order failed in Trilogie
            manualOrder.orderComplete = trilogieReq.TrilogieStatus == TrilogieStatus.Pass;

            if (manualOrder.orderComplete)
            {
                manualOrder.timeCompleted = GetCurrentEasternTime();
            }
            else
            {
                manualOrder.timeCompleted = null;
            }
#endif
        }
        /// <summary>
        ///     Creates a new manual order object for orders that cannot be auto-sourced and require manual intervention by a rep.
        /// </summary>
        /// <param name="atgOrderRes">ATG Order with sourcing fields.</param>
        /// <returns>The manual order object that was created from the ATG order.</returns>
        public ManualOrder CreateManualOrder(AtgOrderRes atgOrderRes, TrilogieRequest trilogieReq = null)
        {
            try
            {
                var manualOrder = new ManualOrder(atgOrderRes, trilogieReq);

                SetLogons(manualOrder);

                return(manualOrder);
            }
            catch (Exception ex)
            {
                var title = $"Error in CreateOrUpdateManualOrder. Order ID: {atgOrderRes.atgOrderId}.";
                _logger.LogError(@"{Title} {Ex}", title, ex);
#if RELEASE
                var teamsMessage = new TeamsMessage(title, $"Error message: {ex.Message}. Stacktrace: {ex.StackTrace}", "red", SourcingEngineFunctions.errorLogsUrl);
                teamsMessage.LogToTeams(teamsMessage);
#endif
                throw;
            }
        }
        /// <summary>
        ///     Sets the TrilogieStatus, TrilogieErrorMessage and TrilogieOrderID fieldson the ManualOrder in CosmosDB based on the request values.
        /// </summary>
        /// <param name="atgOrderId">ID of the original ATG order.</param>
        /// <param name="document">Cosmos DocumentDB client.</param>
        /// <param name="trilogieReq">Request containing the results of submitting the order to Trilogie.</param>
        public static async Task UpdateTrilogieStatusOnManualOrder(string atgOrderId, DocumentClient document, TrilogieRequest trilogieReq)
        {
            try
            {
                var manualOrderDoc = await GetOrder <ManualOrder>(atgOrderId, document);

                ManualOrder manualOrder = (dynamic)manualOrderDoc;

                if (manualOrder != null)
                {
                    await SetTrilogieFieldsOnManualOrder(trilogieReq, manualOrder);
                }
                else // create a new manual order
                {
                    var orderController = new OrderController(new LocationController());

                    var orderDoc = await GetOrder <AtgOrderRes>(atgOrderId, document);

                    AtgOrderRes atgOrder = (dynamic)orderDoc;

                    manualOrder = orderController.CreateManualOrder(atgOrder, trilogieReq);
                }

                var containerName = Environment.GetEnvironmentVariable("MANUAL_ORDERS_CONTAINER_NAME");
                var collectionUri = UriFactory.CreateDocumentCollectionUri("sourcing-engine", containerName);

                _ = document.UpsertDocumentAsync(collectionUri, manualOrder);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception in UpdateTrilogieStatusOnManualOrder");
                throw;
            }
        }
        /// <summary>
        ///     Sets the TrilogieStatus, TrilogieErrorMessage and TrilogieOrderID fields on the ATGOrder in CosmosDB based on the request values.
        /// </summary>
        /// <param name="atgOrderId">ID of the original ATG order.</param>
        /// <param name="document">Cosmos DocumentDB client.</param>
        /// <param name="trilogieReq">Request containing the results of submitting the order to Trilogie.</param>
        /// <returns>The updated ATG order.</returns>
        public static async Task <AtgOrderRes> UpdateTrilogieStatusOnAtgOrder(string atgOrderId, DocumentClient document, TrilogieRequest trilogieReq)
        {
            try
            {
                var orderDoc = await GetOrder <AtgOrderRes>(atgOrderId, document);

                AtgOrderRes atgOrder = (dynamic)orderDoc;

                await SetTrilogieFieldsOnATGOrder(trilogieReq, atgOrder);

                _ = document.ReplaceDocumentAsync(orderDoc.SelfLink, atgOrder);

                return(atgOrder);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception in UpdateTrilogieStatusOnAtgOrder");
                throw;
            }
        }