Created
March 7, 2023 15:01
-
-
Save AThraen/1ad95969e8a2d0bf76f29b2168680198 to your computer and use it in GitHub Desktop.
Episerver/Optimizely VisitorGroupSyncService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class VisitorGroupSyncService | |
| { | |
| private readonly IVisitorGroupRepository _visitorGroupRepository; | |
| private readonly IVisitorGroupCriterionRepository _visitorGroupCriterionRepository; | |
| public VisitorGroupSyncService(IVisitorGroupRepository visitorGroupRepository, IVisitorGroupCriterionRepository visitorGroupCriterionRepository) | |
| { | |
| _visitorGroupRepository = visitorGroupRepository; | |
| _visitorGroupCriterionRepository = visitorGroupCriterionRepository; | |
| } | |
| public List<VisitorGroup> LoadGroupsFromJson(string json) | |
| { | |
| var options = new JsonSerializerOptions(); | |
| options.Converters.Add(new PolymorphicJsonConverter<ICriterionModel>()); | |
| options.Converters.Add(new IgnoreTypeInJsonConverter<IVisitorGroupCriterionDefinition>()); | |
| options.Converters.Add(new IgnoreTypeInJsonConverter<EPiServer.Data.Identity>()); | |
| //options.Converters.Add(new CustomStringConversionJsonConverter<EPiServer.Data.Identity>(i => i.ToString(), s => EPiServer.Data.Identity.Parse(s))); | |
| return JsonSerializer.Deserialize<List<VisitorGroup>>(json, options); | |
| } | |
| public void LoadAndSyncVisitorGroups(string json) | |
| { | |
| var currlist = _visitorGroupRepository.List(); | |
| var lst = LoadGroupsFromJson(json); | |
| var criterions = _visitorGroupCriterionRepository.List().ToList(); | |
| //Sync groups | |
| foreach(var grp in lst) | |
| { | |
| //Check if group exists | |
| var currgrp = currlist.FirstOrDefault(g => g.Id == grp.Id); | |
| if(currgrp != null) | |
| { | |
| //Existing group, update all properties | |
| } else | |
| { | |
| //New group, create | |
| currgrp = new VisitorGroup(); | |
| currgrp.Id = grp.Id; | |
| } | |
| currgrp.Name = grp.Name; | |
| currgrp.Notes = grp.Notes; | |
| currgrp.PointsThreshold = grp.PointsThreshold; | |
| currgrp.IsSecurityRole = grp.IsSecurityRole; | |
| currgrp.IsNew = grp.IsNew; | |
| currgrp.CriteriaOperator = grp.CriteriaOperator; | |
| currgrp.EnableStatistics = grp.EnableStatistics; | |
| //Check criteria | |
| foreach(var criteria in grp.Criteria) | |
| { | |
| var currcrit = currgrp.Criteria.FirstOrDefault(c => c.Id == criteria.Id); | |
| if(currcrit != null) | |
| { | |
| //Existing criteria | |
| } else | |
| { | |
| //Create criteria? | |
| currcrit = criterions.FirstOrDefault(vgc => vgc.TypeName == criteria.TypeName); | |
| if (currcrit == null) continue; //problem with this criterion. | |
| currcrit.Id = criteria.Id; | |
| currgrp.Criteria.Add(currcrit); | |
| } | |
| //Sync criteria properties | |
| currcrit.Model = criteria.Model; | |
| currcrit.Points = criteria.Points; | |
| currcrit.Required = criteria.Required; | |
| } | |
| //Remove criteria in local group that does not exist in imported. | |
| currgrp.Criteria = currgrp.Criteria.Where(vgc => grp.Criteria.Any(gc => gc.Id == vgc.Id)).ToList(); | |
| _visitorGroupRepository.Save(currgrp); | |
| } | |
| } | |
| public string ExtractVisitorGroups(params Guid[] GroupIDs) | |
| { | |
| var lst = _visitorGroupRepository.List().Where(vg => !GroupIDs.Any() || GroupIDs.Contains(vg.Id)).ToList(); | |
| var options = new JsonSerializerOptions(); | |
| options.WriteIndented = true; | |
| options.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault | System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull; | |
| options.Converters.Add(new PolymorphicJsonConverter<ICriterionModel>()); | |
| options.Converters.Add(new IgnoreTypeInJsonConverter<IVisitorGroupCriterionDefinition>()); | |
| options.Converters.Add(new IgnoreTypeInJsonConverter<EPiServer.Data.Identity>()); | |
| return JsonSerializer.Serialize(lst, options: options); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment