-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor order notification with new command and handler
- Loading branch information
1 parent
63aadfb
commit b8ac6a9
Showing
3 changed files
with
130 additions
and
102 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...iness/Grand.Business.Checkout/Commands/Handlers/Orders/OrderNotificationCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using Grand.Business.Core.Commands.Messages.Common; | ||
using Grand.Business.Core.Interfaces.Checkout.Orders; | ||
using Grand.Business.Core.Interfaces.Common.Pdf; | ||
using Grand.Business.Core.Interfaces.Messages; | ||
using Grand.Business.Core.Queries.Checkout.Orders; | ||
using Grand.Domain.Localization; | ||
using Grand.Domain.Orders; | ||
using MediatR; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Grand.Business.Checkout.Commands.Handlers.Orders; | ||
|
||
public class OrderNotificationCommandHandler : IRequestHandler<OrderNotificationCommand> | ||
{ | ||
private readonly IOrderService _orderService; | ||
private readonly IMessageProviderService _messageProviderService; | ||
private readonly OrderSettings _orderSettings; | ||
private readonly LanguageSettings _languageSettings; | ||
private readonly IPdfService _pdfService; | ||
private readonly ILogger<OrderNotificationCommandHandler> _logger; | ||
private readonly IMediator _mediator; | ||
|
||
public OrderNotificationCommandHandler( | ||
IOrderService orderService, | ||
IMessageProviderService messageProviderService, | ||
OrderSettings orderSettings, | ||
LanguageSettings languageSettings, | ||
IPdfService pdfService, | ||
ILogger<OrderNotificationCommandHandler> logger, | ||
IMediator mediator) | ||
{ | ||
_orderService = orderService; | ||
_messageProviderService = messageProviderService; | ||
_orderSettings = orderSettings; | ||
_languageSettings = languageSettings; | ||
_pdfService = pdfService; | ||
_logger = logger; | ||
_mediator = mediator; | ||
} | ||
|
||
public async Task Handle(OrderNotificationCommand request, CancellationToken cancellationToken) | ||
{ | ||
|
||
try | ||
{ | ||
if (request.WorkContext.OriginalCustomerIfImpersonated != null) | ||
//this order is placed by a store administrator impersonating a customer | ||
await _orderService.InsertOrderNote(new OrderNote { | ||
Note = | ||
$"Order placed by a store owner ('{request.WorkContext.OriginalCustomerIfImpersonated.Email}'. ID = {request.WorkContext.OriginalCustomerIfImpersonated.Id}) impersonating the customer.", | ||
DisplayToCustomer = false, | ||
OrderId = request.Order.Id | ||
}); | ||
else | ||
await _orderService.InsertOrderNote(new OrderNote { | ||
Note = "Order placed", | ||
DisplayToCustomer = false, | ||
OrderId = request.Order.Id | ||
}); | ||
|
||
//send email notifications | ||
await _messageProviderService.SendOrderPlacedStoreOwnerMessage(request.Order, request.WorkContext.CurrentCustomer, | ||
_languageSettings.DefaultAdminLanguageId); | ||
|
||
string orderPlacedAttachmentFilePath = string.Empty, orderPlacedAttachmentFileName = string.Empty; | ||
var orderPlacedAttachments = new List<string>(); | ||
|
||
try | ||
{ | ||
orderPlacedAttachmentFilePath = | ||
_orderSettings.AttachPdfInvoiceToOrderPlacedEmail && !_orderSettings.AttachPdfInvoiceToBinary | ||
? await _pdfService.PrintOrderToPdf(request.Order, request.Order.CustomerLanguageId) | ||
: null; | ||
orderPlacedAttachmentFileName = | ||
_orderSettings.AttachPdfInvoiceToOrderPlacedEmail && !_orderSettings.AttachPdfInvoiceToBinary | ||
? "order.pdf" | ||
: null; | ||
orderPlacedAttachments = _orderSettings.AttachPdfInvoiceToOrderPlacedEmail && | ||
_orderSettings.AttachPdfInvoiceToBinary | ||
? [ | ||
await _pdfService.SaveOrderToBinary(request.Order, request.Order.CustomerLanguageId) | ||
] | ||
: []; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error - order placed attachment file {OrderOrderNumber}", request.Order.OrderNumber); | ||
} | ||
|
||
await _messageProviderService.SendOrderPlacedCustomerMessage(request.Order, request.WorkContext.CurrentCustomer, request.Order.CustomerLanguageId, orderPlacedAttachmentFilePath, orderPlacedAttachmentFileName, orderPlacedAttachments); | ||
|
||
if (request.Order.OrderItems.Any(x => !string.IsNullOrEmpty(x.VendorId))) | ||
{ | ||
var vendors = await _mediator.Send(new GetVendorsInOrderQuery { Order = request.Order }); | ||
foreach (var vendor in vendors) | ||
await _messageProviderService.SendOrderPlacedVendorMessage(request.Order, request.WorkContext.CurrentCustomer, vendor, | ||
_languageSettings.DefaultAdminLanguageId); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Place order send notification error"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Business/Grand.Business.Core/Commands/Messages/Common/OrderNotificationCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Grand.Domain.Customers; | ||
using Grand.Domain.Orders; | ||
using Grand.Infrastructure; | ||
using MediatR; | ||
|
||
namespace Grand.Business.Core.Commands.Messages.Common; | ||
|
||
public class OrderNotificationCommand : IRequest | ||
{ | ||
public Order Order { get; set; } | ||
public IWorkContext WorkContext { get; set; } | ||
} |