Skip to content

Commit

Permalink
PT-9138: load parent orgationzation user groups in price evaluation m…
Browse files Browse the repository at this point in the history
…iddleware (#27)
  • Loading branch information
ksavosteev authored Oct 6, 2022
1 parent f07f4b6 commit f533674
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
Expand All @@ -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;
Expand All @@ -17,10 +19,13 @@ public class LoadUserToEvalContextMiddleware : IAsyncMiddleware<PromotionEvaluat
{
private readonly IMapper _mapper;
private readonly IMemberResolver _memberIdResolver;
public LoadUserToEvalContextMiddleware(IMapper mapper, IMemberResolver memberIdResolver)
private readonly IMemberService _memberService;

public LoadUserToEvalContextMiddleware(IMapper mapper, IMemberResolver memberIdResolver, IMemberService memberService)
{
_mapper = mapper;
_memberIdResolver = memberIdResolver;
_memberService = memberService;
}

public async Task Run(PromotionEvaluationContext parameter, Func<PromotionEvaluationContext, Task> next)
Expand Down Expand Up @@ -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<string>();

if (!evalContextBase.UserGroups.IsNullOrEmpty())
{
userGroups.AddRange(evalContextBase.UserGroups);
}

var organizations = await _memberService.GetByIdsAsync(contact.Organizations.ToArray(), MemberResponseGroup.WithGroups.ToString());
userGroups.AddRange(organizations.OfType<Organization>().SelectMany(x => x.Groups));

evalContextBase.UserGroups = userGroups.Distinct().ToArray();
}
}
}
}
Expand Down

0 comments on commit f533674

Please sign in to comment.