diff --git a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Middlewares/LoadUserToEvalContextMiddleware.cs b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Middlewares/LoadUserToEvalContextMiddleware.cs index e1b63ebb..5864729b 100644 --- a/src/VirtoCommerce.ProfileExperienceApiModule.Data/Middlewares/LoadUserToEvalContextMiddleware.cs +++ b/src/VirtoCommerce.ProfileExperienceApiModule.Data/Middlewares/LoadUserToEvalContextMiddleware.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AutoMapper; @@ -7,6 +8,7 @@ using VirtoCommerce.CustomerModule.Core.Model; using VirtoCommerce.CustomerModule.Core.Services; using VirtoCommerce.MarketingModule.Core.Model.Promotions; +using VirtoCommerce.Platform.Core.Common; using VirtoCommerce.Platform.Core.DynamicProperties; using VirtoCommerce.PricingModule.Core.Model; using VirtoCommerce.TaxModule.Core.Model; @@ -17,10 +19,13 @@ public class LoadUserToEvalContextMiddleware : IAsyncMiddleware next) @@ -60,15 +65,32 @@ private async Task InnerSetShopperDataFromMember(EvaluationContextBase evalConte if (member is Contact contact) { evalContextBase.ShopperGender = contact.GetDynamicPropertyValue("gender", string.Empty); + if (contact.BirthDate != null) { var zeroTime = new DateTime(1, 1, 1); var span = DateTime.UtcNow - contact.BirthDate.Value; evalContextBase.ShopperAge = (zeroTime + span).Year - 1; } - evalContextBase.UserGroups = contact.Groups?.ToArray(); + evalContextBase.GeoTimeZone = contact.TimeZone; - //PT-5445: Set other fields from customer + + evalContextBase.UserGroups = contact.Groups?.ToArray(); + + if (!contact.Organizations.IsNullOrEmpty()) + { + var userGroups = new List(); + + if (!evalContextBase.UserGroups.IsNullOrEmpty()) + { + userGroups.AddRange(evalContextBase.UserGroups); + } + + var organizations = await _memberService.GetByIdsAsync(contact.Organizations.ToArray(), MemberResponseGroup.WithGroups.ToString()); + userGroups.AddRange(organizations.OfType().SelectMany(x => x.Groups)); + + evalContextBase.UserGroups = userGroups.Distinct().ToArray(); + } } } }