using Nop.Core; using Nop.Core.Domain.Messages; using Nop.Core.Domain.Orders; using Nop.Services.Customers; using Nop.Services.Messages; namespace Nop.Plugin.Misc.FruitBankPlugin.Services; public class FruitBankNotificationService( IMessageTemplateService messageTemplateService, IEmailAccountService emailAccountService, EmailAccountSettings emailAccountSettings, IMessageTokenProvider messageTokenProvider, IWorkflowMessageService workflowMessageService, ICustomerService customerService, IStoreContext storeContext) { public const string ORDER_AUDITED_TEMPLATE_NAME = "FruitBank.OrderAudited.CustomerNotification"; public const string ORDER_STARTED_TEMPLATE_NAME = "FruitBank.OrderStarted.CustomerNotification"; /// /// Sends the "order started" (being prepared) customer notification. /// For measurable orders, informs the customer that final prices will be /// confirmed after weighing. Fires once when MeasuringStatus transitions to Started. /// public async Task SendOrderStartedCustomerNotificationAsync(Order order, bool isMeasurable) { var measurableNote = isMeasurable ? "

Rendelésed mérhető tételeket tartalmaz. A végleges ár a mérés után kerül megerősítésre.

" : string.Empty; return await SendNotificationAsync(ORDER_STARTED_TEMPLATE_NAME, order, measurableNote); } /// /// Sends the "order audited" customer notification. /// For measurable orders, confirms that weights have been recorded and /// the final price is as shown on the order. /// Fires once when MeasuringStatus transitions to Audited. /// public async Task SendOrderAuditedCustomerNotificationAsync(Order order, bool isMeasurable) { var measurableNote = isMeasurable ? "

A mért tételek súlyait rögzítettük, a végleges ár a rendelésen feltüntetett összeg.

" : string.Empty; return await SendNotificationAsync(ORDER_AUDITED_TEMPLATE_NAME, order, measurableNote); } // ── shared core ───────────────────────────────────────────────────────── private async Task SendNotificationAsync(string templateName, Order order, string measurableNote) { var store = await storeContext.GetCurrentStoreAsync(); var templates = await messageTemplateService.GetMessageTemplatesByNameAsync(templateName, store.Id); var messageTemplate = templates?.FirstOrDefault(); if (messageTemplate is null || !messageTemplate.IsActive) return 0; var emailAccount = await emailAccountService.GetEmailAccountByIdAsync(messageTemplate.EmailAccountId) ?? await emailAccountService.GetEmailAccountByIdAsync(emailAccountSettings.DefaultEmailAccountId); var tokens = new List(); await messageTokenProvider.AddStoreTokensAsync(tokens, store, emailAccount, order.CustomerLanguageId); await messageTokenProvider.AddOrderTokensAsync(tokens, order, order.CustomerLanguageId); var customer = await customerService.GetCustomerByIdAsync(order.CustomerId); await messageTokenProvider.AddCustomerTokensAsync(tokens, customer); tokens.Add(new Token("Order.MeasurableNote", measurableNote, true)); var toEmail = customer.Email; var toName = $"{customer.FirstName} {customer.LastName}".Trim(); if (string.IsNullOrWhiteSpace(toName)) toName = customer.Email; return await workflowMessageService.SendNotificationAsync( messageTemplate, emailAccount, order.CustomerLanguageId, tokens, toEmail, toName); } }