Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Add Customer login notification

support96d2fd87aaba
diff --git a/src/Business/Grand.Business.Customers/Events/Handlers/CustomerLoggedInEventHandler.cs b/src/Business/Grand.Business.Customers/Events/Handlers/CustomerLoggedInEventHandler.cs
new file mode 100644
index 000000000..e390476df
--- /dev/null
+++ b/src/Business/Grand.Business.Customers/Events/Handlers/CustomerLoggedInEventHandler.cs
@@ -0,0 +1,27 @@
+using Grand.Business.Core.Events.Customers;
+using Grand.Business.Core.Interfaces.Customers;
+using MediatR;
+
+namespace Grand.Business.Customers.Events.Handlers;
+
+public class CustomerLoggedInEventHandler: INotificationHandler<CustomerLoggedInEvent>
+{
+ private readonly ICustomerService _customerService;
+
+ public CustomerLoggedInEventHandler(ICustomerService customerService)
+ {
+ _customerService = customerService;
+ }
+
+ public async Task Handle(CustomerLoggedInEvent notification, CancellationToken cancellationToken)
+ {
+ if (notification.Customer != null)
+ {
+ //save last login date
+ notification.Customer.FailedLoginAttempts = 0;
+ notification.Customer.CannotLoginUntilDateUtc = null;
+ notification.Customer.LastLoginDateUtc = DateTime.UtcNow;
+ await _customerService.UpdateCustomerLastLoginDate(notification.Customer);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Business/Grand.Business.Customers/Services/CustomerManagerService.cs b/src/Business/Grand.Business.Customers/Services/CustomerManagerService.cs
index 3a70903a7..05f1e4c1a 100644
--- a/src/Business/Grand.Business.Customers/Services/CustomerManagerService.cs
+++ b/src/Business/Grand.Business.Customers/Services/CustomerManagerService.cs
@@ -128,12 +128,7 @@ namespace Grand.Business.Customers.Services
//2fa required
if (customer.GetUserFieldFromEntity<bool>(SystemCustomerFieldNames.TwoFactorEnabled) && _customerSettings.TwoFactorAuthenticationEnabled)
return CustomerLoginResults.RequiresTwoFactor;
-
- //save last login date
- customer.FailedLoginAttempts = 0;
- customer.CannotLoginUntilDateUtc = null;
- customer.LastLoginDateUtc = DateTime.UtcNow;
- await _customerService.UpdateCustomerLastLoginDate(customer);
+
return CustomerLoginResults.Successful;
}
diff --git a/src/Tests/Grand.Business.Customers.Tests/Handler/CustomerLoggedInNotificationHandlerTests.cs b/src/Tests/Grand.Business.Customers.Tests/Handler/CustomerLoggedInNotificationHandlerTests.cs
new file mode 100644
index 000000000..d37e9d5c4
--- /dev/null
+++ b/src/Tests/Grand.Business.Customers.Tests/Handler/CustomerLoggedInNotificationHandlerTests.cs
@@ -0,0 +1,45 @@
+using Grand.Business.Core.Interfaces.Common.Directory;
+using Grand.Business.Customers.Commands.Handlers;
+using Grand.Business.Core.Commands.Customers;
+using Grand.Business.Core.Events.Customers;
+using Grand.Business.Core.Interfaces.Customers;
+using Grand.Business.Customers.Events.Handlers;
+using Grand.Domain.Customers;
+using MediatR;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Customers.Tests.Handler
+{
+ [TestClass()]
+ public class CustomerLoggedInNotificationHandlerTests
+ {
+ private Mock<ICustomerService> _cumstomerServiceMock;
+ private CustomerLoggedInEventHandler _handler;
+
+ [TestInitialize()]
+ public void Init()
+ {
+ _cumstomerServiceMock = new Mock<ICustomerService>();
+ _handler = new CustomerLoggedInEventHandler(_cumstomerServiceMock.Object);
+ }
+
+ [TestMethod()]
+ public async Task HandleTest()
+ {
+ //Arrange
+ var customer = new Customer {
+ FailedLoginAttempts = 1,
+ CannotLoginUntilDateUtc = DateTime.Now,
+ };
+ //Act
+ await _handler.Handle(new CustomerLoggedInEvent(customer), CancellationToken.None);
+
+ //Assert
+ Assert.IsTrue(customer.FailedLoginAttempts == 0);
+ Assert.IsTrue(customer.CannotLoginUntilDateUtc == null);
+ _cumstomerServiceMock.Verify(c => c.UpdateCustomerLastLoginDate(customer), Times.Once);
+ }
+
+ }
+}