Skip to content

Commit

Permalink
SDE-160 - limit the number of posts per email for daily digests
Browse files Browse the repository at this point in the history
  • Loading branch information
rharrington-scottlogic committed Aug 21, 2019
1 parent afe9387 commit 4252abe
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions functions/calculateUserDailyDigest.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,27 @@ const formatInputQueryBody = (subscriptions, dateBegin, dateEnd) => subscription
search: [{ value }, { dateRange: [dateBegin, dateEnd] }]
}));

// Not implemented yet
const trimDigest = results => results;
const trimDigest = (results, maxPosts) => {
const digestSections = results.length;
let postPool = digestSections >= maxPosts ? 0 : maxPosts - digestSections;

const newResults = [];

for (const entry of results) {
let postsToCopy = 1;
if (postPool > 0) {
postsToCopy = entry.posts.length - 1 <= postPool ? entry.posts.length : postPool + 1;
postPool -= postsToCopy - 1;
}

newResults.push({
searchTerms: entry.searchTerms,
posts: entry.posts.slice(0, postsToCopy)
});
}

return newResults;
};

const compareSortedArrays = (arr1, arr2) => {
if (arr1.length !== arr2.length) return false;
Expand Down Expand Up @@ -92,7 +111,7 @@ const transformMultiSearchResult = (userID, subscriptions, mSearchResult) => {

results.sort((a, b) => b.searchTerms.length - a.searchTerms.length);

if (totalPostsCount > EMAIL_MAX_POSTS) results = trimDigest(results);
if (totalPostsCount > EMAIL_MAX_POSTS) results = trimDigest(results, EMAIL_MAX_POSTS);

return { userID, results };
};
Expand Down

0 comments on commit 4252abe

Please sign in to comment.