private void CheckPromocodeLimit(MiddlewareDBContext dbContext, JArray promocodes, List <string> warnings, KeyValuePair <string, PromocodeInformation> promocodeInformation, PromocodePool poolInfo, Guid contactId) { var purchaseCount = GetPromocodeInPurchaseCount(promocodeInformation.Value.Id); if (purchaseCount >= poolInfo.UseCountRestriction) { promocodes.Remove(promocodes.FirstOrDefault(p => p.Value <string>() == promocodeInformation.Key)); SavePromocodeAttempt(dbContext, promocodeInformation.Key, poolInfo.Id, false, Guid.Empty, contactId); warnings.Add($"Количество использований промокода {promocodeInformation.Key} исчерпано"); } else { SavePromocodeAttempt(dbContext, promocodeInformation.Key, poolInfo.Id, true, Constants.PromocodeStatuses.Accepted, contactId); } }
private void SavePromocodeAttempt(MiddlewareDBContext dbContext, string promocode, Guid poolId, bool isNeedCreateNew, Guid statusId, Guid contactId) { dbContext.PromocodeUseAttempts.Add(new PromocodeUseAttempt() { Date = DateTime.UtcNow, Id = Guid.NewGuid(), Promocode = promocode, PoolId = poolId, IsNeedCreateNew = isNeedCreateNew, StatusId = statusId, ContactId = contactId }); dbContext.SaveChanges(); }
public Dictionary <string, object> GetHandledResponse(Dictionary <string, object> requestData, Dictionary <string, object> responseData, Dictionary <string, object> additionalResponseData, MiddlewareDBContext dbContext) { if (responseData.ContainsKey("data")) { JObject data = responseData["data"] as JObject; if (data.ContainsKey("productDiscounts")) { JArray productDiscounts = data["productDiscounts"] as JArray; if (productDiscounts != null && productDiscounts.Count > 0) { foreach (JObject productDiscount in productDiscounts) { var discountAmount = 0; JArray discounts = productDiscount["discounts"] as JArray; if (discounts != null && discounts.Count > 0) { foreach (JObject discount in discounts) { if (discount.ContainsKey("discount")) { var d = Convert.ToInt32(Math.Round((decimal)discount["discount"])); discount["discount"] = d; discountAmount += d; } } } if (productDiscount.ContainsKey("discount")) { productDiscount["discount"] = discountAmount; } } } } } if (additionalResponseData != null) { responseData = new Dictionary <string, object>[] { responseData, additionalResponseData }.SelectMany(dict => dict) .ToDictionary(pair => pair.Key, pair => pair.Value); } return(responseData); }
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); }
public Dictionary <string, object> GetHandledResponse(Dictionary <string, object> requestData, Dictionary <string, object> responseData, Dictionary <string, object> additionalResponseData, MiddlewareDBContext dbContext) { //todo: confirm middlewarecode return(responseData); }
public PurchaseController(MiddlewareDBContext context) { _dbContext = context; }
public EntityController(MiddlewareDBContext context) { _dbContext = context; }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, MiddlewareDBContext dBContext) { loggerFactory.AddLog4Net(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); dBContext.Database.Migrate(); JobManager.Initialize(new Scheduller(app.ApplicationServices)); }
public PromocodeAttemptsJob(MiddlewareDBContext dbContext) { _dBContext = dbContext; }