-
Notifications
You must be signed in to change notification settings - Fork 525
Expand file tree
/
Copy pathSendNotificationsToSubscribersCommandHandler.cs
More file actions
77 lines (67 loc) · 3.26 KB
/
SendNotificationsToSubscribersCommandHandler.cs
File metadata and controls
77 lines (67 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Grand.Business.Core.Commands.Catalog;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Business.Core.Interfaces.Messages;
using Grand.Data;
using Grand.Domain.Catalog;
using Grand.Domain.Common;
using Grand.Domain.Customers;
using Grand.SharedKernel.Extensions;
using MediatR;
namespace Grand.Module.ScheduledTasks.Commands.Handlers.Catalog;
public class
SendNotificationsToSubscribersCommandHandler : IRequestHandler<SendNotificationsToSubscribersCommand,
IList<OutOfStockSubscription>>
{
private readonly ICustomerService _customerService;
private readonly IMessageProviderService _messageProviderService;
private readonly IRepository<OutOfStockSubscription> _outOfStockSubscriptionRepository;
public SendNotificationsToSubscribersCommandHandler(
ICustomerService customerService,
IMessageProviderService messageProviderService,
IRepository<OutOfStockSubscription> outOfStockSubscriptionRepository)
{
_customerService = customerService;
_messageProviderService = messageProviderService;
_outOfStockSubscriptionRepository = outOfStockSubscriptionRepository;
}
public async Task<IList<OutOfStockSubscription>> Handle(SendNotificationsToSubscribersCommand request,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(request.Product);
var subscriptions =
await GetAllSubscriptionsByProductId(request.Product.Id, request.Attributes, request.Warehouse);
foreach (var subscription in subscriptions)
{
var customer = await _customerService.GetCustomerById(subscription.CustomerId);
//ensure that customer is registered (simple and fast way)
if (customer == null || !CommonHelper.IsValidEmail(customer.Email)) continue;
var customerLanguageId =
customer.GetUserFieldFromEntity<string>(SystemCustomerFieldNames.LanguageId, subscription.StoreId);
await _messageProviderService.SendBackInStockMessage(customer, request.Product, subscription,
customerLanguageId);
}
return subscriptions;
}
/// <summary>
/// Gets all subscriptions
/// </summary>
/// <param name="productId">Product identifier</param>
/// <param name="attributes">Attributes</param>
/// <param name="warehouseId">Store identifier; pass "" to load all records</param>
/// <returns>Subscriptions</returns>
private async Task<IList<OutOfStockSubscription>> GetAllSubscriptionsByProductId(string productId,
IList<CustomAttribute> attributes, string warehouseId)
{
var query = _outOfStockSubscriptionRepository.Table
.Where(biss => biss.ProductId == productId)
.OrderByDescending(biss => biss.CreatedOnUtc).ToList();
//warehouse
if (!string.IsNullOrEmpty(warehouseId))
query = query.Where(biss => biss.WarehouseId == warehouseId).ToList();
//attributes
if (attributes == null || !attributes.Any()) return await Task.FromResult(query.ToList());
foreach (var item in attributes)
query = query.Where(x => x.Attributes.Any(y => y.Key == item.Key && y.Value == item.Value)).ToList();
return await Task.FromResult(query.ToList());
}
}