From 32907fbc610b8541e0373b2dcda168d6ef7e6aac Mon Sep 17 00:00:00 2001 From: Daniel Truong Date: Fri, 19 Nov 2021 16:10:35 -0800 Subject: [PATCH] BRS-161: Expiry EventBridge fix (#29) --- docs/README.md | 4 ++ docs/StateDiagramForPasses.svg | 4 ++ lambda/checkActivation/index.js | 13 +---- lambda/checkExpiry/index.js | 99 ++++++++++++++------------------- 4 files changed, 52 insertions(+), 68 deletions(-) create mode 100644 docs/README.md create mode 100644 docs/StateDiagramForPasses.svg diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 00000000..8126e4af --- /dev/null +++ b/docs/README.md @@ -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) \ No newline at end of file diff --git a/docs/StateDiagramForPasses.svg b/docs/StateDiagramForPasses.svg new file mode 100644 index 00000000..05fd0005 --- /dev/null +++ b/docs/StateDiagramForPasses.svg @@ -0,0 +1,4 @@ + + + +
Cancelled prior to date
Cancelled prior to date
Day Change to 12:01 AM
Day Change to 12:01 AM
Reserved
Reserved
Cancelled
Cancelled
Same Day cancellation
Same Day cancellation
Day changed to 12:01AM the next day
Day changed to 12:01AM the next day
Active
Active
Expired
Expired
Viewer does not support full SVG 1.1
\ No newline at end of file diff --git a/lambda/checkActivation/index.js b/lambda/checkActivation/index.js index b0c989e0..39b3aa00 100644 --- a/lambda/checkActivation/index.js +++ b/lambda/checkActivation/index.js @@ -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 = {}; @@ -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('-'); -} diff --git a/lambda/checkExpiry/index.js b/lambda/checkExpiry/index.js index 81784241..ffc328e3 100644 --- a/lambda/checkExpiry/index.js +++ b/lambda/checkExpiry/index.js @@ -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('-'); -} \ No newline at end of file