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.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Refactor InsertGuestCustomer in CustomerService
support1bec3f2b3dbb
diff --git a/src/API/Grand.Api/Controllers/TokenWebController.cs b/src/API/Grand.Api/Controllers/TokenWebController.csindex c7b7d9e55..6d0ec34d8 100644--- a/src/API/Grand.Api/Controllers/TokenWebController.cs+++ b/src/API/Grand.Api/Controllers/TokenWebController.cs@@ -55,8 +55,16 @@ namespace Grand.Api.Controllers{if (!_apiConfig.Enabled)return BadRequest("API is disabled");-- var customer = await _customerService.InsertGuestCustomer(_storeHelper.StoreHost);++ var customer = new Customer {+ CustomerGuid = Guid.NewGuid(),+ Active = true,+ StoreId = _storeHelper.StoreHost.Id,+ LastActivityDateUtc = DateTime.UtcNow,+ };++ customer = await _customerService.InsertGuestCustomer(customer);+var claims = new Dictionary<string, string> {{ "Guid", customer.CustomerGuid.ToString()}};diff --git a/src/Business/Grand.Business.Core/Interfaces/Customers/ICustomerService.cs b/src/Business/Grand.Business.Core/Interfaces/Customers/ICustomerService.csindex f95a6fb7f..9bddf3bff 100644--- a/src/Business/Grand.Business.Core/Interfaces/Customers/ICustomerService.cs+++ b/src/Business/Grand.Business.Core/Interfaces/Customers/ICustomerService.cs@@ -116,7 +116,7 @@ namespace Grand.Business.Core.Interfaces.Customers/// Insert a guest customer/// </summary>/// <returns>Customer</returns>- Task<Customer> InsertGuestCustomer(Store store);+ Task<Customer> InsertGuestCustomer(Customer customer = null);/// <summary>/// Insert a customerdiff --git a/src/Business/Grand.Business.Customers/Services/CustomerService.cs b/src/Business/Grand.Business.Customers/Services/CustomerService.csindex a4aad1537..d3e5e55e5 100644--- a/src/Business/Grand.Business.Customers/Services/CustomerService.cs+++ b/src/Business/Grand.Business.Customers/Services/CustomerService.cs@@ -244,14 +244,10 @@ namespace Grand.Business.Customers.Services/// Insert a guest customer/// </summary>/// <returns>Customer</returns>- public virtual async Task<Customer> InsertGuestCustomer(Store store)+ public virtual async Task<Customer> InsertGuestCustomer(Customer customer){- var customer = new Customer {- CustomerGuid = Guid.NewGuid(),- Active = true,- StoreId = store.Id,- LastActivityDateUtc = DateTime.UtcNow- };+ ArgumentNullException.ThrowIfNull(customer);+//add to 'Guests' groupvar guestGroup = await _mediator.Send(new GetGroupBySystemNameQuery { SystemName = SystemCustomerGroupNames.Guests });if (guestGroup == null)diff --git a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerServiceTests.cs b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerServiceTests.csindex eb5b94b17..c082843b1 100644--- a/src/Tests/Grand.Business.Customers.Tests/Services/CustomerServiceTests.cs+++ b/src/Tests/Grand.Business.Customers.Tests/Services/CustomerServiceTests.cs@@ -138,9 +138,16 @@ namespace Grand.Business.Customers.Tests.Servicesvar cg = new CustomerGroup();_mediatorMock.Setup(x => x.Send(It.IsAny<GetGroupBySystemNameQuery>(), default)).Returns(Task.FromResult(cg));++ var customer = new Customer {+ CustomerGuid = Guid.NewGuid(),+ Active = true,+ StoreId = "1",+ LastActivityDateUtc = DateTime.UtcNow,+ };//Act- await _customerService.InsertGuestCustomer(new Domain.Stores.Store { Id = "1" });+ await _customerService.InsertGuestCustomer(customer);//AssertAssert.IsTrue(_repository.Table.Any());Assert.IsTrue(_repository.Table.Any(x => x.StoreId == "1"));diff --git a/src/Web/Grand.Web.Common/WorkContext.cs b/src/Web/Grand.Web.Common/WorkContext.csindex 6d1813d52..417ca5fce 100644--- a/src/Web/Grand.Web.Common/WorkContext.cs+++ b/src/Web/Grand.Web.Common/WorkContext.cs@@ -210,9 +210,6 @@ namespace Grand.Web.Common//if customer is authenticatedif (customer != null){- //remove cookie- await _authenticationService.SetCustomerGuid(Guid.Empty);-//set if use impersonated sessionvar impersonatedCustomer = await SetImpersonatedCustomer(customer);if (impersonatedCustomer != null)@@ -265,15 +262,39 @@ namespace Grand.Web.Commonreturn _cachedCustomer = customer;//create guest if not exists- customer = await _customerService.InsertGuestCustomer(CurrentStore);+ customer = await CustomerGuest();++ //cache the found customer+ return _cachedCustomer = customer;+ }++ private async Task<Customer> CustomerGuest()+ {+ var userFields = new List<UserField>();+if (_httpContextAccessor?.HttpContext?.Request.GetTypedHeaders().Referer?.ToString() is { } referer)- {- await _userFieldService.SaveField(customer, SystemCustomerFieldNames.UrlReferrer, referer);- }+ userFields.Add(new UserField() { Key = SystemCustomerFieldNames.UrlReferrer, Value = referer, StoreId = ""});++ if (!string.IsNullOrEmpty(CurrentStore.DefaultCurrencyId))+ userFields.Add(new UserField() { Key = SystemCustomerFieldNames.CurrencyId, Value = CurrentStore.DefaultCurrencyId, StoreId = CurrentStore.Id});++ if (!string.IsNullOrEmpty(CurrentStore.DefaultLanguageId))+ userFields.Add(new UserField() { Key = SystemCustomerFieldNames.LanguageId, Value = CurrentStore.DefaultLanguageId, StoreId = CurrentStore.Id});++ var customer = new Customer {+ CustomerGuid = Guid.NewGuid(),+ Active = true,+ StoreId = CurrentStore.Id,+ LastActivityDateUtc = DateTime.UtcNow,+ LastIpAddress = _httpContextAccessor?.HttpContext?.Connection?.RemoteIpAddress?.ToString(),+ UserFields = userFields+ };++ customer = await _customerService.InsertGuestCustomer(customer);+//set customer cookieawait _authenticationService.SetCustomerGuid(customer.CustomerGuid);- //cache the found customer- return _cachedCustomer = customer;+ return customer;}/// <summary>