Skip to content

Commit

Permalink
Merge branch 'master' into SDE-139-individual-lambda-iam-roles
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverForeman committed Aug 22, 2019
2 parents e2e15bc + ae796ad commit 88752f5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
- run:
name: Run ESLint
command: npm run lint
- run:
name: Run Unit Tests
command: npm test
serverless-deploy:
docker:
- image: circleci/node:10.15.1
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ circleci.exe

# Env directories
env/

# VS Code Config Directory
.vscode/
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
9 changes: 4 additions & 5 deletions functions/scheduledDailyDigests.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { DynamoDB, Lambda } from 'aws-sdk';

const dynamoDbDocumentClient = new DynamoDB.DocumentClient();
const lambda = new Lambda();

const { SUBSCRIPTIONS_TABLE: TableName, USER_DIGEST_LAMBDA_NAME: FunctionName } = process.env;

export const handler = async () => {
const { SUBSCRIPTIONS_TABLE: TableName, USER_DIGEST_LAMBDA_NAME: FunctionName } = process.env;
const dynamoDbDocumentClient = new DynamoDB.DocumentClient();
const lambda = new Lambda();

const scanParams = { TableName };
const { Items } = await dynamoDbDocumentClient.scan(scanParams).promise();
await Promise.all(
Expand Down
40 changes: 40 additions & 0 deletions tests/scheduledDailyDigests.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { DynamoDB, Lambda } from 'aws-sdk';
import { handler } from '../functions/scheduledDailyDigests';

jest.mock('aws-sdk');

const subscriptionsTableData = {
Items: [
{ userId: 1, subscriptions: [] },
{ userId: 2, subscriptions: ['test'] },
{ userId: 3, subscriptions: ['test1', 'test2'] }
]
};

const mockDynamoDbScan = {
promise: jest.fn()
};

const mockLambdaInvoke = {
promise: jest.fn()
};

DynamoDB.DocumentClient.mockImplementation(() => ({ scan: () => mockDynamoDbScan }));

Lambda.mockImplementation(() => ({ invoke: () => mockLambdaInvoke }));

describe('scheduledDailyDigests function', () => {
beforeAll(() => {
process.env = Object.assign(process.env, {
SUBSCRIPTIONS_TABLE: 'subscriptions',
USER_DIGEST_LAMBDA_NAME: 'calculateUserDailyDigest'
});
});

it('invokes calculateUserDailyDigest for each user with at least one subscription', async () => {
mockDynamoDbScan.promise.mockResolvedValue(subscriptionsTableData);
mockLambdaInvoke.promise.mockResolvedValue();
await handler();
expect(mockLambdaInvoke.promise).toHaveBeenCalledTimes(2);
});
});

0 comments on commit 88752f5

Please sign in to comment.