From 8d9006d348580b6b6c47b4ccb05c8565366fcae0 Mon Sep 17 00:00:00 2001 From: Ivan Angelov Date: Mon, 8 Jun 2026 04:35:52 +0300 Subject: [PATCH 1/3] Fixed `UserViewedProduct` event --- ...ProductViewedHandler.cs => UserViewedProductHandler.cs} | 6 +++--- .../Application/ProductViewed/ProductViewedHandler.cs | 3 ++- .../Account/Accounts/UserViewedProductApplicationEvent.cs | 7 +++++++ .../Accounts/Events/Application/Viewed/Tests.cs | 6 +++--- .../Products/Events/Application/Viewed/Tests.cs | 3 ++- 5 files changed, 17 insertions(+), 8 deletions(-) rename src/Modules/Accounts/Application/Accounts/Events/Application/{ProductViewedHandler.cs => UserViewedProductHandler.cs} (63%) create mode 100644 src/Shared/Application/Events/Account/Accounts/UserViewedProductApplicationEvent.cs diff --git a/src/Modules/Accounts/Application/Accounts/Events/Application/ProductViewedHandler.cs b/src/Modules/Accounts/Application/Accounts/Events/Application/UserViewedProductHandler.cs similarity index 63% rename from src/Modules/Accounts/Application/Accounts/Events/Application/ProductViewedHandler.cs rename to src/Modules/Accounts/Application/Accounts/Events/Application/UserViewedProductHandler.cs index f6869c540..aa02232c6 100644 --- a/src/Modules/Accounts/Application/Accounts/Events/Application/ProductViewedHandler.cs +++ b/src/Modules/Accounts/Application/Accounts/Events/Application/UserViewedProductHandler.cs @@ -1,12 +1,12 @@ using CustomCADs.Modules.Accounts.Domain.Repositories; using CustomCADs.Modules.Accounts.Domain.Repositories.Writes; -using CustomCADs.Shared.Application.Events.Catalog; +using CustomCADs.Shared.Application.Events.Account.Accounts; namespace CustomCADs.Modules.Accounts.Application.Accounts.Events.Application; -public class ProductViewedHandler(IAccountWrites writes, IUnitOfWork uow) +public class UserViewedProductHandler(IAccountWrites writes, IUnitOfWork uow) { - public async Task HandleAsync(ProductViewedApplicationEvent @event) + public async Task HandleAsync(UserViewedProductApplicationEvent @event) { await writes.ViewProductAsync(@event.AccountId, @event.Id, @event.ViewedAt).ConfigureAwait(false); await uow.SaveChangesAsync().ConfigureAwait(false); diff --git a/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs b/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs index 4d4afb64c..8d98c0fd1 100644 --- a/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs +++ b/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs @@ -2,6 +2,7 @@ using CustomCADs.Modules.Catalog.Domain.Repositories.Reads; using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Abstractions.Requests.Sender; +using CustomCADs.Shared.Application.Events.Account.Accounts; using CustomCADs.Shared.Application.Events.Catalog; using CustomCADs.Shared.Application.UseCases.Accounts.Queries; @@ -33,7 +34,7 @@ public async Task HandleAsync(ProductViewedApplicationEvent @event) await uow.SaveChangesAsync().ConfigureAwait(false); await raiser.RaiseApplicationEventAsync( - @event: new ProductViewedApplicationEvent( + @event: new UserViewedProductApplicationEvent( Id: @event.Id, AccountId: @event.AccountId, ViewedAt: @event.ViewedAt diff --git a/src/Shared/Application/Events/Account/Accounts/UserViewedProductApplicationEvent.cs b/src/Shared/Application/Events/Account/Accounts/UserViewedProductApplicationEvent.cs new file mode 100644 index 000000000..4c0247940 --- /dev/null +++ b/src/Shared/Application/Events/Account/Accounts/UserViewedProductApplicationEvent.cs @@ -0,0 +1,7 @@ +namespace CustomCADs.Shared.Application.Events.Account.Accounts; + +public record UserViewedProductApplicationEvent( + ProductId Id, + AccountId AccountId, + DateTimeOffset ViewedAt +) : BaseApplicationEvent; diff --git a/tests/UnitTests/Accounts/Application/Accounts/Events/Application/Viewed/Tests.cs b/tests/UnitTests/Accounts/Application/Accounts/Events/Application/Viewed/Tests.cs index 47a10e974..7032fe555 100644 --- a/tests/UnitTests/Accounts/Application/Accounts/Events/Application/Viewed/Tests.cs +++ b/tests/UnitTests/Accounts/Application/Accounts/Events/Application/Viewed/Tests.cs @@ -1,7 +1,7 @@ using CustomCADs.Modules.Accounts.Application.Accounts.Events.Application; using CustomCADs.Modules.Accounts.Domain.Repositories; using CustomCADs.Modules.Accounts.Domain.Repositories.Writes; -using CustomCADs.Shared.Application.Events.Catalog; +using CustomCADs.Shared.Application.Events.Account.Accounts; namespace CustomCADs.UnitTests.Accounts.Application.Accounts.Events.Application.Viewed; @@ -9,8 +9,8 @@ namespace CustomCADs.UnitTests.Accounts.Application.Accounts.Events.Application. public class Tests : Data.Accounts.BaseUnitTests { - private readonly ProductViewedHandler handler; - private readonly ProductViewedApplicationEvent request = new(ValidProductId, ValidId, ViewedAt); + private readonly UserViewedProductHandler handler; + private readonly UserViewedProductApplicationEvent request = new(ValidProductId, ValidId, ViewedAt); private readonly Mock writes = new(); private readonly Mock uow = new(); diff --git a/tests/UnitTests/Catalog/Application/Products/Events/Application/Viewed/Tests.cs b/tests/UnitTests/Catalog/Application/Products/Events/Application/Viewed/Tests.cs index fffcf0733..cefe96735 100644 --- a/tests/UnitTests/Catalog/Application/Products/Events/Application/Viewed/Tests.cs +++ b/tests/UnitTests/Catalog/Application/Products/Events/Application/Viewed/Tests.cs @@ -3,6 +3,7 @@ using CustomCADs.Modules.Catalog.Domain.Repositories.Reads; using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Abstractions.Requests.Sender; +using CustomCADs.Shared.Application.Events.Account.Accounts; using CustomCADs.Shared.Application.Events.Catalog; using CustomCADs.Shared.Application.Exceptions; using CustomCADs.Shared.Application.UseCases.Accounts.Queries; @@ -129,7 +130,7 @@ public async Task Handle_ShouldRaiseEvents() // Assert raiser.Verify( x => x.RaiseApplicationEventAsync( - It.Is(x => x.Id == ValidId && x.AccountId == ValidCreatorId) + It.Is(x => x.Id == ValidId && x.AccountId == ValidCreatorId) ), Times.Once() ); From de8084fe65f02a1fb3dfaa124e1733094b5e5119 Mon Sep 17 00:00:00 2001 From: Ivan Angelov Date: Mon, 8 Jun 2026 20:59:40 +0300 Subject: [PATCH 2/3] Added `IEventHandler` interface --- .../Accounts/Events/Application/UserDeletedHandler.cs | 2 ++ .../Accounts/Events/Application/UserEditedHandler.cs | 2 ++ .../Accounts/Events/Application/UserViewedProductHandler.cs | 2 ++ .../Application/ProductDeleted/ProductDeletedHandler.cs | 2 ++ .../DeliveryRequested/ActiveCartDeliveryRequestedHandler.cs | 3 ++- .../PaymentCompleted/CartPaymentCompletedHandler.cs | 3 ++- .../Application/ProductCreated/ProductCreatedHandler.cs | 2 ++ .../ProductPurchased/ProductsPurchasedHandler.cs | 6 ++++-- .../Application/ProductViewed/ProductViewedHandler.cs | 1 + .../DeliveryRequested/CustomDeliveryRequestedHandler.cs | 3 ++- .../PaymentCompleted/CustomPaymentCompletedHandler.cs | 3 ++- .../Cads/Events/Application/ProductDeletedHandler.cs | 2 ++ .../Images/Events/Application/ProductDeletedHandler.cs | 2 ++ .../EmailVerificationRequestedEventHandler.cs | 2 ++ .../PasswordReset/PasswordResetRequestedEventHandler.cs | 2 ++ .../Users/Events/Application/Roles/RoleCreatedHandler.cs | 5 +++-- .../Users/Events/Application/Roles/RoleDeletedHandler.cs | 5 +++-- .../Users/Events/Application/Users/AccountCreatedHandler.cs | 4 +++- .../Users/Events/Application/Users/AccountDeletedHandler.cs | 4 +++- .../Notifications/Events/NotificationRequestedHandler.cs | 2 ++ src/Shared/Application/Abstractions/Events/IEventHandler.cs | 6 ++++++ 21 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 src/Shared/Application/Abstractions/Events/IEventHandler.cs diff --git a/src/Modules/Accounts/Application/Accounts/Events/Application/UserDeletedHandler.cs b/src/Modules/Accounts/Application/Accounts/Events/Application/UserDeletedHandler.cs index 21e09f5e2..44e0a6df3 100644 --- a/src/Modules/Accounts/Application/Accounts/Events/Application/UserDeletedHandler.cs +++ b/src/Modules/Accounts/Application/Accounts/Events/Application/UserDeletedHandler.cs @@ -1,11 +1,13 @@ using CustomCADs.Modules.Accounts.Domain.Repositories; using CustomCADs.Modules.Accounts.Domain.Repositories.Reads; using CustomCADs.Modules.Accounts.Domain.Repositories.Writes; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Events.Identity; namespace CustomCADs.Modules.Accounts.Application.Accounts.Events.Application; public class UserDeletedHandler(IAccountReads reads, IAccountWrites writes, IUnitOfWork uow) + : IEventHandler { public async Task HandleAsync(UserDeletedApplicationEvent @event) { diff --git a/src/Modules/Accounts/Application/Accounts/Events/Application/UserEditedHandler.cs b/src/Modules/Accounts/Application/Accounts/Events/Application/UserEditedHandler.cs index 4bc2e6d73..63201e095 100644 --- a/src/Modules/Accounts/Application/Accounts/Events/Application/UserEditedHandler.cs +++ b/src/Modules/Accounts/Application/Accounts/Events/Application/UserEditedHandler.cs @@ -1,10 +1,12 @@ using CustomCADs.Modules.Accounts.Domain.Repositories; using CustomCADs.Modules.Accounts.Domain.Repositories.Reads; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Events.Identity; namespace CustomCADs.Modules.Accounts.Application.Accounts.Events.Application; public class UserEditedHandler(IAccountReads reads, IUnitOfWork uow) + : IEventHandler { public async Task HandleAsync(UserEditedApplicationEvent @event) { diff --git a/src/Modules/Accounts/Application/Accounts/Events/Application/UserViewedProductHandler.cs b/src/Modules/Accounts/Application/Accounts/Events/Application/UserViewedProductHandler.cs index aa02232c6..9cb246a8b 100644 --- a/src/Modules/Accounts/Application/Accounts/Events/Application/UserViewedProductHandler.cs +++ b/src/Modules/Accounts/Application/Accounts/Events/Application/UserViewedProductHandler.cs @@ -1,10 +1,12 @@ using CustomCADs.Modules.Accounts.Domain.Repositories; using CustomCADs.Modules.Accounts.Domain.Repositories.Writes; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Events.Account.Accounts; namespace CustomCADs.Modules.Accounts.Application.Accounts.Events.Application; public class UserViewedProductHandler(IAccountWrites writes, IUnitOfWork uow) + : IEventHandler { public async Task HandleAsync(UserViewedProductApplicationEvent @event) { diff --git a/src/Modules/Carts/Application/ActiveCarts/Events/Application/ProductDeleted/ProductDeletedHandler.cs b/src/Modules/Carts/Application/ActiveCarts/Events/Application/ProductDeleted/ProductDeletedHandler.cs index a83ee115a..945a3db20 100644 --- a/src/Modules/Carts/Application/ActiveCarts/Events/Application/ProductDeleted/ProductDeletedHandler.cs +++ b/src/Modules/Carts/Application/ActiveCarts/Events/Application/ProductDeleted/ProductDeletedHandler.cs @@ -1,9 +1,11 @@ using CustomCADs.Modules.Carts.Domain.Repositories; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Events.Catalog; namespace CustomCADs.Modules.Carts.Application.ActiveCarts.Events.Application.ProductDeleted; public class ProductDeletedHandler(IUnitOfWork uow) + : IEventHandler { public async Task HandleAsync(ProductDeletedApplicationEvent @event) { diff --git a/src/Modules/Carts/Application/PurchasedCarts/Events/Application/DeliveryRequested/ActiveCartDeliveryRequestedHandler.cs b/src/Modules/Carts/Application/PurchasedCarts/Events/Application/DeliveryRequested/ActiveCartDeliveryRequestedHandler.cs index 919e6f03b..923d599d8 100644 --- a/src/Modules/Carts/Application/PurchasedCarts/Events/Application/DeliveryRequested/ActiveCartDeliveryRequestedHandler.cs +++ b/src/Modules/Carts/Application/PurchasedCarts/Events/Application/DeliveryRequested/ActiveCartDeliveryRequestedHandler.cs @@ -1,6 +1,7 @@ using CustomCADs.Modules.Carts.Application.ActiveCarts.Events.Application.DeliveryRequested; using CustomCADs.Modules.Carts.Domain.Repositories; using CustomCADs.Modules.Carts.Domain.Repositories.Reads; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Abstractions.Requests.Sender; using CustomCADs.Shared.Application.UseCases.Accounts.Queries; using CustomCADs.Shared.Application.UseCases.Shipments.Commands; @@ -12,7 +13,7 @@ public class ActiveCartDeliveryRequestedHandler( IPurchasedCartReads reads, IUnitOfWork uow, IRequestSender sender -) +) : IEventHandler { public async Task HandleAsync(ActiveCartDeliveryRequestedApplicationEvent @event) { diff --git a/src/Modules/Carts/Application/PurchasedCarts/Events/Application/PaymentCompleted/CartPaymentCompletedHandler.cs b/src/Modules/Carts/Application/PurchasedCarts/Events/Application/PaymentCompleted/CartPaymentCompletedHandler.cs index 43fbe955c..a85fcb8ed 100644 --- a/src/Modules/Carts/Application/PurchasedCarts/Events/Application/PaymentCompleted/CartPaymentCompletedHandler.cs +++ b/src/Modules/Carts/Application/PurchasedCarts/Events/Application/PaymentCompleted/CartPaymentCompletedHandler.cs @@ -1,6 +1,7 @@ using CustomCADs.Modules.Carts.Domain.Repositories; using CustomCADs.Modules.Carts.Domain.Repositories.Reads; using CustomCADs.Shared.Application.Abstractions.Email; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Abstractions.Requests.Sender; using CustomCADs.Shared.Application.Events.Carts; using CustomCADs.Shared.Application.UseCases.Accounts.Queries; @@ -15,7 +16,7 @@ public class CartPaymentCompletedHandler( IUnitOfWork uow, IRequestSender sender, IEmailService email -) +) : IEventHandler { public async Task HandleAsync(CartPaymentCompletedApplicationEvent @event) { diff --git a/src/Modules/Catalog/Application/Products/Events/Application/ProductCreated/ProductCreatedHandler.cs b/src/Modules/Catalog/Application/Products/Events/Application/ProductCreated/ProductCreatedHandler.cs index 57b7dc8c0..f3ccfcaeb 100644 --- a/src/Modules/Catalog/Application/Products/Events/Application/ProductCreated/ProductCreatedHandler.cs +++ b/src/Modules/Catalog/Application/Products/Events/Application/ProductCreated/ProductCreatedHandler.cs @@ -1,9 +1,11 @@ using CustomCADs.Modules.Catalog.Domain.Repositories; using CustomCADs.Modules.Catalog.Domain.Repositories.Writes; +using CustomCADs.Shared.Application.Abstractions.Events; namespace CustomCADs.Modules.Catalog.Application.Products.Events.Application.ProductCreated; public class ProductCreatedHandler(IProductWrites writes, IUnitOfWork uow) + : IEventHandler { public async Task HandleAsync(ProductCreatedApplicationEvent @event) { diff --git a/src/Modules/Catalog/Application/Products/Events/Application/ProductPurchased/ProductsPurchasedHandler.cs b/src/Modules/Catalog/Application/Products/Events/Application/ProductPurchased/ProductsPurchasedHandler.cs index a386a7446..fd21f4d43 100644 --- a/src/Modules/Catalog/Application/Products/Events/Application/ProductPurchased/ProductsPurchasedHandler.cs +++ b/src/Modules/Catalog/Application/Products/Events/Application/ProductPurchased/ProductsPurchasedHandler.cs @@ -1,12 +1,14 @@ using CustomCADs.Modules.Catalog.Domain.Repositories; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Events.Catalog; namespace CustomCADs.Modules.Catalog.Application.Products.Events.Application.ProductPurchased; public sealed class ProductsPurchasedHandler(IUnitOfWork uow) + : IEventHandler { - public async Task HandleAsync(ProductsPurchasedApplicationEvent req, CancellationToken ct) + public async Task HandleAsync(ProductsPurchasedApplicationEvent req) { - await uow.AddProductsPurchasesAsync(req.Ids, count: 1, ct: ct).ConfigureAwait(false); + await uow.AddProductsPurchasesAsync(req.Ids, count: 1).ConfigureAwait(false); } } diff --git a/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs b/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs index 8d98c0fd1..9788ef2fa 100644 --- a/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs +++ b/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs @@ -9,6 +9,7 @@ namespace CustomCADs.Modules.Catalog.Application.Products.Events.Application.ProductViewed; public class ProductViewedHandler(IProductReads reads, IUnitOfWork uow, IRequestSender sender, IEventRaiser raiser) + : IEventHandler { public async Task HandleAsync(ProductViewedApplicationEvent @event) { diff --git a/src/Modules/Customs/Application/Customs/Events/Application/DeliveryRequested/CustomDeliveryRequestedHandler.cs b/src/Modules/Customs/Application/Customs/Events/Application/DeliveryRequested/CustomDeliveryRequestedHandler.cs index b702133c0..15983ac04 100644 --- a/src/Modules/Customs/Application/Customs/Events/Application/DeliveryRequested/CustomDeliveryRequestedHandler.cs +++ b/src/Modules/Customs/Application/Customs/Events/Application/DeliveryRequested/CustomDeliveryRequestedHandler.cs @@ -1,5 +1,6 @@ using CustomCADs.Modules.Customs.Domain.Repositories; using CustomCADs.Modules.Customs.Domain.Repositories.Reads; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Abstractions.Requests.Sender; using CustomCADs.Shared.Application.UseCases.Accounts.Queries; using CustomCADs.Shared.Application.UseCases.Shipments.Commands; @@ -11,7 +12,7 @@ public class CustomDeliveryRequestedHandler( ICustomReads reads, IUnitOfWork uow, IRequestSender sender -) +) : IEventHandler { public async Task HandleAsync(CustomDeliveryRequestedApplicationEvent @event) { diff --git a/src/Modules/Customs/Application/Customs/Events/Application/PaymentCompleted/CustomPaymentCompletedHandler.cs b/src/Modules/Customs/Application/Customs/Events/Application/PaymentCompleted/CustomPaymentCompletedHandler.cs index 91efe7ce2..6324cd25e 100644 --- a/src/Modules/Customs/Application/Customs/Events/Application/PaymentCompleted/CustomPaymentCompletedHandler.cs +++ b/src/Modules/Customs/Application/Customs/Events/Application/PaymentCompleted/CustomPaymentCompletedHandler.cs @@ -1,6 +1,7 @@ using CustomCADs.Modules.Customs.Domain.Repositories; using CustomCADs.Modules.Customs.Domain.Repositories.Reads; using CustomCADs.Shared.Application.Abstractions.Email; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Abstractions.Requests.Sender; using CustomCADs.Shared.Application.Events.Customs; using CustomCADs.Shared.Application.UseCases.Accounts.Queries; @@ -15,7 +16,7 @@ public class CustomPaymentCompletedHandler( IUnitOfWork uow, IRequestSender sender, IEmailService email -) +) : IEventHandler { public async Task HandleAsync(CustomPaymentCompletedApplicationEvent @event) { diff --git a/src/Modules/Files/Application/Cads/Events/Application/ProductDeletedHandler.cs b/src/Modules/Files/Application/Cads/Events/Application/ProductDeletedHandler.cs index b32e3f412..352baf900 100644 --- a/src/Modules/Files/Application/Cads/Events/Application/ProductDeletedHandler.cs +++ b/src/Modules/Files/Application/Cads/Events/Application/ProductDeletedHandler.cs @@ -1,11 +1,13 @@ using CustomCADs.Modules.Files.Application.Cads.Storage; using CustomCADs.Modules.Files.Domain.Repositories; using CustomCADs.Modules.Files.Domain.Repositories.Reads; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Events.Catalog; namespace CustomCADs.Modules.Files.Application.Cads.Events.Application; public class ProductDeletedHandler(ICadReads reads, IWrites writes, IUnitOfWork uow, ICadStorageService storage, BaseCachingService cache) + : IEventHandler { public async Task HandleAsync(ProductDeletedApplicationEvent @event) { diff --git a/src/Modules/Files/Application/Images/Events/Application/ProductDeletedHandler.cs b/src/Modules/Files/Application/Images/Events/Application/ProductDeletedHandler.cs index 27176b598..9f756796a 100644 --- a/src/Modules/Files/Application/Images/Events/Application/ProductDeletedHandler.cs +++ b/src/Modules/Files/Application/Images/Events/Application/ProductDeletedHandler.cs @@ -1,11 +1,13 @@ using CustomCADs.Modules.Files.Application.Images.Storage; using CustomCADs.Modules.Files.Domain.Repositories; using CustomCADs.Modules.Files.Domain.Repositories.Reads; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Events.Catalog; namespace CustomCADs.Modules.Files.Application.Images.Events.Application; public class ProductDeletedHandler(IImageReads reads, IWrites writes, IUnitOfWork uow, IImageStorageService storage, BaseCachingService cache) + : IEventHandler { public async Task HandleAsync(ProductDeletedApplicationEvent @event) { diff --git a/src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedEventHandler.cs b/src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedEventHandler.cs index 5ae65946e..8bd11eb44 100644 --- a/src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedEventHandler.cs +++ b/src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedEventHandler.cs @@ -1,8 +1,10 @@ using CustomCADs.Shared.Application.Abstractions.Email; +using CustomCADs.Shared.Application.Abstractions.Events; namespace CustomCADs.Modules.Identity.Application.Users.Events.Application.Emails.EmailVerification; public class EmailVerificationRequestedEventHandler(IEmailService email) + : IEventHandler { public async Task HandleAsync(EmailVerificationRequestedApplicationEvent @event) { diff --git a/src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedEventHandler.cs b/src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedEventHandler.cs index 84c116549..36a9445f7 100644 --- a/src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedEventHandler.cs +++ b/src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedEventHandler.cs @@ -1,8 +1,10 @@ using CustomCADs.Shared.Application.Abstractions.Email; +using CustomCADs.Shared.Application.Abstractions.Events; namespace CustomCADs.Modules.Identity.Application.Users.Events.Application.Emails.PasswordReset; public class PasswordResetRequestedEventHandler(IEmailService email) + : IEventHandler { public async Task HandleAsync(PasswordResetRequestedApplicationEvent @event) { diff --git a/src/Modules/Identity/Application/Users/Events/Application/Roles/RoleCreatedHandler.cs b/src/Modules/Identity/Application/Users/Events/Application/Roles/RoleCreatedHandler.cs index cb0c1d535..093564632 100644 --- a/src/Modules/Identity/Application/Users/Events/Application/Roles/RoleCreatedHandler.cs +++ b/src/Modules/Identity/Application/Users/Events/Application/Roles/RoleCreatedHandler.cs @@ -1,8 +1,9 @@ -using CustomCADs.Shared.Application.Events.Account.Roles; +using CustomCADs.Shared.Application.Abstractions.Events; +using CustomCADs.Shared.Application.Events.Account.Roles; namespace CustomCADs.Modules.Identity.Application.Users.Events.Application.Roles; -public class RoleCreatedHandler(IRoleService service) +public class RoleCreatedHandler(IRoleService service) : IEventHandler { public async Task HandleAsync(RoleCreatedApplicationEvent @event) => await service.CreateAsync( diff --git a/src/Modules/Identity/Application/Users/Events/Application/Roles/RoleDeletedHandler.cs b/src/Modules/Identity/Application/Users/Events/Application/Roles/RoleDeletedHandler.cs index 1378ec79a..e2a612fe7 100644 --- a/src/Modules/Identity/Application/Users/Events/Application/Roles/RoleDeletedHandler.cs +++ b/src/Modules/Identity/Application/Users/Events/Application/Roles/RoleDeletedHandler.cs @@ -1,8 +1,9 @@ -using CustomCADs.Shared.Application.Events.Account.Roles; +using CustomCADs.Shared.Application.Abstractions.Events; +using CustomCADs.Shared.Application.Events.Account.Roles; namespace CustomCADs.Modules.Identity.Application.Users.Events.Application.Roles; -public class RoleDeletedHandler(IRoleService service) +public class RoleDeletedHandler(IRoleService service) : IEventHandler { public async Task HandleAsync(RoleDeletedApplicationEvent @event) => await service.DeleteAsync( diff --git a/src/Modules/Identity/Application/Users/Events/Application/Users/AccountCreatedHandler.cs b/src/Modules/Identity/Application/Users/Events/Application/Users/AccountCreatedHandler.cs index efe300e16..15f2031cf 100644 --- a/src/Modules/Identity/Application/Users/Events/Application/Users/AccountCreatedHandler.cs +++ b/src/Modules/Identity/Application/Users/Events/Application/Users/AccountCreatedHandler.cs @@ -1,8 +1,10 @@ -using CustomCADs.Shared.Application.Events.Account.Accounts; +using CustomCADs.Shared.Application.Abstractions.Events; +using CustomCADs.Shared.Application.Events.Account.Accounts; namespace CustomCADs.Modules.Identity.Application.Users.Events.Application.Users; public class AccountCreatedHandler(IUserService service) + : IEventHandler { public async Task HandleAsync(AccountCreatedApplicationEvent @event) { diff --git a/src/Modules/Identity/Application/Users/Events/Application/Users/AccountDeletedHandler.cs b/src/Modules/Identity/Application/Users/Events/Application/Users/AccountDeletedHandler.cs index 7855bfc6e..546df5180 100644 --- a/src/Modules/Identity/Application/Users/Events/Application/Users/AccountDeletedHandler.cs +++ b/src/Modules/Identity/Application/Users/Events/Application/Users/AccountDeletedHandler.cs @@ -1,8 +1,10 @@ -using CustomCADs.Shared.Application.Events.Account.Accounts; +using CustomCADs.Shared.Application.Abstractions.Events; +using CustomCADs.Shared.Application.Events.Account.Accounts; namespace CustomCADs.Modules.Identity.Application.Users.Events.Application.Users; public class AccountDeletedHandler(IUserService service) + : IEventHandler { public async Task HandleAsync(AccountDeletedApplicationEvent @event) { diff --git a/src/Modules/Notifications/Application/Notifications/Events/NotificationRequestedHandler.cs b/src/Modules/Notifications/Application/Notifications/Events/NotificationRequestedHandler.cs index 5f33541bb..02d53672f 100644 --- a/src/Modules/Notifications/Application/Notifications/Events/NotificationRequestedHandler.cs +++ b/src/Modules/Notifications/Application/Notifications/Events/NotificationRequestedHandler.cs @@ -1,5 +1,6 @@ using CustomCADs.Modules.Notifications.Application.Contracts; using CustomCADs.Modules.Notifications.Domain.Repositories; +using CustomCADs.Shared.Application.Abstractions.Events; using CustomCADs.Shared.Application.Abstractions.Requests.Sender; using CustomCADs.Shared.Application.Events.Notifications; using CustomCADs.Shared.Application.UseCases.Accounts.Queries; @@ -8,6 +9,7 @@ namespace CustomCADs.Modules.Notifications.Application.Notifications.Events; public class NotificationRequestedHandler(IWrites writes, IUnitOfWork uow, IRequestSender sender, INotificationsRealTimeNotifier notifier) + : IEventHandler { public async Task HandleAsync(NotificationRequestedEvent @event) { diff --git a/src/Shared/Application/Abstractions/Events/IEventHandler.cs b/src/Shared/Application/Abstractions/Events/IEventHandler.cs new file mode 100644 index 000000000..cdcd8d7c1 --- /dev/null +++ b/src/Shared/Application/Abstractions/Events/IEventHandler.cs @@ -0,0 +1,6 @@ +namespace CustomCADs.Shared.Application.Abstractions.Events; + +public interface IEventHandler where TEvent : BaseEvent +{ + Task HandleAsync(TEvent @event); +} From 21f40c824e661aa72f2ad2a6a2a40e7eaa64c582 Mon Sep 17 00:00:00 2001 From: Ivan Angelov Date: Thu, 18 Jun 2026 02:34:08 +0300 Subject: [PATCH 3/3] Improved Event Handlers codestyle --- .../Events/Application/UserDeletedHandler.cs | 7 +- .../CartPaymentCompletedHandler.cs | 26 ++++-- .../ProductViewed/ProductViewedHandler.cs | 11 ++- .../CustomPaymentCompletedHandler.cs | 27 ++++-- .../Application/ProductDeletedHandler.cs | 9 +- .../Application/ProductDeletedHandler.cs | 9 +- ...s => EmailVerificationRequestedHandler.cs} | 2 +- ...er.cs => PasswordResetRequestedHandler.cs} | 2 +- .../Events/NotificationRequestedHandler.cs | 91 +++++++++++-------- .../Events/Application/Purchased/Tests.cs | 2 +- .../Emails/EmailVerification/Tests.cs | 2 +- .../Application/Emails/PasswordReset/Tests.cs | 2 +- .../Events/NotificationRequested/Tests.cs | 13 +-- 13 files changed, 124 insertions(+), 79 deletions(-) rename src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/{EmailVerificationRequestedEventHandler.cs => EmailVerificationRequestedHandler.cs} (86%) rename src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/{PasswordResetRequestedEventHandler.cs => PasswordResetRequestedHandler.cs} (86%) diff --git a/src/Modules/Accounts/Application/Accounts/Events/Application/UserDeletedHandler.cs b/src/Modules/Accounts/Application/Accounts/Events/Application/UserDeletedHandler.cs index 44e0a6df3..4529a6719 100644 --- a/src/Modules/Accounts/Application/Accounts/Events/Application/UserDeletedHandler.cs +++ b/src/Modules/Accounts/Application/Accounts/Events/Application/UserDeletedHandler.cs @@ -6,8 +6,11 @@ namespace CustomCADs.Modules.Accounts.Application.Accounts.Events.Application; -public class UserDeletedHandler(IAccountReads reads, IAccountWrites writes, IUnitOfWork uow) - : IEventHandler +public class UserDeletedHandler( + IAccountReads reads, + IAccountWrites writes, + IUnitOfWork uow +) : IEventHandler { public async Task HandleAsync(UserDeletedApplicationEvent @event) { diff --git a/src/Modules/Carts/Application/PurchasedCarts/Events/Application/PaymentCompleted/CartPaymentCompletedHandler.cs b/src/Modules/Carts/Application/PurchasedCarts/Events/Application/PaymentCompleted/CartPaymentCompletedHandler.cs index a85fcb8ed..f7bdeeb48 100644 --- a/src/Modules/Carts/Application/PurchasedCarts/Events/Application/PaymentCompleted/CartPaymentCompletedHandler.cs +++ b/src/Modules/Carts/Application/PurchasedCarts/Events/Application/PaymentCompleted/CartPaymentCompletedHandler.cs @@ -28,24 +28,30 @@ public async Task HandleAsync(CartPaymentCompletedApplicationEvent @event) await uow.BulkDeleteItemsByBuyerIdAsync(@event.BuyerId).ConfigureAwait(false); - string to = await sender.SendQueryAsync( + string recipient = await sender.SendQueryAsync( query: new GetUserEmailByIdQuery(@event.BuyerId) ).ConfigureAwait(false); - string url = await sender.SendQueryAsync( + string clientUrl = await sender.SendQueryAsync( query: new GetClientUrlQuery() ).ConfigureAwait(false); - await email.SendRewardGrantedEmailAsync(to, $"{url}/carts/{cart.Id}").ConfigureAwait(false); + await email.SendRewardGrantedEmailAsync(recipient, $"{clientUrl}/carts/{cart.Id}").ConfigureAwait(false); - if (cart is { HasDelivery: true, ShipmentId: not null }) + if (cart.HasDelivery) { - ShipmentId shipmentId = cart.ShipmentId.Value; - - await sender.SendCommandAsync( - new ActivateShipmentCommand(shipmentId) - ).ConfigureAwait(false); - await email.SendRewardGrantedEmailAsync(to, $"{url}/shipments/{shipmentId}").ConfigureAwait(false); + await ActivateShipmentAsync(cart.ShipmentId).ConfigureAwait(false); + await email.SendRewardGrantedEmailAsync(recipient, $"{clientUrl}/shipments/{cart.ShipmentId}").ConfigureAwait(false); } } + + private async Task ActivateShipmentAsync(ShipmentId? shipmentId) + { + if (shipmentId is null) + throw new CustomException("Shipment Activation requested, but missing ShipmentId"); + + await sender.SendCommandAsync( + command: new ActivateShipmentCommand(shipmentId.Value) + ).ConfigureAwait(false); + } } diff --git a/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs b/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs index 9788ef2fa..e21abaf91 100644 --- a/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs +++ b/src/Modules/Catalog/Application/Products/Events/Application/ProductViewed/ProductViewedHandler.cs @@ -8,8 +8,12 @@ namespace CustomCADs.Modules.Catalog.Application.Products.Events.Application.ProductViewed; -public class ProductViewedHandler(IProductReads reads, IUnitOfWork uow, IRequestSender sender, IEventRaiser raiser) - : IEventHandler +public class ProductViewedHandler( + IProductReads reads, + IUnitOfWork uow, + IRequestSender sender, + IEventRaiser raiser +) : IEventHandler { public async Task HandleAsync(ProductViewedApplicationEvent @event) { @@ -18,14 +22,13 @@ public async Task HandleAsync(ProductViewedApplicationEvent @event) ).ConfigureAwait(false); if (userAlreadyViewed) return; - var account = await sender.SendQueryAsync( + AccountInfoDto account = await sender.SendQueryAsync( query: new GetAccountInfoByUsernameQuery( Username: await sender.SendQueryAsync( query: new GetUsernameByIdQuery(@event.AccountId) ).ConfigureAwait(false) ) ).ConfigureAwait(false); - if (!account.TrackViewedProducts) return; Product product = await reads.SingleByIdAsync(@event.Id).ConfigureAwait(false) diff --git a/src/Modules/Customs/Application/Customs/Events/Application/PaymentCompleted/CustomPaymentCompletedHandler.cs b/src/Modules/Customs/Application/Customs/Events/Application/PaymentCompleted/CustomPaymentCompletedHandler.cs index 6324cd25e..00bd50a78 100644 --- a/src/Modules/Customs/Application/Customs/Events/Application/PaymentCompleted/CustomPaymentCompletedHandler.cs +++ b/src/Modules/Customs/Application/Customs/Events/Application/PaymentCompleted/CustomPaymentCompletedHandler.cs @@ -26,24 +26,31 @@ public async Task HandleAsync(CustomPaymentCompletedApplicationEvent @event) custom.FinishPayment(success: true); await uow.SaveChangesAsync().ConfigureAwait(false); - string to = await sender.SendQueryAsync( + string recipient = await sender.SendQueryAsync( query: new GetUserEmailByIdQuery(@event.BuyerId) ).ConfigureAwait(false); - string url = await sender.SendQueryAsync( + string clientUrl = await sender.SendQueryAsync( query: new GetClientUrlQuery() ).ConfigureAwait(false); - await email.SendRewardGrantedEmailAsync(to, $"{url}/customs/{custom.Id}").ConfigureAwait(false); + await email.SendRewardGrantedEmailAsync(recipient, $"{clientUrl}/customs/{custom.Id}").ConfigureAwait(false); - if (custom is { ForDelivery: true, CompletedCustom.ShipmentId: not null }) + if (custom.ForDelivery) { - ShipmentId shipmentId = custom.CompletedCustom.ShipmentId.Value; - - await sender.SendCommandAsync( - command: new ActivateShipmentCommand(shipmentId) - ).ConfigureAwait(false); - await email.SendRewardGrantedEmailAsync(to, $"{url}/shipments/{shipmentId}").ConfigureAwait(false); + ShipmentId? shipmentId = custom.CompletedCustom?.ShipmentId; + await ActivateShipmentAsync(shipmentId).ConfigureAwait(false); + await email.SendRewardGrantedEmailAsync(recipient, $"{clientUrl}/shipments/{shipmentId}").ConfigureAwait(false); } } + + private async Task ActivateShipmentAsync(ShipmentId? shipmentId) + { + if (shipmentId is null) + throw new CustomException("Shipment Activation requested, but missing ShipmentId"); + + await sender.SendCommandAsync( + command: new ActivateShipmentCommand(shipmentId.Value) + ).ConfigureAwait(false); + } } diff --git a/src/Modules/Files/Application/Cads/Events/Application/ProductDeletedHandler.cs b/src/Modules/Files/Application/Cads/Events/Application/ProductDeletedHandler.cs index 352baf900..1361b686e 100644 --- a/src/Modules/Files/Application/Cads/Events/Application/ProductDeletedHandler.cs +++ b/src/Modules/Files/Application/Cads/Events/Application/ProductDeletedHandler.cs @@ -6,8 +6,13 @@ namespace CustomCADs.Modules.Files.Application.Cads.Events.Application; -public class ProductDeletedHandler(ICadReads reads, IWrites writes, IUnitOfWork uow, ICadStorageService storage, BaseCachingService cache) - : IEventHandler +public class ProductDeletedHandler( + ICadReads reads, + IWrites writes, + IUnitOfWork uow, + ICadStorageService storage, + BaseCachingService cache +) : IEventHandler { public async Task HandleAsync(ProductDeletedApplicationEvent @event) { diff --git a/src/Modules/Files/Application/Images/Events/Application/ProductDeletedHandler.cs b/src/Modules/Files/Application/Images/Events/Application/ProductDeletedHandler.cs index 9f756796a..a2c99e80b 100644 --- a/src/Modules/Files/Application/Images/Events/Application/ProductDeletedHandler.cs +++ b/src/Modules/Files/Application/Images/Events/Application/ProductDeletedHandler.cs @@ -6,8 +6,13 @@ namespace CustomCADs.Modules.Files.Application.Images.Events.Application; -public class ProductDeletedHandler(IImageReads reads, IWrites writes, IUnitOfWork uow, IImageStorageService storage, BaseCachingService cache) - : IEventHandler +public class ProductDeletedHandler( + IImageReads reads, + IWrites writes, + IUnitOfWork uow, + IImageStorageService storage, + BaseCachingService cache +) : IEventHandler { public async Task HandleAsync(ProductDeletedApplicationEvent @event) { diff --git a/src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedEventHandler.cs b/src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedHandler.cs similarity index 86% rename from src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedEventHandler.cs rename to src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedHandler.cs index 8bd11eb44..0b1a4a973 100644 --- a/src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedEventHandler.cs +++ b/src/Modules/Identity/Application/Users/Events/Application/Emails/EmailVerification/EmailVerificationRequestedHandler.cs @@ -3,7 +3,7 @@ namespace CustomCADs.Modules.Identity.Application.Users.Events.Application.Emails.EmailVerification; -public class EmailVerificationRequestedEventHandler(IEmailService email) +public class EmailVerificationRequestedHandler(IEmailService email) : IEventHandler { public async Task HandleAsync(EmailVerificationRequestedApplicationEvent @event) diff --git a/src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedEventHandler.cs b/src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedHandler.cs similarity index 86% rename from src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedEventHandler.cs rename to src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedHandler.cs index 36a9445f7..f640dba8e 100644 --- a/src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedEventHandler.cs +++ b/src/Modules/Identity/Application/Users/Events/Application/Emails/PasswordReset/PasswordResetRequestedHandler.cs @@ -3,7 +3,7 @@ namespace CustomCADs.Modules.Identity.Application.Users.Events.Application.Emails.PasswordReset; -public class PasswordResetRequestedEventHandler(IEmailService email) +public class PasswordResetRequestedHandler(IEmailService email) : IEventHandler { public async Task HandleAsync(PasswordResetRequestedApplicationEvent @event) diff --git a/src/Modules/Notifications/Application/Notifications/Events/NotificationRequestedHandler.cs b/src/Modules/Notifications/Application/Notifications/Events/NotificationRequestedHandler.cs index 02d53672f..04b374e24 100644 --- a/src/Modules/Notifications/Application/Notifications/Events/NotificationRequestedHandler.cs +++ b/src/Modules/Notifications/Application/Notifications/Events/NotificationRequestedHandler.cs @@ -8,51 +8,66 @@ namespace CustomCADs.Modules.Notifications.Application.Notifications.Events; -public class NotificationRequestedHandler(IWrites writes, IUnitOfWork uow, IRequestSender sender, INotificationsRealTimeNotifier notifier) - : IEventHandler +public class NotificationRequestedHandler( + IWrites writes, + IUnitOfWork uow, IRequestSender sender, + INotificationsRealTimeNotifier notifier +) : IEventHandler { public async Task HandleAsync(NotificationRequestedEvent @event) { - List notifications = []; - if (@event.ReceiverIds is [AccountId receiverId]) + if (@event.ReceiverIds.Length == 0) return; + string author = await sender.SendQueryAsync(new GetUsernameByIdQuery(@event.AuthorId)).ConfigureAwait(false); + + List notifications = @event.ReceiverIds.Length == 1 + ? [await CreateNotificationAsync(@event).ConfigureAwait(false)] + : await BulkCreateNotificationsAsync(@event).ConfigureAwait(false); + + foreach (Notification notification in notifications) { - Notification notification = await writes.AddAsync( - entity: Notification.Create( - type: @event.Type.ToString(), - content: new(@event.Description, @event.Link), - authorId: @event.AuthorId, - receiverId: receiverId - ) + await notifier.NotifyUserAsync( + id: notification.ReceiverId, + message: "ReceiveNew", + payload: new + { + id = notification.Id.Value, + status = notification.Status, + createdAt = notification.CreatedAt, + type = @event.Type, + description = @event.Description, + link = @event.Link, + author = author, + } ).ConfigureAwait(false); - await uow.SaveChangesAsync().ConfigureAwait(false); - - notifications.Add(notification); } - else notifications = [.. - await uow.InsertNotificationsAsync( - notifications: Notification.CreateBulk( - type: @event.Type.ToString(), - content: new(@event.Description, @event.Link), - authorId: @event.AuthorId, - receiverIds: [.. @event.ReceiverIds.Distinct()] - ) - ).ConfigureAwait(false) - ]; + } - string author = await sender.SendQueryAsync(new GetUsernameByIdQuery(@event.AuthorId)).ConfigureAwait(false); - await notifier.NotifyUsersAsync( - ids: @event.ReceiverIds, - message: "ReceiveNew", - payload: notifications.Select(x => new - { - id = x.Id.Value, - type = x.Type, - description = x.Content.Description, - link = x.Content.Link, - status = x.Status, - createdAt = x.CreatedAt, - author = author, - }).FirstOrDefault() + private async Task CreateNotificationAsync(NotificationRequestedEvent @event) + { + Notification notification = await writes.AddAsync( + entity: Notification.Create( + type: @event.Type.ToString(), + content: new(@event.Description, @event.Link), + authorId: @event.AuthorId, + receiverId: @event.ReceiverIds.First() + ) ).ConfigureAwait(false); + await uow.SaveChangesAsync().ConfigureAwait(false); + + return notification; + } + + private async Task> BulkCreateNotificationsAsync(NotificationRequestedEvent @event) + { + ICollection notifications = await uow.InsertNotificationsAsync( + notifications: Notification.CreateBulk( + type: @event.Type.ToString(), + content: new(@event.Description, @event.Link), + authorId: @event.AuthorId, + receiverIds: [.. @event.ReceiverIds.Distinct()] + ) + ).ConfigureAwait(false); + + return [.. notifications]; } } diff --git a/tests/UnitTests/Catalog/Application/Products/Events/Application/Purchased/Tests.cs b/tests/UnitTests/Catalog/Application/Products/Events/Application/Purchased/Tests.cs index 9130b1886..e678dcedf 100644 --- a/tests/UnitTests/Catalog/Application/Products/Events/Application/Purchased/Tests.cs +++ b/tests/UnitTests/Catalog/Application/Products/Events/Application/Purchased/Tests.cs @@ -24,7 +24,7 @@ public async Task Handle_ShouldPersistToDatabase() // Arrange // Act - await handler.HandleAsync(request, ct); + await handler.HandleAsync(request); // Assert uow.Verify( diff --git a/tests/UnitTests/Identity/Application/Users/Events/Application/Emails/EmailVerification/Tests.cs b/tests/UnitTests/Identity/Application/Users/Events/Application/Emails/EmailVerification/Tests.cs index 7ec13dbec..462bbba4e 100644 --- a/tests/UnitTests/Identity/Application/Users/Events/Application/Emails/EmailVerification/Tests.cs +++ b/tests/UnitTests/Identity/Application/Users/Events/Application/Emails/EmailVerification/Tests.cs @@ -5,7 +5,7 @@ namespace CustomCADs.UnitTests.Identity.Application.Users.Events.Application.Ema public class Tests : Data.Users.BaseUnitTests { - private readonly EmailVerificationRequestedEventHandler handler; + private readonly EmailVerificationRequestedHandler handler; private readonly EmailVerificationRequestedApplicationEvent request = new(Email, Endpoint); private readonly Mock email = new(); diff --git a/tests/UnitTests/Identity/Application/Users/Events/Application/Emails/PasswordReset/Tests.cs b/tests/UnitTests/Identity/Application/Users/Events/Application/Emails/PasswordReset/Tests.cs index 5a0a3b981..d6ea73f70 100644 --- a/tests/UnitTests/Identity/Application/Users/Events/Application/Emails/PasswordReset/Tests.cs +++ b/tests/UnitTests/Identity/Application/Users/Events/Application/Emails/PasswordReset/Tests.cs @@ -5,7 +5,7 @@ namespace CustomCADs.UnitTests.Identity.Application.Users.Events.Application.Ema public class Tests : Data.Users.BaseUnitTests { - private readonly PasswordResetRequestedEventHandler handler; + private readonly PasswordResetRequestedHandler handler; private readonly PasswordResetRequestedApplicationEvent request = new(Email, Endpoint); private readonly Mock email = new(); diff --git a/tests/UnitTests/Notifications/Application/Notifications/Events/NotificationRequested/Tests.cs b/tests/UnitTests/Notifications/Application/Notifications/Events/NotificationRequested/Tests.cs index cbc291c6d..2d2fc83d1 100644 --- a/tests/UnitTests/Notifications/Application/Notifications/Events/NotificationRequested/Tests.cs +++ b/tests/UnitTests/Notifications/Application/Notifications/Events/NotificationRequested/Tests.cs @@ -6,6 +6,7 @@ using CustomCADs.Shared.Application.Dtos.Notifications; using CustomCADs.Shared.Application.Events.Notifications; using CustomCADs.Shared.Application.UseCases.Accounts.Queries; +using CustomCADs.Shared.Domain.TypedIds.Accounts; namespace CustomCADs.UnitTests.Notifications.Application.Notifications.Events.NotificationRequested; @@ -19,7 +20,7 @@ public class Tests : Data.Notifications.BaseUnitTests Description: MinValidDescription, Link: ValidLink, AuthorId: ValidAuthorId, - ReceiverIds: [] + ReceiverIds: [ValidReceiverId, ValidAuthorId, AccountId.New()] ); private readonly Mock> writes = new(); @@ -31,11 +32,11 @@ public Tests() { handler = new(writes.Object, uow.Object, sender.Object, notifier.Object); - Notification[] notifications = [CreateNotification(), CreateNotification()]; + Notification[] notifications = [.. request.ReceiverIds.Select(x => CreateNotification(receiverId: x))]; writes.Setup(x => x.AddAsync( It.Is(x => x.ReceiverId == ValidReceiverId), ct - )).ReturnsAsync(notifications.First()); + )).ReturnsAsync(notifications.First(x => x.ReceiverId == ValidReceiverId)); uow.Setup(x => x.InsertNotificationsAsync( It.IsAny(), ct @@ -120,13 +121,13 @@ public async Task Handle_ShouldNotifySubscribers() // Assert notifier.Verify( - x => x.NotifyUsersAsync( - request.ReceiverIds, + x => x.NotifyUserAsync( + It.IsAny(), It.IsAny(), It.IsAny(), ct ), - Times.Once() + Times.Exactly(request.ReceiverIds.Length) ); } }