Skip to content

Instantly share code, notes, and snippets.

@islaytitans
Last active January 2, 2017 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save islaytitans/c85af8280ababa3cc783292e7bb2d077 to your computer and use it in GitHub Desktop.
Save islaytitans/c85af8280ababa3cc783292e7bb2d077 to your computer and use it in GitHub Desktop.
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