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.

Refactor InsertGuestCustomer in CustomerService

support1bec3f2b3dbb
diff --git a/src/API/Grand.Api/Controllers/TokenWebController.cs b/src/API/Grand.Api/Controllers/TokenWebController.cs
index 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.cs
index 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 customer
diff --git a/src/Business/Grand.Business.Customers/Services/CustomerService.cs b/src/Business/Grand.Business.Customers/Services/CustomerService.cs
index 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' group
var 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.cs
index 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.Services
var 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);
//Assert
Assert.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.cs
index 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 authenticated
if (customer != null)
{
- //remove cookie
- await _authenticationService.SetCustomerGuid(Guid.Empty);
-
//set if use impersonated session
var impersonatedCustomer = await SetImpersonatedCustomer(customer);
if (impersonatedCustomer != null)
@@ -265,15 +262,39 @@ namespace Grand.Web.Common
return _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 cookie
await _authenticationService.SetCustomerGuid(customer.CustomerGuid);
- //cache the found customer
- return _cachedCustomer = customer;
+ return customer;
}
/// <summary>