Skip to content

Instantly share code, notes, and snippets.

@islaytitans

islaytitans/IndexSampleOrders.cs Secret

Last active Jan 2, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.