/// <summary> /// Delete a customer group product /// </summary> /// <param name="customerGroupProduct">Customer group product</param> public virtual async Task DeleteCustomerGroupProduct(CustomerGroupProduct customerGroupProduct) { if (customerGroupProduct == null) { throw new ArgumentNullException(nameof(customerGroupProduct)); } await _customerGroupProductRepository.DeleteAsync(customerGroupProduct); //clear cache await _cacheBase.RemoveAsync(string.Format(CacheKey.CUSTOMERGROUPSPRODUCTS_ROLE_KEY, customerGroupProduct.CustomerGroupId)); await _cacheBase.RemoveByPrefix(CacheKey.PRODUCTS_CUSTOMER_GROUP_PATTERN); //event notification await _mediator.EntityDeleted(customerGroupProduct); }
/// <summary> /// Deletes a product specification attribute mapping /// </summary> /// <param name="productSpecificationAttribute">Product specification attribute</param> public virtual async Task DeleteProductSpecificationAttribute(ProductSpecificationAttribute productSpecificationAttribute) { if (productSpecificationAttribute == null) { throw new ArgumentNullException("productSpecificationAttribute"); } var updatebuilder = Builders <Product> .Update; var update = updatebuilder.Pull(p => p.ProductSpecificationAttributes, productSpecificationAttribute); await _productRepository.Collection.UpdateOneAsync(new BsonDocument("_id", productSpecificationAttribute.ProductId), update); //clear cache await _cacheBase.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, productSpecificationAttribute.ProductId)); //event notification await _mediator.EntityDeleted(productSpecificationAttribute); }
/// <summary> /// Attach a tag to the order /// </summary> /// <param name="orderTag">Order's identification</param> public virtual async Task AttachOrderTag(string orderTagId, string orderId) { //assign to product await _orderRepository.AddToSet(orderId, x => x.OrderTags, orderTagId); var orderTag = await GetOrderTagById(orderTagId); //update product tag await _orderTagRepository.UpdateField(orderTagId, x => x.Count, orderTag.Count + 1); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.ORDERS_BY_ID_KEY, orderId)); await _cacheBase.RemoveAsync(string.Format(CacheKey.ORDERTAG_COUNT_KEY, orderTagId)); //event notification await _mediator.EntityUpdated(orderTag); }
/// <summary> /// Deletes a product attribute mapping /// </summary> /// <param name="productAttributeMapping">Product attribute mapping</param> public virtual async Task DeleteProductAttributeMapping(ProductAttributeMapping productAttributeMapping) { if (productAttributeMapping == null) { throw new ArgumentNullException("productAttributeMapping"); } var updatebuilder = Builders <Product> .Update; var update = updatebuilder.PullFilter(p => p.ProductAttributeMappings, y => y.Id == productAttributeMapping.Id); await _productRepository.Collection.UpdateManyAsync(new BsonDocument("_id", productAttributeMapping.ProductId), update); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, productAttributeMapping.ProductId)); //event notification await _mediator.EntityDeleted(productAttributeMapping); }
/// <summary> /// Attach a tag to the order /// </summary> /// <param name="orderTag">Order's identification</param> public virtual async Task AttachOrderTag(string orderTagId, string orderId) { var updateBuilder = Builders <Order> .Update; var update = updateBuilder.AddToSet(p => p.OrderTags, orderTagId); await _orderRepository.Collection.UpdateOneAsync(new BsonDocument("_id", orderId), update); // update ordertag with count's order and new order id var updateBuilderTag = Builders <OrderTag> .Update .Inc(x => x.Count, 1); await _orderTagRepository.Collection.UpdateOneAsync(new BsonDocument("_id", orderTagId), updateBuilderTag); var orderTag = await _orderTagRepository.GetByIdAsync(orderTagId); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.ORDERS_BY_ID_KEY, orderId)); await _cacheBase.RemoveAsync(string.Format(CacheKey.ORDERTAG_COUNT_KEY, orderTagId)); //event notification await _mediator.EntityUpdated(orderTag); }
public async Task <bool> Handle(UpdateProductReviewTotalsCommand request, CancellationToken cancellationToken) { if (request.Product == null) { throw new ArgumentNullException("product"); } int approvedRatingSum = 0; int notApprovedRatingSum = 0; int approvedTotalReviews = 0; int notApprovedTotalReviews = 0; var reviews = await _productReviewService.GetAllProductReviews(null, null, null, null, null, null, request.Product.Id, 0, int.MaxValue); foreach (var pr in reviews) { if (pr.IsApproved) { approvedRatingSum += pr.Rating; approvedTotalReviews++; } else { notApprovedRatingSum += pr.Rating; notApprovedTotalReviews++; } } request.Product.ApprovedRatingSum = approvedRatingSum; request.Product.NotApprovedRatingSum = notApprovedRatingSum; request.Product.ApprovedTotalReviews = approvedTotalReviews; request.Product.NotApprovedTotalReviews = notApprovedTotalReviews; var filter = Builders <Product> .Filter.Eq("Id", request.Product.Id); var update = Builders <Product> .Update .Set(x => x.ApprovedRatingSum, request.Product.ApprovedRatingSum) .Set(x => x.NotApprovedRatingSum, request.Product.NotApprovedRatingSum) .Set(x => x.ApprovedTotalReviews, request.Product.ApprovedTotalReviews) .Set(x => x.NotApprovedTotalReviews, request.Product.NotApprovedTotalReviews); await _productRepository.Collection.UpdateOneAsync(filter, update); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, request.Product.Id)); return(true); }
public virtual async Task UpdateCustomerActionType(CustomerActionType customerActionType) { if (customerActionType == null) { throw new ArgumentNullException(nameof(customerActionType)); } await _customerActionTypeRepository.UpdateAsync(customerActionType); //clear cache await _cacheBase.RemoveAsync(CacheKey.CUSTOMER_ACTION_TYPE); //event notification await _mediator.EntityUpdated(customerActionType); }
/// <summary> /// Attach a tag to the product /// </summary> /// <param name="productPicture">Product picture</param> public virtual async Task AttachProductTag(ProductTag productTag) { if (productTag == null) { throw new ArgumentNullException("productTag"); } var updatebuilder = Builders <Product> .Update; var update = updatebuilder.AddToSet(p => p.ProductTags, productTag.Name); await _productRepository.Collection.UpdateOneAsync(new BsonDocument("_id", productTag.ProductId), update); var builder = Builders <ProductTag> .Filter; var filter = builder.Eq(x => x.Id, productTag.Id); var updateTag = Builders <ProductTag> .Update .Inc(x => x.Count, 1); await _productTagRepository.Collection.UpdateManyAsync(filter, updateTag); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, productTag.ProductId)); //event notification await _mediator.EntityInserted(productTag); }
public bool DeleteCurrency(string currencyName) { IEnumerable <Currency> result = GetAllActive().ToList().Where(c => c.name.Contains(currencyName)); if (result.Any()) { var item = result.FirstOrDefault(); item.isActive = false; _cache.RemoveAsync(currencyName); return(_repoCurrency.Update <Currency>(item)); } else { _logger.LogError($"Called DeleteCurrency cannot found any currency for {currencyName}"); return(false); } }
public async Task <bool> Handle(UpdateIntervalPropertiesCommand request, CancellationToken cancellationToken) { if (request.Product == null) { throw new ArgumentNullException("product"); } var filter = Builders <Product> .Filter.Eq("Id", request.Product.Id); var update = Builders <Product> .Update .Set(x => x.Interval, request.Interval) .Set(x => x.IntervalUnitId, (int)request.IntervalUnit) .Set(x => x.IncBothDate, request.IncludeBothDates); await _productRepository.Collection.UpdateOneAsync(filter, update); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, request.Product.Id)); //event notification await _mediator.EntityUpdated(request.Product); return(true); }
/// <summary> /// Adjust inventory /// </summary> /// <param name="product">Product</param> /// <param name="quantityToChange">Quantity to increase or descrease</param> /// <param name="attributes">Attributes</param> public virtual async Task AdjustInventory(Product product, int quantityToChange, IList <CustomAttribute> attributes = null, string warehouseId = "") { if (product == null) { throw new ArgumentNullException("product"); } if (quantityToChange == 0) { return; } if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStock) { var prevStockQuantity = product.GetTotalStockQuantity(warehouseId: warehouseId); //update stock quantity if (product.UseMultipleWarehouses) { //use multiple warehouses if (quantityToChange < 0) { await ReserveInventory(product, quantityToChange, warehouseId); } else { await UnblockReservedInventory(product, quantityToChange, warehouseId); } product.StockQuantity = product.ProductWarehouseInventory.Sum(x => x.StockQuantity); await UpdateStockProduct(product); } else { //do not use multiple warehouses //simple inventory management product.StockQuantity += quantityToChange; await UpdateStockProduct(product); } //check if minimum quantity is reached if (quantityToChange < 0 && product.MinStockQuantity >= product.GetTotalStockQuantity(warehouseId: "")) { switch (product.LowStockActivity) { case LowStockActivity.DisableBuyButton: product.DisableBuyButton = true; product.DisableWishlistButton = true; var filter = Builders <Product> .Filter.Eq("Id", product.Id); var update = Builders <Product> .Update .Set(x => x.DisableBuyButton, product.DisableBuyButton) .Set(x => x.DisableWishlistButton, product.DisableWishlistButton) .Set(x => x.LowStock, true) .CurrentDate("UpdatedOnUtc"); await _productRepository.Collection.UpdateOneAsync(filter, update); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, product.Id)); //event notification await _mediator.EntityUpdated(product); break; case LowStockActivity.Unpublish: product.Published = false; var filter2 = Builders <Product> .Filter.Eq("Id", product.Id); var update2 = Builders <Product> .Update .Set(x => x.Published, product.Published) .CurrentDate("UpdatedOnUtc"); await _productRepository.Collection.UpdateOneAsync(filter2, update2); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, product.Id)); if (product.ShowOnHomePage) { await _cacheBase.RemoveByPrefix(CacheKey.PRODUCTS_SHOWONHOMEPAGE); } //event notification await _mediator.EntityUpdated(product); break; default: break; } } //qty is increased. product is back in stock (minimum stock quantity is reached again)? if (_catalogSettings.PublishBackProductWhenCancellingOrders) { if (quantityToChange > 0 && prevStockQuantity <= product.MinStockQuantity && product.MinStockQuantity < product.GetTotalStockQuantity(warehouseId: "")) { switch (product.LowStockActivity) { case LowStockActivity.DisableBuyButton: var filter = Builders <Product> .Filter.Eq("Id", product.Id); var update = Builders <Product> .Update .Set(x => x.DisableBuyButton, product.DisableBuyButton) .Set(x => x.DisableWishlistButton, product.DisableWishlistButton) .Set(x => x.LowStock, true) .CurrentDate("UpdatedOnUtc"); await _productRepository.Collection.UpdateOneAsync(filter, update); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, product.Id)); break; case LowStockActivity.Unpublish: product.Published = false; var filter2 = Builders <Product> .Filter.Eq("Id", product.Id); var update2 = Builders <Product> .Update .Set(x => x.Published, product.Published) .CurrentDate("UpdatedOnUtc"); await _productRepository.Collection.UpdateOneAsync(filter2, update2); //cache await _cacheBase.RemoveAsync(string.Format(CacheKey.PRODUCTS_BY_ID_KEY, product.Id)); if (product.ShowOnHomePage) { await _cacheBase.RemoveByPrefix(CacheKey.PRODUCTS_SHOWONHOMEPAGE); } break; default: break; } } } //send email notification if (quantityToChange < 0 && product.GetTotalStockQuantity(warehouseId: warehouseId) < product.NotifyAdminForQuantityBelow) { await _mediator.Send(new SendQuantityBelowStoreOwnerNotificationCommand() { Product = product }); } } if (attributes != null && product.ManageInventoryMethod == ManageInventoryMethod.ManageStockByAttributes) { var combination = _productAttributeParser.FindProductAttributeCombination(product, attributes); if (combination != null) { combination.ProductId = product.Id; if (!product.UseMultipleWarehouses) { combination.StockQuantity += quantityToChange; await _productAttributeService.UpdateProductAttributeCombination(combination); } else { if (quantityToChange < 0) { await ReserveInventoryCombination(product, combination, quantityToChange, warehouseId); } else { await UnblockReservedInventoryCombination(product, combination, quantityToChange, warehouseId); } } product.StockQuantity += quantityToChange; await UpdateStockProduct(product); //send email notification if (quantityToChange < 0 && combination.StockQuantity < combination.NotifyAdminForQuantityBelow) { await _mediator.Send(new SendQuantityBelowStoreOwnerNotificationCommand() { Product = product, ProductAttributeCombination = combination }); } } } if (product.ManageInventoryMethod == ManageInventoryMethod.ManageStockByBundleProducts) { foreach (var item in product.BundleProducts) { var p1 = await _productRepository.GetByIdAsync(item.ProductId); if (p1 != null && (p1.ManageInventoryMethod == ManageInventoryMethod.ManageStock || p1.ManageInventoryMethod == ManageInventoryMethod.ManageStockByAttributes)) { await AdjustInventory(p1, quantityToChange *item.Quantity, attributes, warehouseId); } } } //bundled products var attributeValues = _productAttributeParser.ParseProductAttributeValues(product, attributes); foreach (var attributeValue in attributeValues) { if (attributeValue.AttributeValueType == AttributeValueType.AssociatedToProduct) { //associated product (bundle) var associatedProduct = await _productRepository.GetByIdAsync(attributeValue.AssociatedProductId); if (associatedProduct != null) { await AdjustInventory(associatedProduct, quantityToChange *attributeValue.Quantity, null, warehouseId); } } } //event notification await _mediator.EntityUpdated(product); }