Mango.Nop.Plugins/Nop.Plugin.Misc.AIPlugin/Services/FruitBankNotificationServic...

85 lines
3.9 KiB
C#

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";
/// <summary>
/// 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.
/// </summary>
public async Task<int> SendOrderStartedCustomerNotificationAsync(Order order, bool isMeasurable)
{
var measurableNote = isMeasurable
? "<p>Rendel&#233;sed m&#233;rhet&#337; t&#233;teleket tartalmaz. A v&#233;gleges &#225;r a m&#233;r&#233;s ut&#225;n ker&#252;l meger&#337;s&#237;t&#233;sre.</p>"
: string.Empty;
return await SendNotificationAsync(ORDER_STARTED_TEMPLATE_NAME, order, measurableNote);
}
/// <summary>
/// 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.
/// </summary>
public async Task<int> SendOrderAuditedCustomerNotificationAsync(Order order, bool isMeasurable)
{
var measurableNote = isMeasurable
? "<p>A m&#233;rt t&#233;telek s&#250;lyait r&#246;gz&#237;tett&#252;k, a v&#233;gleges &#225;r a rendel&#233;sen felt&#252;ntetett &#246;sszeg.</p>"
: string.Empty;
return await SendNotificationAsync(ORDER_AUDITED_TEMPLATE_NAME, order, measurableNote);
}
// ── shared core ─────────────────────────────────────────────────────────
private async Task<int> 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<Token>();
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);
}
}