Skip to content

Instantly share code, notes, and snippets.

@troygoode
Created July 6, 2011 16:57
Show Gist options
  • Save troygoode/1067763 to your computer and use it in GitHub Desktop.
Save troygoode/1067763 to your computer and use it in GitHub Desktop.
Generic Buy Signal
function(contact, activities, config){
var sortByActivityDateDescending = function(activity1, activity2){
if (activity1.activityDate > activity2.activityDate) return -1;
if (activity1.activityDate < activity2.activityDate) return 1;
return 0;
};
if(config.timeframe)
var earliestDateToProcess = new Date().setDate(newDate().getDate() - config.timeframe);
var sortedActivities = activities.sort(sortByActivityDateDescending);
var valueToReturn = 1;
var threshold = config.count ? config.count : 1;
var count = 0;
var mostRecentActivityDate;
for(var i = 0; i < sortedActivities.length && (!earliestDateToProcess || sortedActivities[i].date > earliestDateToProcess); i++){
var activity = sortedActivities[i];
if(activity.type === config.activityType){
count++;
if(!mostRecentActivityDate)
mostRecentActivityDate = activity.date;
}
if(count >= threshold)
return {
score: valueToReturn,
date: mostRecentActivityDate
};
}
return {score: 0, date: null};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment