Skip to content

Commit

Permalink
BRS-161: Expiry EventBridge fix (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Truong authored Nov 20, 2021
1 parent 8ee58d8 commit 32907fb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 68 deletions.
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## DUP Pass state diagram
The following is the state diagram for passes:

![Pass State Diagram](https://raw.github.com/bcgov/parks-reso-api/main/docs/StateDiagramForPasses.svg)
4 changes: 4 additions & 0 deletions docs/StateDiagramForPasses.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 1 addition & 12 deletions lambda/checkActivation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.handler = async (event, context) => {
};

// Look for today's activations
let todaysDate = formatDate(new Date());
let todaysDate = new Date().toISOString().split('T')[0];

try {
queryObj.ExpressionAttributeValues = {};
Expand Down Expand Up @@ -45,14 +45,3 @@ exports.handler = async (event, context) => {
return sendResponse(200, { msg: 'Activation Check Complete' }, context);
}
};

function formatDate(d) {
let month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();

if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;

return [year, month, day].join('-');
}
99 changes: 43 additions & 56 deletions lambda/checkExpiry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,48 @@ const { runQuery, setStatus } = require('../dynamoUtil');
const { sendResponse } = require('../responseUtil');

exports.handler = async (event, context) => {
console.log('check expiry', event);

let queryObj = {
TableName: process.env.TABLE_NAME
};

// Look for today's expiries
let yesterdaysDate = formatDate(new Date())

try {
queryObj.ExpressionAttributeValues = {};
queryObj.ExpressionAttributeValues[':pk'] = { S: 'park' };
queryObj.KeyConditionExpression = 'pk =:pk';
console.log("queryObj:", queryObj);

const parkData = await runQuery(queryObj);

for(let i=0;i < parkData.length;i++) {
let passQuery = {
TableName: process.env.TABLE_NAME
};
passQuery.ExpressionAttributeNames = {
'#dateselector': 'date'
};
passQuery.ExpressionAttributeValues = {};
passQuery.ExpressionAttributeValues[':pk'] = { S: 'pass::' + parkData[i].sk };
passQuery.ExpressionAttributeValues[':yesterdaysDate'] = { S: yesterdaysDate };
passQuery.ExpressionAttributeValues[':reservedStatus'] = { S: 'reserved' };
passQuery.KeyConditionExpression = 'pk =:pk';
passQuery.FilterExpression = 'begins_with(#dateselector, :yesterdaysDate) AND passStatus =:reservedStatus';

console.log("passQuery:", passQuery);
const passData = await runQuery(passQuery);
console.log("passData:", passData);

await setStatus(passData, 'expired');
}
return sendResponse(200, {}, context);
} catch (err) {
console.log(err);
return sendResponse(200, {msg: 'Activation Check Complete'}, context);
console.log('check expiry', event);

let queryObj = {
TableName: process.env.TABLE_NAME
};

// Look for today's expiries
let yd = new Date();
yd.setDate(yd.getDate() - 1);
const yesterdaysDate = yd.toISOString().split('T')[0];

try {
queryObj.ExpressionAttributeValues = {};
queryObj.ExpressionAttributeValues[':pk'] = { S: 'park' };
queryObj.KeyConditionExpression = 'pk =:pk';
console.log("queryObj:", queryObj);

const parkData = await runQuery(queryObj);

for (let i = 0; i < parkData.length; i++) {
let passQuery = {
TableName: process.env.TABLE_NAME
};
passQuery.ExpressionAttributeNames = {
'#dateselector': 'date'
};
passQuery.ExpressionAttributeValues = {};
passQuery.ExpressionAttributeValues[':pk'] = { S: 'pass::' + parkData[i].sk };
passQuery.ExpressionAttributeValues[':yesterdaysDate'] = { S: yesterdaysDate };
passQuery.ExpressionAttributeValues[':activeStatus'] = { S: 'active' };
passQuery.KeyConditionExpression = 'pk =:pk';
passQuery.FilterExpression = 'begins_with(#dateselector, :yesterdaysDate) AND passStatus =:activeStatus';

console.log("passQuery:", passQuery);
const passData = await runQuery(passQuery);
console.log("passData:", passData);

await setStatus(passData, 'expired');
}
return sendResponse(200, {}, context);
} catch (err) {
console.log(err);
return sendResponse(200, { msg: 'Activation Check Complete' }, context);
}
}



function formatDate(d) {
let month = '' + (d.getMonth() + 1),
day = '' + (d.getDate() - 1), // We need yesterday's date to check for moving active -> expired
year = d.getFullYear();

if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;

return [year, month, day].join('-');
}

0 comments on commit 32907fb

Please sign in to comment.