Skip to content

Commit

Permalink
NOBUG: Tweak importer, update handleActivity on subarea POST.
Browse files Browse the repository at this point in the history
  • Loading branch information
marklise committed Apr 13, 2022
1 parent cfaa493 commit 090d51b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lambda/subarea/POST/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function handleActivity(event) {
// Set pk/sk
event.body["pk"] = `${event.body.orcs}::${event.body.subAreaName}::${event.body.activity}`;

if (date.length !== 6 || isNaN(date)) {
if (event.body.date.length !== 6 || isNaN(event.body.date)) {
return sendResponse(400, { msg: "Invalid date."}, context);
}

Expand Down
33 changes: 29 additions & 4 deletions tools/convertParksSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ async function doMigration() {
parkName: AWS.DynamoDB.Converter.input(row['Park']),
// subAreas: row['Park Sub Area'], // Add to activities
};
await putItem(parkRecord);
if (await putItem(parkRecord) == false) {
// Record already existed, lets add the subarea to the object.
await updateItem(parkRecord, row['Park Sub Area']);
}

// 2. Add the subarea /w activities record
const parkSubAreaRecord = {
Expand All @@ -119,7 +122,7 @@ async function doMigration() {
// 3. For each activity, add the config for that orc::subarea::activity
for (const activity of activities) {
const activityRecord = {
pk: AWS.DynamoDB.Converter.input('park::' + parkRecord.sk.S + '::' + parkSubAreaRecord.subAreaName.S),
pk: AWS.DynamoDB.Converter.input(parkRecord.sk.S + '::' + parkSubAreaRecord.subAreaName.S + '::' + activity),
sk: { S: 'config' },
parkName: AWS.DynamoDB.Converter.input(parkRecord.parkName.S),
orcs: AWS.DynamoDB.Converter.input(parkRecord.sk.S),
Expand All @@ -135,6 +138,28 @@ async function doMigration() {
});
}

async function updateItem(record, subarea) {
let putParkObj = {
TableName: TABLE_NAME,
Key: {
pk: record.pk,
sk: record.sk
},
UpdateExpression: 'ADD subAreas :subAreas',
ExpressionAttributeValues: {
':subAreas': {
'SS': [subarea]
}
}
};

try {
const res = await dynamodb.updateItem(putParkObj).promise();
} catch (err) {
console.log("Error:", err);
}
}

async function putItem(record) {
let putParkObj = {
TableName: TABLE_NAME,
Expand All @@ -145,9 +170,9 @@ async function putItem(record) {
try {
const res = await dynamodb.putItem(putParkObj).promise();
// If we get here, the park didn't exist already.
// console.log("PUT:", record);
return true;
} catch (err) {
// console.log("Error:", err);
return false;
// Fall through, it already existed.
}
}
Expand Down

0 comments on commit 090d51b

Please sign in to comment.