Skip to content

Commit

Permalink
BRS-796: Add Audit values to passes in DUP (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
marklise authored Oct 14, 2022
1 parent 4206b58 commit c29665c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 7 deletions.
56 changes: 52 additions & 4 deletions lambda/deletePass/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ exports.handler = async (event, context) => {
if (!event.queryStringParameters) {
return sendResponse(400, { msg: 'Invalid Request' }, context);
}

const currentPSTDateTime = DateTime.now().setZone(TIMEZONE);
const currentTimeISO = currentPSTDateTime.toUTC().toISO();

if (event.queryStringParameters.code) {
logger.debug('Get the specific pass, this person is NOT authenticated but has a code');

Expand Down Expand Up @@ -53,12 +57,32 @@ exports.handler = async (event, context) => {
sk: { S: decodedToken.passId }
},
ExpressionAttributeValues: {
':cancelled': { S: 'cancelled' }
':cancelled': { S: 'cancelled' },
':dateUpdated': { S: currentTimeISO },
':empty_list': { "L": [] }, // For pass objects which do not have an audit property.
':audit_val': {
"L": [
{
"M": {
"by": {
"S": "public"
},
"passStatus": {
"S": 'cancelled'
}
,
"dateUpdated": {
"S": currentTimeISO
}
}
}
]
}
},
// If the pass is already cancelled, error so that we don't decrement the available
// count multiple times.
ConditionExpression: 'attribute_exists(pk) AND attribute_exists(sk) AND (NOT passStatus = :cancelled)',
UpdateExpression: 'SET passStatus = :cancelled',
UpdateExpression: 'SET passStatus = :cancelled, audit = list_append(if_not_exists(audit, :empty_list), :audit_val), dateUpdated = :dateUpdated',
TableName: TABLE_NAME
};
transactionObj.TransactItems.push({
Expand Down Expand Up @@ -93,8 +117,12 @@ exports.handler = async (event, context) => {
const res = await dynamodb.transactWriteItems(transactionObj).promise();
logger.debug('res:', res);

// Prune audit
delete passNoAuth.audit;

return sendResponse(200, { msg: 'Cancelled', pass: passNoAuth }, context);
} else if (event.queryStringParameters.passId && event.queryStringParameters.park) {
// ADMIN
const token = await decodeJWT(event);
const permissionObject = resolvePermissions(token);
if (permissionObject.isAdmin !== true) {
Expand Down Expand Up @@ -129,12 +157,32 @@ exports.handler = async (event, context) => {
sk: { S: event.queryStringParameters.passId }
},
ExpressionAttributeValues: {
':cancelled': { S: 'cancelled' }
':cancelled': { S: 'cancelled' },
':dateUpdated': { S: currentTimeISO },
':empty_list': { "L": [] }, // For pass objects which do not have an audit property.
':audit_val': {
"L": [
{
"M": {
"by": {
"S": "admin"
},
"passStatus": {
"S": 'cancelled'
}
,
"dateUpdated": {
"S": currentTimeISO
}
}
}
]
}
},
// If the pass is already cancelled, error so that we don't decrement the available
// count multiple times.
ConditionExpression: 'attribute_exists(pk) AND attribute_exists(sk) AND (NOT passStatus = :cancelled)',
UpdateExpression: 'SET passStatus = :cancelled',
UpdateExpression: 'SET passStatus = :cancelled, audit = list_append(if_not_exists(audit, :empty_list), :audit_val), dateUpdated = :dateUpdated',
TableName: TABLE_NAME
};
transactionObj.TransactItems.push({
Expand Down
28 changes: 26 additions & 2 deletions lambda/dynamoUtil.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const AWS = require('aws-sdk');
const { logger } = require('./logger');
const { DateTime } = require('luxon');

const TABLE_NAME = process.env.TABLE_NAME || 'parksreso';
const options = {
Expand Down Expand Up @@ -30,16 +31,39 @@ exports.dynamodb = new AWS.DynamoDB();


async function setStatus(passes, status) {
const currentPSTDateTime = DateTime.now().setZone(TIMEZONE);
const currentTimeISO = currentPSTDateTime.toUTC().toISO();

for (let i = 0; i < passes.length; i++) {
let updateParams = {
Key: {
pk: { S: passes[i].pk },
sk: { S: passes[i].sk }
},
ExpressionAttributeValues: {
':statusValue': { S: status }
':statusValue': { S: status },
':empty_list': { "L": [] }, // For pass objects which do not have an audit property.
':dateUpdated': { S: currentTimeISO },
':audit_val': {
"L": [
{
"M": {
"by": {
"S": "system"
},
"passStatus": {
"S": status
}
,
"dateUpdated": {
"S": currentTimeISO
}
}
}
]
}
},
UpdateExpression: 'SET passStatus = :statusValue',
UpdateExpression: 'SET passStatus = :statusValue, audit = list_append(if_not_exists(audit, :empty_list), :audit_val), dateUpdated = :dateUpdated',
ReturnValues: 'ALL_NEW',
TableName: TABLE_NAME
};
Expand Down
26 changes: 25 additions & 1 deletion lambda/writePass/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,28 @@ exports.handler = async (event, context) => {
passObject.Item['passStatus'] = { S: status };
passObject.Item['phoneNumber'] = AWS.DynamoDB.Converter.input(phoneNumber);
passObject.Item['facilityType'] = { S: facilityData.type };
passObject.Item['creationDate'] = { S: currentPSTDateTime.toUTC().toISO() };
passObject.Item['isOverbooked'] = { BOOL: false };
// Audit
passObject.Item['creationDate'] = { S: currentPSTDateTime.toUTC().toISO() };
passObject.Item['dateUpdated'] = { S: currentPSTDateTime.toUTC().toISO() };
passObject.Item['audit'] = {
"L": [
{
"M": {
"by": {
"S": "system"
},
"passStatus": {
"S": status
}
,
"dateUpdated": {
"S": currentPSTDateTime.toUTC().toISO()
}
}
}
]
}

const cancellationLink =
process.env.PUBLIC_FRONTEND +
Expand Down Expand Up @@ -410,6 +430,10 @@ exports.handler = async (event, context) => {
}
});
logger.debug('GCNotify email sent.');

// Prune audit
delete passObject.Item['audit'];

return sendResponse(200, AWS.DynamoDB.Converter.unmarshall(passObject.Item));
} catch (err) {
logger.error('GCNotify error:', err);
Expand Down

0 comments on commit c29665c

Please sign in to comment.