Skip to content

Commit

Permalink
PDR-326 updating AWS-SDK to V3 (#331)
Browse files Browse the repository at this point in the history
* PDR-326 updating AWS-SDK to V3

* sleep for 15s to give DynamoDB a chance to wake up
  • Loading branch information
cameronpettit authored May 13, 2024
1 parent 06e1923 commit f37fd92
Show file tree
Hide file tree
Showing 25 changed files with 2,079 additions and 1,867 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/on-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ jobs:
port: 8000
cors: "*"

- run: yarn test
- name: Sleep so DynamoDB has time to wake up
uses: GuillaumeFalourd/wait-sleep-action@v1
with:
time: '15' # for 15 seconds

- run: yarn test --coverage --runInBand
env:
IS_OFFLINE: true
AWS_REGION: local-env
Expand Down
58 changes: 30 additions & 28 deletions __tests__/activity.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const AWS = require("aws-sdk");
const { DocumentClient } = require("aws-sdk/clients/dynamodb");
const { DynamoDBClient, PutItemCommand, GetItemCommand } = require('@aws-sdk/client-dynamodb');
const { marshall, unmarshall } = require('@aws-sdk/util-dynamodb');
const { REGION, ENDPOINT, TABLE_NAME } = require("./global/settings");
const {
PARKSLIST,
Expand All @@ -19,11 +19,7 @@ const emptyRole = {
};

async function setupDb() {
new AWS.DynamoDB({
region: REGION,
endpoint: ENDPOINT,
});
docClient = new DocumentClient({
docClient = new DynamoDBClient({
region: REGION,
endpoint: ENDPOINT,
convertEmptyValues: true,
Expand All @@ -47,12 +43,12 @@ async function setupDb() {
}

async function genericPutDocument(item) {
return await docClient
.put({
TableName: TABLE_NAME,
Item: item,
})
.promise();
const input = {
Item: marshall(item, { convertEmptyValues: true }),
TableName: TABLE_NAME,
};
const command = new PutItemCommand(input);
return await docClient.send(command);
}

describe("Activity Test", () => {
Expand All @@ -63,7 +59,7 @@ describe("Activity Test", () => {
});

const mockedUnauthenticatedUser = {
decodeJWT: jest.fn((event) => {}),
decodeJWT: jest.fn((event) => { }),
resolvePermissions: jest.fn((token) => {
return {
isAdmin: false,
Expand All @@ -77,7 +73,7 @@ describe("Activity Test", () => {
};

const mockedSysadmin = {
decodeJWT: jest.fn((event) => {}),
decodeJWT: jest.fn((event) => { }),
resolvePermissions: jest.fn((token) => {
return {
isAdmin: true,
Expand Down Expand Up @@ -220,14 +216,18 @@ describe("Activity Test", () => {
expect(response.statusCode).toBe(200);

// Expect no variance to be created
const doc = await docClient.get({
TableName: TABLE_NAME,
const input = {
Key: {
pk: `variance::${SUBAREA_ENTRIES[0].orcs}::202201`,
sk: `${SUBAREA_ENTRIES[0].pk.split("::")[0]}}::${SUBAREA_ENTRIES[0].pk.split("::")[1]}`
pk: marshall(`variance::${SUBAREA_ENTRIES[0].orcs}::202201`),
sk: marshall(`${SUBAREA_ENTRIES[0].pk.split("::")[0]}::${SUBAREA_ENTRIES[0].pk.split("::")[1]}`)
},
}).promise();
expect(doc).toEqual({});
TableName: TABLE_NAME,
};
const command = new GetItemCommand(input);
const doc = await docClient.send(command);
expect(doc?.Item).toBe(undefined);



// Change year and create a new record
const secondResponse = await activityPOST.handlePost(
Expand Down Expand Up @@ -257,14 +257,16 @@ describe("Activity Test", () => {
expect(secondResponse.statusCode).toBe(200);

// Expect variance to be created
const doc2 = await docClient.get({
TableName: TABLE_NAME,
const input2 = {
Key: {
pk: `variance::${SUBAREA_ENTRIES[0].orcs}::202301`,
sk: `${SUBAREA_ENTRIES[0].pk.split("::")[0]}::${SUBAREA_ENTRIES[0].pk.split("::")[1]}`
pk: marshall(`variance::${SUBAREA_ENTRIES[0].orcs}::202301`),
sk: marshall(`${SUBAREA_ENTRIES[0].pk.split("::")[0]}::${SUBAREA_ENTRIES[0].pk.split("::")[1]}`)
},
}).promise();
expect(doc2.Item).toEqual({
TableName: TABLE_NAME,
};
const command2 = new GetItemCommand(input2);
const doc2 = await docClient.send(command2);
expect(unmarshall(doc2?.Item)).toEqual({
parkName: 'Cultus Lake Park',
orcs: '0041',
sk: '0087::Day Use',
Expand All @@ -279,7 +281,7 @@ describe("Activity Test", () => {
}],
resolved: false,
subAreaId: '0087',
roles: [ 'sysadmin', '0041:0087'],
roles: ['sysadmin', '0041:0087'],
subAreaName: 'TBD',
bundle: 'N/A'
});
Expand Down
13 changes: 5 additions & 8 deletions __tests__/global/setup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const AWS = require('aws-sdk');
const { DynamoDB } = require('@aws-sdk/client-dynamodb');

const { REGION, ENDPOINT, TABLE_NAME, CONFIG_TABLE_NAME, NAME_CACHE_TABLE_NAME } = require('./settings');
const { logger } = require('../../lambda/logger');

module.exports = async () => {
dynamoDb = new AWS.DynamoDB({
dynamoDb = new DynamoDB({
region: REGION,
endpoint: ENDPOINT
});
Expand Down Expand Up @@ -62,8 +62,7 @@ module.exports = async () => {
}
}
]
})
.promise();
});

console.log("Creating name-cache table.");
await dynamoDb
Expand All @@ -85,8 +84,7 @@ module.exports = async () => {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
})
.promise();
});

console.log("Creating config table.");
await dynamoDb
Expand All @@ -108,8 +106,7 @@ module.exports = async () => {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
})
.promise();
});
} catch (err) {
logger.error(err);
}
Expand Down
13 changes: 5 additions & 8 deletions __tests__/global/teardown.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const AWS = require('aws-sdk');
const { DynamoDB } = require('@aws-sdk/client-dynamodb');

const { REGION, ENDPOINT, TABLE_NAME, CONFIG_TABLE_NAME, NAME_CACHE_TABLE_NAME } = require('./settings');
const { logger } = require('../../lambda/logger');

module.exports = async () => {
dynamoDb = new AWS.DynamoDB({
dynamoDb = new DynamoDB({
region: REGION,
endpoint: ENDPOINT
});
Expand All @@ -13,18 +13,15 @@ module.exports = async () => {
await dynamoDb
.deleteTable({
TableName: TABLE_NAME
})
.promise();
});
await dynamoDb
.deleteTable({
TableName: NAME_CACHE_TABLE_NAME
})
.promise();
});
await dynamoDb
.deleteTable({
TableName: CONFIG_TABLE_NAME
})
.promise();
});
} catch (err) {
logger.error(err);
}
Expand Down
24 changes: 12 additions & 12 deletions lambda/activity/POST/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const AWS = require('aws-sdk');
const { marshall, unmarshall } = require('@aws-sdk/util-dynamodb');
const { dynamodb, runQuery, TABLE_NAME, getOne, FISCAL_YEAR_FINAL_MONTH, TIMEZONE } = require('../../dynamoUtil');
const { sendResponse } = require('../../responseUtil');
const { decodeJWT, resolvePermissions } = require('../../permissionUtil');
Expand Down Expand Up @@ -94,10 +94,10 @@ async function main(event, context, lock = null) {
lock = true;
}

// Check variance. If 'warn', and no notes, return the variance without saving it and don't update the activity.
// Check variance. If 'warn', and no notes, return the variance without saving it and don't update the activity.
const fields = await checkVarianceTrigger(body);
const notesOverride = Boolean(body.notes);
if (warnIfVariance && fields.length && !notesOverride) {
if (warnIfVariance && fields.length && !notesOverride) {
return sendResponse(200, { msg: 'Variance triggered, nothing saved.', fields: fields, varianceWarning: true }, context);
} else {
// Create variance object if variance was triggered or if a variance note exists
Expand Down Expand Up @@ -125,7 +125,7 @@ async function deleteVariance(body) {

logger.info('Deleting variance record:', params);

await dynamodb.deleteItem(params).promise();
await dynamodb.deleteItem(params);
}

async function checkVarianceTrigger(body) {
Expand Down Expand Up @@ -204,7 +204,7 @@ async function createVariance(body, fields) {
}
logger.info('Creating Variance:', JSON.stringify(body));
try {
const newObject = {
const newObject = marshall({
pk: `variance::${body?.orcs}::${body?.date}`,
sk: `${body?.subAreaId}::${body?.activity}`,
fields: fields,
Expand All @@ -216,12 +216,12 @@ async function createVariance(body, fields) {
subAreaId: body?.subAreaId,
bundle: bundle,
roles: ['sysadmin', `${body?.orcs}:${body?.subAreaId}`],
};
}, { removeUndefinedValues: true });
const putObj = {
TableName: TABLE_NAME,
Item: AWS.DynamoDB.Converter.marshall(newObject),
Item: newObject,
};
await dynamodb.putItem(putObj).promise();
await dynamodb.putItem(putObj);
} catch (e) {
logger.error(e);
}
Expand Down Expand Up @@ -311,12 +311,12 @@ async function handleLockUnlock(record, lock, context) {
ReturnValues: 'ALL_NEW',
};
try {
const res = await dynamodb.updateItem(updateObj).promise();
const res = await dynamodb.updateItem(updateObj);
logger.info(`Updated record pk: ${record.pk}, sk: ${record.sk} `);
const s = lock ? 'locked' : 'unlocked';
return sendResponse(200, {
msg: `Record successfully ${s}`,
data: AWS.DynamoDB.Converter.unmarshall(res.Attributes),
data: unmarshall(res.Attributes),
});
} catch (err) {
if (err.code === 'ConditionalCheckFailedException') {
Expand Down Expand Up @@ -366,14 +366,14 @@ async function handleActivity(body, lock = false, context) {

body['isLocked'] = lock ?? false;

const newObject = AWS.DynamoDB.Converter.marshall(body);
const newObject = marshall(body, {removeUndefinedValues: true});

let putObject = {
TableName: TABLE_NAME,
Item: newObject,
};

await dynamodb.putItem(putObject).promise();
await dynamodb.putItem(putObject);
logger.info('Activity Updated.');
return sendResponse(200, body, context);
} catch (err) {
Expand Down
Loading

0 comments on commit f37fd92

Please sign in to comment.