Skip to content

Commit

Permalink
PT-13171: Empty Store Notification log (#141)
Browse files Browse the repository at this point in the history
fix: Empty Store Notification log (#141)
  • Loading branch information
OlegoO committed Aug 16, 2023
1 parent 33f63a2 commit 932b136
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,33 @@ public static async Task<Notification> GetNotificationAsync(this INotificationSe
{
throw new ArgumentNullException(nameof(service));
}

var criteria = AbstractTypeFactory<NotificationSearchCriteria>.TryCreateInstance();
criteria.NotificationType = notificationType;

//try to get with the Tenant
criteria.Take = 1;
criteria.ResponseGroup = responseGroup;

if (tenant != null && !tenant.IsEmpty)
{
criteria.TenantId = tenant.Id;
criteria.TenantType = tenant.Type;
}
var searchResult = await service.SearchNotificationsAsync(criteria);

var searchResult = await service.SearchNotificationsAsync(criteria);
var result = searchResult?.Results.FirstOrDefault(x => x.TenantIdentity == tenant);

if (result == null)
{
//Find first global notification (without tenant)
criteria.TenantId = null;
criteria.TenantType = null;

searchResult = await service.SearchNotificationsAsync(criteria);
result = searchResult?.Results.FirstOrDefault(x => x.TenantIdentity.IsEmpty);

if (result != null)
{
result.TenantIdentity = tenant;
}
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,29 @@ public NotificationSearchServiceUnitTests()
_notificationRegistrar = new NotificationRegistrar(null);
_notificationSearchService = new NotificationSearchService(_repositoryFactory, _notificationServiceMock.Object, _memCache);


var criteria = AbstractTypeFactory<NotificationSearchCriteria>.TryCreateInstance();
criteria.Take = 1;
criteria.NotificationType = nameof(InvoiceEmailNotification);
_notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult());
_notificationRegistrar.RegisterNotification<InvoiceEmailNotification>();

criteria.NotificationType = nameof(OrderSentEmailNotification);
_notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult());
_notificationRegistrar.RegisterNotification<OrderSentEmailNotification>();

criteria.NotificationType = nameof(OrderPaidEmailNotification);
_notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult());
_notificationRegistrar.RegisterNotification<OrderPaidEmailNotification>();

criteria.NotificationType = nameof(RemindUserNameEmailNotification);
_notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult());
_notificationRegistrar.RegisterNotification<RemindUserNameEmailNotification>();

criteria.NotificationType = nameof(RegistrationEmailNotification);
_notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult());
_notificationRegistrar.RegisterNotification<RegistrationEmailNotification>();


}

Expand Down Expand Up @@ -125,7 +125,7 @@ public async Task GetNotificationByAliasAsync_ReturnNotification()
criteria4.Take = 1;
criteria4.NotificationType = type;
_notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria4)).ReturnsAsync(new NotificationSearchResult());

var mockNotifications = notifications.AsQueryable().BuildMock();
_repositoryMock.Setup(r => r.Notifications).Returns(mockNotifications.Object);
var ids = notifications.Select(n => n.Id).ToArray();
Expand Down Expand Up @@ -165,7 +165,7 @@ public async Task SearchNotificationsAsync_GetOneItem()
var ids = notifications.Select(n => n.Id).ToArray();
_notificationServiceMock.Setup(ns => ns.GetByIdsAsync(ids, null))
.ReturnsAsync(notifications.Select(n => n.ToModel(AbstractTypeFactory<Notification>.TryCreateInstance(n.Type))).ToArray());


//Act
var result = await _notificationSearchService.SearchNotificationsAsync(searchCriteria);
Expand Down Expand Up @@ -196,7 +196,7 @@ public async Task SearchNotificationsAsync_ContainsActiveNotifications()
var ids = notificationEntities.Select(n => n.Id).ToArray();
_notificationServiceMock.Setup(ns => ns.GetByIdsAsync(ids, responseGroup))
.ReturnsAsync(notifications);


//Act
var result = await _notificationSearchService.SearchNotificationsAsync(searchCriteria);
Expand Down Expand Up @@ -225,7 +225,7 @@ public async Task SearchNotificationsAsync_PagingNotifications(int skip, int tak
var notifications = notificationEntities.Select(n => n.ToModel(AbstractTypeFactory<Notification>.TryCreateInstance(n.Type))).ToArray();
_notificationServiceMock.Setup(ns => ns.GetByIdsAsync(It.IsAny<string[]>(), responseGroup))
.ReturnsAsync(notifications.Where(n => expectedTypes.Contains(n.Type)).ToArray());

//Act
var result = await _notificationSearchService.SearchNotificationsAsync(searchCriteria);

Expand Down Expand Up @@ -256,7 +256,7 @@ public async Task GetNotificationAsync_GetByTenant()
var notifications = notificationEntities.Select(n => n.ToModel(AbstractTypeFactory<Notification>.TryCreateInstance(n.Type))).ToArray();
_notificationServiceMock.Setup(ns => ns.GetByIdsAsync(It.IsAny<string[]>(), null))
.ReturnsAsync(notifications.ToArray());


//Act
var result = await NotificationSearchServiceExtensions.GetNotificationAsync<RegistrationEmailNotification>(_notificationSearchService, searchTenant);
Expand All @@ -267,6 +267,37 @@ public async Task GetNotificationAsync_GetByTenant()
Assert.Equal(searchType, result.Type);
}

[Fact]
public async Task GetNotificationAsync_GetByTenant_WithGlobal()
{
//Arrange
var searchTenant = new TenantIdentity(Guid.NewGuid().ToString(), "Store");
var searchType = nameof(RegistrationEmailNotification);
var searchCriteria = AbstractTypeFactory<NotificationSearchCriteria>.TryCreateInstance();
searchCriteria.Take = 1;
searchCriteria.Skip = 0;
searchCriteria.TenantId = searchTenant.Id;
searchCriteria.TenantType = searchTenant.Type;
searchCriteria.NotificationType = searchType;
var notificationEntities = new List<NotificationEntity> {
new EmailNotificationEntity { Type = searchType, Kind = nameof(EmailNotification), Id = Guid.NewGuid().ToString(), TenantId = null, TenantType = null },
new EmailNotificationEntity { Type = searchType, Kind = nameof(EmailNotification), Id = Guid.NewGuid().ToString(), TenantId = "someId", TenantType = "Store" },
};
var mockNotifications = notificationEntities.AsQueryable().BuildMock();
_repositoryMock.Setup(r => r.Notifications).Returns(mockNotifications.Object);
var notifications = notificationEntities.Select(n => n.ToModel(AbstractTypeFactory<Notification>.TryCreateInstance(n.Type))).ToArray();
_notificationServiceMock.Setup(ns => ns.GetByIdsAsync(It.IsAny<string[]>(), null))
.ReturnsAsync(notifications.ToArray());


//Act
var result = await NotificationSearchServiceExtensions.GetNotificationAsync<RegistrationEmailNotification>(_notificationSearchService, searchTenant);

//Assert
Assert.Equal(searchTenant.Id, result.TenantIdentity.Id);
Assert.Equal(searchTenant.Type, result.TenantIdentity.Type);
Assert.Equal(searchType, result.Type);
}

[Fact]
public async Task SearchNotificationsAsync_GetExtendedNotificationWithBaseType()
Expand All @@ -293,7 +324,7 @@ public async Task SearchNotificationsAsync_GetExtendedNotificationWithBaseType()
var notifications = notificationEntities.Select(n => n.ToModel(AbstractTypeFactory<Notification>.TryCreateInstance(n.Type))).ToArray();
_notificationServiceMock.Setup(ns => ns.GetByIdsAsync(It.IsAny<string[]>(), searchCriteria.ResponseGroup))
.ReturnsAsync(notifications.Where(x => x.Id.EqualsInvariant(sampleNotificationEntity.Id)).ToArray());


//Act
var result = (await _notificationSearchService.SearchNotificationsAsync(searchCriteria)).Results.FirstOrDefault();
Expand Down

0 comments on commit 932b136

Please sign in to comment.