Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Admin panel - GiftVoucherController.cs - add new validator
supportec7b939a6b5e
diff --git a/src/Web/Grand.Web.Admin/Controllers/GiftVoucherController.cs b/src/Web/Grand.Web.Admin/Controllers/GiftVoucherController.csindex a18560f6c..3f2fc0673 100644--- a/src/Web/Grand.Web.Admin/Controllers/GiftVoucherController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/GiftVoucherController.cs@@ -132,20 +132,15 @@ namespace Grand.Web.Admin.Controllers[PermissionAuthorizeAction(PermissionActionName.Edit)][HttpPost]- public async Task<IActionResult> NotifyRecipient(GiftVoucherModel model)+ public async Task<IActionResult> NotifyRecipient(GiftVoucherNotifyRecipient model){var giftVoucher = await _giftVoucherService.GetGiftVoucherById(model.Id);- if (!CommonHelper.IsValidEmail(giftVoucher.RecipientEmail))- ModelState.AddModelError("", "Recipient email is not valid");- if (!CommonHelper.IsValidEmail(giftVoucher.SenderEmail))- ModelState.AddModelError("", "Sender email is not valid");-try{if (ModelState.IsValid){- await _giftVoucherViewModelService.NotifyRecipient(giftVoucher, model);+ await _giftVoucherViewModelService.NotifyRecipient(giftVoucher);}elseError(ModelState);diff --git a/src/Web/Grand.Web.Admin/Interfaces/IGiftVoucherViewModelService.cs b/src/Web/Grand.Web.Admin/Interfaces/IGiftVoucherViewModelService.csindex 280899ceb..1b7741dfc 100644--- a/src/Web/Grand.Web.Admin/Interfaces/IGiftVoucherViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Interfaces/IGiftVoucherViewModelService.cs@@ -8,8 +8,9 @@ namespace Grand.Web.Admin.InterfacesGiftVoucherListModel PrepareGiftVoucherListModel();Task<GiftVoucherModel> PrepareGiftVoucherModel(GiftVoucherModel model = null);Task<(IEnumerable<GiftVoucherModel> giftVoucherModels, int totalCount)> PrepareGiftVoucherModel(GiftVoucherListModel model, int pageIndex, int pageSize);- Task<Order> FillGiftVoucherModel(GiftVoucher giftVoucher, GiftVoucherModel model);- Task NotifyRecipient(GiftVoucher giftVoucher, GiftVoucherModel model);+ Task<GiftVoucherModel> FillGiftVoucherModel(GiftVoucher giftVoucher, GiftVoucherModel model);+ Task NotifyRecipient(GiftVoucher giftVoucher);+ Task<Order> GetOrderFromGiftVoucher(GiftVoucher giftVoucher);Task<GiftVoucher> InsertGiftVoucherModel(GiftVoucherModel model);Task<GiftVoucher> UpdateGiftVoucherModel(GiftVoucher giftVoucher, GiftVoucherModel model);Task DeleteGiftVoucher(GiftVoucher giftVoucher);diff --git a/src/Web/Grand.Web.Admin/Models/Orders/GiftVoucherNotifyRecipient.cs b/src/Web/Grand.Web.Admin/Models/Orders/GiftVoucherNotifyRecipient.csnew file mode 100644index 000000000..0dd2e9e76--- /dev/null+++ b/src/Web/Grand.Web.Admin/Models/Orders/GiftVoucherNotifyRecipient.cs@@ -0,0 +1,3 @@+namespace Grand.Web.Admin.Models.Orders;++public record GiftVoucherNotifyRecipient(string Id);\ No newline at end of filediff --git a/src/Web/Grand.Web.Admin/Services/GiftVoucherViewModelService.cs b/src/Web/Grand.Web.Admin/Services/GiftVoucherViewModelService.csindex e28439dbf..c4b664730 100644--- a/src/Web/Grand.Web.Admin/Services/GiftVoucherViewModelService.cs+++ b/src/Web/Grand.Web.Admin/Services/GiftVoucherViewModelService.cs@@ -72,9 +72,11 @@ namespace Grand.Web.Admin.Servicesmodel ??= new GiftVoucherModel();foreach (var currency in await _currencyService.GetAllCurrencies())- model.AvailableCurrencies.Add(new SelectListItem { Text = currency.Name, Value = currency.CurrencyCode });+ model.AvailableCurrencies.Add(+ new SelectListItem { Text = currency.Name, Value = currency.CurrencyCode });- model.AvailableStores.Add(new SelectListItem { Text = _translationService.GetResource("Admin.Common.All"), Value = "" });+ model.AvailableStores.Add(new SelectListItem+ { Text = _translationService.GetResource("Admin.Common.All"), Value = "" });foreach (var s in await _storeService.GetAllStores())model.AvailableStores.Add(new SelectListItem { Text = s.Shortcut, Value = s.Id });@@ -84,31 +86,31 @@ namespace Grand.Web.Admin.Servicespublic virtual GiftVoucherListModel PrepareGiftVoucherListModel(){var model = new GiftVoucherListModel();- model.ActivatedList.Add(new SelectListItem- {+ model.ActivatedList.Add(new SelectListItem {Value = "",Text = _translationService.GetResource("Admin.GiftVouchers.List.Activated.All")});- model.ActivatedList.Add(new SelectListItem- {+ model.ActivatedList.Add(new SelectListItem {Value = "1",Text = _translationService.GetResource("Admin.GiftVouchers.List.Activated.ActivatedOnly")});- model.ActivatedList.Add(new SelectListItem- {+ model.ActivatedList.Add(new SelectListItem {Value = "2",Text = _translationService.GetResource("Admin.GiftVouchers.List.Activated.DeactivatedOnly")});return model;}- public virtual async Task<(IEnumerable<GiftVoucherModel> giftVoucherModels, int totalCount)> PrepareGiftVoucherModel(GiftVoucherListModel model, int pageIndex, int pageSize)++ public virtual async Task<(IEnumerable<GiftVoucherModel> giftVoucherModels, int totalCount)>+ PrepareGiftVoucherModel(GiftVoucherListModel model, int pageIndex, int pageSize){bool? isGiftVoucherActivated = null;if (model.ActivatedId == 1)isGiftVoucherActivated = true;else if (model.ActivatedId == 2)isGiftVoucherActivated = false;- var giftVouchers = await _giftVoucherService.GetAllGiftVouchers(isGiftVoucherActivated: isGiftVoucherActivated,+ var giftVouchers = await _giftVoucherService.GetAllGiftVouchers(+ isGiftVoucherActivated: isGiftVoucherActivated,giftVoucherCouponCode: model.CouponCode,recipientName: model.RecipientName,pageIndex: pageIndex - 1, pageSize: pageSize);@@ -117,10 +119,13 @@ namespace Grand.Web.Admin.Servicesforeach (var item in giftVouchers){var gift = item.ToModel(_dateTimeService);- var currency = await _currencyService.GetCurrencyByCode(item.CurrencyCode) ?? await _currencyService.GetPrimaryStoreCurrency();+ var currency = await _currencyService.GetCurrencyByCode(item.CurrencyCode) ??+ await _currencyService.GetPrimaryStoreCurrency();- gift.RemainingAmountStr = _priceFormatter.FormatPrice(item.GetGiftVoucherRemainingAmount(), currency, _workContext.WorkingLanguage, true, false);- gift.AmountStr = _priceFormatter.FormatPrice(item.Amount, currency, _workContext.WorkingLanguage, true, false);+ gift.RemainingAmountStr = _priceFormatter.FormatPrice(item.GetGiftVoucherRemainingAmount(), currency,+ _workContext.WorkingLanguage, true, false);+ gift.AmountStr =+ _priceFormatter.FormatPrice(item.Amount, currency, _workContext.WorkingLanguage, true, false);gift.CreatedOn = _dateTimeService.ConvertToUserTime(item.CreatedOnUtc, DateTimeKind.Utc);giftvouchers.Add(gift);@@ -128,6 +133,7 @@ namespace Grand.Web.Admin.Servicesreturn (giftvouchers, giftVouchers.TotalCount);}+public virtual async Task<GiftVoucher> InsertGiftVoucherModel(GiftVoucherModel model){var giftVoucher = model.ToEntity(_dateTimeService);@@ -140,29 +146,39 @@ namespace Grand.Web.Admin.Services_translationService.GetResource("ActivityLog.AddNewGiftVoucher"), giftVoucher.Code);return giftVoucher;}- public virtual async Task<Order> FillGiftVoucherModel(GiftVoucher giftVoucher, GiftVoucherModel model)++ public virtual async Task<Order> GetOrderFromGiftVoucher(GiftVoucher giftVoucher){Order order = null;if (giftVoucher.PurchasedWithOrderItem != null)order = await _orderService.GetOrderByOrderItemId(giftVoucher.PurchasedWithOrderItem.Id);- var currency = await _currencyService.GetCurrencyByCode(giftVoucher.CurrencyCode) ?? await _currencyService.GetPrimaryStoreCurrency();+ return order;+ }- model.PurchasedWithOrderId = giftVoucher.PurchasedWithOrderItem != null ? order?.Id : null;- model.RemainingAmountStr = _priceFormatter.FormatPrice(giftVoucher.GetGiftVoucherRemainingAmount(), currency, _workContext.WorkingLanguage, true, false);- model.AmountStr = _priceFormatter.FormatPrice(giftVoucher.Amount, currency, _workContext.WorkingLanguage, true, false);+ public virtual async Task<GiftVoucherModel> FillGiftVoucherModel(GiftVoucher giftVoucher, GiftVoucherModel model)+ {+ var currency = await _currencyService.GetCurrencyByCode(giftVoucher.CurrencyCode) ??+ await _currencyService.GetPrimaryStoreCurrency();++ model.PurchasedWithOrderId = giftVoucher.PurchasedWithOrderItem != null ? (await GetOrderFromGiftVoucher(giftVoucher))?.Id : null;+ model.RemainingAmountStr = _priceFormatter.FormatPrice(giftVoucher.GetGiftVoucherRemainingAmount(),+ currency, _workContext.WorkingLanguage, true, false);+ model.AmountStr = _priceFormatter.FormatPrice(giftVoucher.Amount, currency, _workContext.WorkingLanguage,+ true, false);model.CreatedOn = _dateTimeService.ConvertToUserTime(giftVoucher.CreatedOnUtc, DateTimeKind.Utc);model.CurrencyCode = giftVoucher.CurrencyCode;- return order;+ return model;}- public virtual async Task NotifyRecipient(GiftVoucher giftVoucher, GiftVoucherModel model)++ public virtual async Task NotifyRecipient(GiftVoucher giftVoucher){- model = giftVoucher.ToModel(_dateTimeService);- var order = await FillGiftVoucherModel(giftVoucher, model);+ var order = await GetOrderFromGiftVoucher(giftVoucher);var languageId = "";if (order != null){- var customerLang = await _languageService.GetLanguageById(order.CustomerLanguageId) ?? (await _languageService.GetAllLanguages()).FirstOrDefault();+ var customerLang = await _languageService.GetLanguageById(order.CustomerLanguageId) ??+ (await _languageService.GetAllLanguages()).FirstOrDefault();if (customerLang != null)languageId = customerLang.Id;}@@ -170,18 +186,17 @@ namespace Grand.Web.Admin.Services{languageId = _languageSettings.DefaultAdminLanguageId;}+var queuedEmailId = await _messageProviderService.SendGiftVoucherMessage(giftVoucher, order, languageId);if (queuedEmailId > 0){giftVoucher.IsRecipientNotified = true;await _giftVoucherService.UpdateGiftVoucher(giftVoucher);- model.IsRecipientNotified = true;}-}+public virtual async Task<GiftVoucher> UpdateGiftVoucherModel(GiftVoucher giftVoucher, GiftVoucherModel model){-giftVoucher = model.ToEntity(giftVoucher, _dateTimeService);await _giftVoucherService.UpdateGiftVoucher(giftVoucher);//activity log@@ -191,6 +206,7 @@ namespace Grand.Web.Admin.Servicesreturn giftVoucher;}+public virtual async Task DeleteGiftVoucher(GiftVoucher giftVoucher){await _giftVoucherService.DeleteGiftVoucher(giftVoucher);@@ -199,6 +215,7 @@ namespace Grand.Web.Admin.Services_workContext.CurrentCustomer, _httpContextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString(),_translationService.GetResource("ActivityLog.DeleteGiftVoucher"), giftVoucher.Code);}+public virtual async Task<GiftVoucherModel> PrepareGiftVoucherModel(GiftVoucher giftVoucher){var model = giftVoucher.ToModel(_dateTimeService);@@ -207,35 +224,42 @@ namespace Grand.Web.Admin.Servicesif (giftVoucher.PurchasedWithOrderItem != null)order = await _orderService.GetOrderByOrderItemId(giftVoucher.PurchasedWithOrderItem.Id);- var currency = await _currencyService.GetCurrencyByCode(giftVoucher.CurrencyCode) ?? await _currencyService.GetPrimaryStoreCurrency();+ var currency = await _currencyService.GetCurrencyByCode(giftVoucher.CurrencyCode) ??+ await _currencyService.GetPrimaryStoreCurrency();model.PurchasedWithOrderId = giftVoucher.PurchasedWithOrderItem != null ? order?.Id : null;model.PurchasedWithOrderNumber = order?.OrderNumber ?? 0;- model.RemainingAmountStr = _priceFormatter.FormatPrice(giftVoucher.GetGiftVoucherRemainingAmount(), currency, _workContext.WorkingLanguage, true, false);- model.AmountStr = _priceFormatter.FormatPrice(giftVoucher.Amount, currency, _workContext.WorkingLanguage, true, false);+ model.RemainingAmountStr = _priceFormatter.FormatPrice(giftVoucher.GetGiftVoucherRemainingAmount(),+ currency, _workContext.WorkingLanguage, true, false);+ model.AmountStr = _priceFormatter.FormatPrice(giftVoucher.Amount, currency, _workContext.WorkingLanguage,+ true, false);model.CreatedOn = _dateTimeService.ConvertToUserTime(giftVoucher.CreatedOnUtc, DateTimeKind.Utc);model.CurrencyCode = giftVoucher.CurrencyCode;return model;}- public virtual async Task<(IEnumerable<GiftVoucherModel.GiftVoucherUsageHistoryModel> giftVoucherUsageHistoryModels, int totalCount)> PrepareGiftVoucherUsageHistoryModels(GiftVoucher giftVoucher, int pageIndex, int pageSize)++ public virtual async+ Task<(IEnumerable<GiftVoucherModel.GiftVoucherUsageHistoryModel> giftVoucherUsageHistoryModels, int+ totalCount)> PrepareGiftVoucherUsageHistoryModels(GiftVoucher giftVoucher, int pageIndex, int pageSize){- var currency = await _currencyService.GetCurrencyByCode(giftVoucher.CurrencyCode) ?? await _currencyService.GetPrimaryStoreCurrency();+ var currency = await _currencyService.GetCurrencyByCode(giftVoucher.CurrencyCode) ??+ await _currencyService.GetPrimaryStoreCurrency();var items = new List<GiftVoucherModel.GiftVoucherUsageHistoryModel>();foreach (var x in giftVoucher.GiftVoucherUsageHistory.OrderByDescending(gcuh => gcuh.CreatedOnUtc)){var order = await _orderService.GetOrderById(x.UsedWithOrderId);- items.Add(new GiftVoucherModel.GiftVoucherUsageHistoryModel- {+ items.Add(new GiftVoucherModel.GiftVoucherUsageHistoryModel {Id = x.Id,OrderId = x.UsedWithOrderId,OrderNumber = order?.OrderNumber ?? 0,- UsedValue = _priceFormatter.FormatPrice(x.UsedValue, currency, _workContext.WorkingLanguage, true, false),+ UsedValue = _priceFormatter.FormatPrice(x.UsedValue, currency, _workContext.WorkingLanguage, true,+ false),CreatedOn = _dateTimeService.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc)});}+return (items.Skip((pageIndex - 1) * pageSize).Take(pageSize), items.Count);}-}-}+}\ No newline at end of filediff --git a/src/Web/Grand.Web.Admin/Validators/Orders/GiftVoucherNotifyRecipientValidator.cs b/src/Web/Grand.Web.Admin/Validators/Orders/GiftVoucherNotifyRecipientValidator.csnew file mode 100644index 000000000..670a0006f--- /dev/null+++ b/src/Web/Grand.Web.Admin/Validators/Orders/GiftVoucherNotifyRecipientValidator.cs@@ -0,0 +1,30 @@+using FluentValidation;+using Grand.Business.Core.Interfaces.Checkout.GiftVouchers;+using Grand.Domain.Catalog;+using Grand.Infrastructure.Validators;+using Grand.SharedKernel.Extensions;+using Grand.Web.Admin.Models.Orders;++namespace Grand.Web.Admin.Validators.Orders+{+ public class GiftVoucherNotifyRecipientValidator : BaseGrandValidator<GiftVoucherNotifyRecipient>+ {+ public GiftVoucherNotifyRecipientValidator(+ IEnumerable<IValidatorConsumer<GiftVoucherNotifyRecipient>> validators,+ IGiftVoucherService giftVoucherService)+ : base(validators)+ {+ RuleFor(x => x).CustomAsync(async (x, context, _) =>+ {+ var giftVoucher = await giftVoucherService.GetGiftVoucherById(x.Id);+ if(giftVoucher.GiftVoucherTypeId == GiftVoucherType.Physical)+ context.AddFailure("Only virtual type gift voucher can notify recipient");++ if (!CommonHelper.IsValidEmail(giftVoucher.RecipientEmail))+ context.AddFailure("Recipient email is not valid");+ if (!CommonHelper.IsValidEmail(giftVoucher.SenderEmail))+ context.AddFailure("Sender email is not valid");+ });+ }+ }+}\ No newline at end of file