示例#1
0
        public PreHandlerResult GetHandledRequest(Dictionary <string, object> requestData)
        {
            var result = new PreHandlerResult();

            if (requestData.ContainsKey("useMaxDiscount"))
            {
                requestData.Remove("useMaxDiscount");
            }
            requestData.Add("useMaxDiscount", false);

            if (requestData.ContainsKey("products"))
            {
                var products = requestData["products"] as JArray;
                products.Add(
                    JToken.FromObject(new ProductDto()
                {
                    Index       = products.Max(p => System.Convert.ToInt32(p["index"].ToString())) + 1,
                    Price       = 0,
                    ProductCode = "coupon",
                    Quantity    = 1,
                    Amount      = 0
                })
                    );
            }

            result.Request = requestData;
            return(result);
        }
示例#2
0
        public PreHandlerResult GetHandledRequest(Dictionary <string, object> requestData, MiddlewareDBContext dbContext)
        {
            var result = new PreHandlerResult();

            if (Decimal.TryParse(dbContext.Setting.FirstOrDefault(s => s.Code == "MinProductPrice")?.Value, out decimal minPrice) && minPrice > 0)
            {
                if (requestData.ContainsKey("products"))
                {
                    var products = requestData["products"] as Newtonsoft.Json.Linq.JArray;
                    if (products != null && products.Count > 0)
                    {
                        foreach (JObject product in products)
                        {
                            product.Add("minPrice", minPrice);
                        }
                    }
                }
            }

            if (requestData.ContainsKey("promoCodes"))
            {
                var promocodes = requestData["promoCodes"] as Newtonsoft.Json.Linq.JArray;
                if (promocodes != null && promocodes.Count > 0)
                {
                    var contactId = GetContactId(requestData);
                    if (contactId != Guid.Empty)
                    {
                        var promocodesDictionary = ReadPromocodesInformation(promocodes);
                        var warnings             = new List <string>();

                        foreach (var promocodeInformation in promocodesDictionary)
                        {
                            if (promocodeInformation.Value != null)
                            {
                                var poolInfo = dbContext.PromocodePools.FirstOrDefault(pool => pool.Id == promocodeInformation.Value.PoolId);

                                if (poolInfo == null)
                                {
                                    promocodes.Remove(promocodes.FirstOrDefault(p => p.Value <string>() == promocodeInformation.Key));
                                    SavePromocodeAttempt(dbContext, promocodeInformation.Key, Guid.Empty, false, Guid.Empty, contactId);
                                    warnings.Add($"Промокод {promocodeInformation.Key} не найден");
                                }
                                else if (!poolInfo.IsActual)
                                {
                                    promocodes.Remove(promocodes.FirstOrDefault(p => p.Value <string>() == promocodeInformation.Key));
                                    SavePromocodeAttempt(dbContext, promocodeInformation.Key, poolInfo.Id, false, Constants.PromocodeStatuses.NotActual, contactId);
                                    warnings.Add($"Промокод {promocodeInformation.Key} более не актуален");
                                }
                                else if (promocodeInformation.Value.ContactId != Guid.Empty)
                                {
                                    if (promocodeInformation.Value.ContactId == contactId)
                                    {
                                        CheckPromocodeLimit(dbContext, promocodes, warnings, promocodeInformation, poolInfo, contactId);
                                    }
                                    else
                                    {
                                        promocodes.Remove(promocodes.FirstOrDefault(p => p.Value <string>() == promocodeInformation.Key));
                                        SavePromocodeAttempt(dbContext, promocodeInformation.Key, poolInfo.Id, false, Constants.PromocodeStatuses.UsesByAnotherContact, contactId);
                                        warnings.Add($"Промокод {promocodeInformation.Key} более не актуален");
                                    }
                                }
                                else if (promocodeInformation.Value.ContactId == Guid.Empty)
                                {
                                    CheckPromocodeLimit(dbContext, promocodes, warnings, promocodeInformation, poolInfo, contactId);
                                }
                            }
                            else
                            {
                                promocodes.Remove(promocodes.FirstOrDefault(p => p.Value <string>() == promocodeInformation.Key));
                                SavePromocodeAttempt(dbContext, promocodeInformation.Key, Guid.Empty, false, Guid.Empty, contactId);
                                warnings.Add($"Промокод {promocodeInformation.Key} не найден");
                            }
                        }

                        if (warnings.Count > 0)
                        {
                            result.AdditionalResponseData.Add("promocodeWarnings", warnings);
                        }
                    }
                }
            }
            if (requestData.ContainsKey("deliveryForm"))
            {
                requestData.Add("deliveryMethod", requestData["deliveryForm"]);
                requestData.Remove("deliveryForm");
            }
            if (requestData.ContainsKey("deliveryForm"))
            {
                requestData.Add("deliveryMethod", requestData["deliveryForm"]);
                requestData.Remove("deliveryForm");
            }
            if (requestData.ContainsKey("getMaxDiscount"))
            {
                requestData.Add("useMaxDiscount", requestData["getMaxDiscount"]);
                requestData.Remove("getMaxDiscount");
            }

            result.Request = requestData;
            return(result);
        }