-
-
Save islaytitans/c85af8280ababa3cc783292e7bb2d077 to your computer and use it in GitHub Desktop.
This file contains 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 IndexSampleOrders : ContactIndexableLoadFieldsProcessor | |
{ | |
protected override IEnumerable<IIndexableDataField> GetFields(ContactIndexableLoadFieldsPipelineArgs args) | |
{ | |
IContact contact = args.Contact; | |
var keyInteractionsFacet = contact.GetFacet<IKeyInteractionsFacet>(KeyInteractionsFacet.FacetName); | |
var fields = new List<IIndexableDataField>(); | |
if (keyInteractionsFacet != null) | |
{ | |
IndexableDataField<string> favouriteSampleTypeField = CreateFavouriteSampleTypeField(keyInteractionsFacet.SamplesOrdered); | |
IndexableDataField<string[]> samplesOrderedField = CreateSamplesOrderedField(keyInteractionsFacet.SamplesOrdered); | |
if (favouriteSampleTypeField != null) | |
{ | |
fields.Add(favouriteSampleTypeField); | |
} | |
if (samplesOrderedField != null) | |
{ | |
fields.Add(samplesOrderedField); | |
} | |
} | |
return fields; | |
} | |
private IndexableDataField<string> CreateFavouriteSampleTypeField(IElementCollection<ISampleOrder> samplesOrdered) | |
{ | |
string favouriteSampleType = samplesOrdered.GroupBy(s => s.Type) | |
.OrderByDescending(gp => gp.Count()) | |
.Select(g => g.Key) | |
.FirstOrDefault(); | |
IndexableDataField<string> favouriteSampleTypeField = null; | |
if (favouriteSampleType != null) | |
{ | |
favouriteSampleTypeField = new IndexableDataField<string>("contact.SampleOrder.FavouriteType", favouriteSampleType); | |
} | |
return favouriteSampleTypeField; | |
} | |
private IndexableDataField<string[]> CreateSamplesOrderedField(IElementCollection<ISampleOrder> samplesOrdered) | |
{ | |
List<string> skus = (from s in samplesOrdered | |
select s.Sku).Distinct().ToList(); | |
IndexableDataField<string[]> samplesOrderedField = null; | |
if (skus.Any()) | |
{ | |
samplesOrderedField = new IndexableDataField<string[]>("contact.SampleOrder.Skus", skus.ToArray()); | |
} | |
return samplesOrderedField; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment