diff --git a/src/VirtoCommerce.NotificationsModule.Core/Extensions/NotificationSearchServiceExtensions.cs b/src/VirtoCommerce.NotificationsModule.Core/Extensions/NotificationSearchServiceExtensions.cs index 7f7a9d7d..5deba0b9 100644 --- a/src/VirtoCommerce.NotificationsModule.Core/Extensions/NotificationSearchServiceExtensions.cs +++ b/src/VirtoCommerce.NotificationsModule.Core/Extensions/NotificationSearchServiceExtensions.cs @@ -25,27 +25,33 @@ public static async Task GetNotificationAsync(this INotificationSe { throw new ArgumentNullException(nameof(service)); } + var criteria = AbstractTypeFactory.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; diff --git a/tests/VirtoCommerce.NotificationsModule.Tests/UnitTests/NotificationSearchServiceUnitTests.cs b/tests/VirtoCommerce.NotificationsModule.Tests/UnitTests/NotificationSearchServiceUnitTests.cs index b4370b5a..696a2f53 100644 --- a/tests/VirtoCommerce.NotificationsModule.Tests/UnitTests/NotificationSearchServiceUnitTests.cs +++ b/tests/VirtoCommerce.NotificationsModule.Tests/UnitTests/NotificationSearchServiceUnitTests.cs @@ -50,29 +50,29 @@ public NotificationSearchServiceUnitTests() _notificationRegistrar = new NotificationRegistrar(null); _notificationSearchService = new NotificationSearchService(_repositoryFactory, _notificationServiceMock.Object, _memCache); - + var criteria = AbstractTypeFactory.TryCreateInstance(); criteria.Take = 1; criteria.NotificationType = nameof(InvoiceEmailNotification); _notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult()); _notificationRegistrar.RegisterNotification(); - + criteria.NotificationType = nameof(OrderSentEmailNotification); _notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult()); _notificationRegistrar.RegisterNotification(); - + criteria.NotificationType = nameof(OrderPaidEmailNotification); _notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult()); _notificationRegistrar.RegisterNotification(); - + criteria.NotificationType = nameof(RemindUserNameEmailNotification); _notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult()); _notificationRegistrar.RegisterNotification(); - + criteria.NotificationType = nameof(RegistrationEmailNotification); _notificationSearchServiceMock.Setup(x => x.SearchNotificationsAsync(criteria)).ReturnsAsync(new NotificationSearchResult()); _notificationRegistrar.RegisterNotification(); - + } @@ -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(); @@ -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.TryCreateInstance(n.Type))).ToArray()); - + //Act var result = await _notificationSearchService.SearchNotificationsAsync(searchCriteria); @@ -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); @@ -225,7 +225,7 @@ public async Task SearchNotificationsAsync_PagingNotifications(int skip, int tak var notifications = notificationEntities.Select(n => n.ToModel(AbstractTypeFactory.TryCreateInstance(n.Type))).ToArray(); _notificationServiceMock.Setup(ns => ns.GetByIdsAsync(It.IsAny(), responseGroup)) .ReturnsAsync(notifications.Where(n => expectedTypes.Contains(n.Type)).ToArray()); - + //Act var result = await _notificationSearchService.SearchNotificationsAsync(searchCriteria); @@ -256,7 +256,7 @@ public async Task GetNotificationAsync_GetByTenant() var notifications = notificationEntities.Select(n => n.ToModel(AbstractTypeFactory.TryCreateInstance(n.Type))).ToArray(); _notificationServiceMock.Setup(ns => ns.GetByIdsAsync(It.IsAny(), null)) .ReturnsAsync(notifications.ToArray()); - + //Act var result = await NotificationSearchServiceExtensions.GetNotificationAsync(_notificationSearchService, searchTenant); @@ -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.TryCreateInstance(); + searchCriteria.Take = 1; + searchCriteria.Skip = 0; + searchCriteria.TenantId = searchTenant.Id; + searchCriteria.TenantType = searchTenant.Type; + searchCriteria.NotificationType = searchType; + var notificationEntities = new List { + 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.TryCreateInstance(n.Type))).ToArray(); + _notificationServiceMock.Setup(ns => ns.GetByIdsAsync(It.IsAny(), null)) + .ReturnsAsync(notifications.ToArray()); + + + //Act + var result = await NotificationSearchServiceExtensions.GetNotificationAsync(_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() @@ -293,7 +324,7 @@ public async Task SearchNotificationsAsync_GetExtendedNotificationWithBaseType() var notifications = notificationEntities.Select(n => n.ToModel(AbstractTypeFactory.TryCreateInstance(n.Type))).ToArray(); _notificationServiceMock.Setup(ns => ns.GetByIdsAsync(It.IsAny(), searchCriteria.ResponseGroup)) .ReturnsAsync(notifications.Where(x => x.Id.EqualsInvariant(sampleNotificationEntity.Id)).ToArray()); - + //Act var result = (await _notificationSearchService.SearchNotificationsAsync(searchCriteria)).Results.FirstOrDefault();