Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDR-313: adding migration and subarea name update script #318

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions migrations-data/updateSubareaName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
const { getParks, getSubAreas, TABLE_NAME, dynamodb, runQuery } = require("../lambda/dynamoUtil");
const { marshall } = require("@aws-sdk/util-dynamodb");
const { DateTime } = require('luxon');

const minDate = DateTime.now().set({
year: 2000,
month: 1,
day: 1,
}).setZone('America/Vancouver');

async function updateSubareaName(orcs, oldName, newName) {
const parks = await getParks();
let park = parks.find((p) => p.orcs === orcs);
const subareas = await getSubAreas(orcs);
let subarea = subareas.find((s) => s.subAreaName === oldName);
// Running a second time
if (!subarea) {
subarea = subareas.find((s) => s.subAreaName === newName);
}
const said = subarea?.sk || null;

// Update Park
try {
const saIndex = park.subAreas.findIndex((s) => s.name === oldName);
if (saIndex > -1) {
park.subAreas[saIndex].name = newName;
await marshallAndPutItem(park);
console.log(`Updated ${orcs} - ${oldName} to ${newName} (Park).`);
} else {
console.log(`Already updated ${orcs} - ${oldName} to ${newName} (Park).`);
}
} catch (error) {
console.log(`Failed to update ${orcs} - ${oldName} to ${newName} (Park):`, error);
}

// Update subarea
try {
if (subarea) {
const updateItem = {
TableName: TABLE_NAME,
Key: {
pk: { S: subarea.pk },
sk: { S: subarea.sk }
},
UpdateExpression: 'SET subAreaName = :subAreaName',
ExpressionAttributeValues: {
':subAreaName': { S: newName }
}
};
await dynamodb.updateItem(updateItem).promise();
console.log(`Updated ${orcs} - ${oldName} to ${newName} (Subarea).`);
} else {
console.log(`Already updated ${orcs} - ${oldName} to ${newName} (Subarea).`);
}
} catch (error) {
console.log(`Failed to update ${orcs} - ${oldName} to ${newName} (Subarea):`, error);
}

// update configs
try {
if (subarea) {
const configQuery = {
TableName: TABLE_NAME,
KeyConditionExpression: 'pk = :pk',
ExpressionAttributeValues: {
':pk': { S: `config::${said}` }
}
};
const configs = await runQuery(configQuery);
for (let config of configs) {
config.subAreaName = newName;
await marshallAndPutItem(config);
}
console.log(`Updated ${orcs} - ${oldName} to ${newName} (Configs).`);
}
} catch (error) {
console.log(`Failed to update ${orcs} - ${oldName} to ${newName} (Configs):`, error);
}

// variances
// variance pks include dates, so we either have to scan (very long), or choose a start date to iterate through
// we will use the latter.
try {
for (const activity of subarea.activities.values) {
const today = DateTime.now().setZone('America/Vancouver').toFormat('yyyyLL');
let currentDate = minDate.toFormat('yyyyLL');
let runningDate = minDate;
do {
const varianceQuery = {
TableName: TABLE_NAME,
KeyConditionExpression: 'pk = :pk AND sk = :sk',
ExpressionAttributeValues: {
':pk': { S: `variance::${orcs}::${currentDate}` },
':sk': { S: `${said}::${activity}` }
}
};
const variances = await runQuery(varianceQuery);
if (variances?.length) {
for (let variance of variances) {
variance.subAreaName = newName;
await marshallAndPutItem(variance);
}
}
runningDate = runningDate.plus({ months: 1 });
currentDate = runningDate.toFormat('yyyyLL');
} while (currentDate <= today);
}
console.log(`Updated ${orcs} - ${oldName} to ${newName} (Variances).`);
} catch (error) {
console.log(`Failed to update ${orcs} - ${oldName} to ${newName} (Variancess):`, error);
}

// activity records
try {
for (const activity of subarea?.activities?.values) {
const activityGet = {
TableName: TABLE_NAME,
KeyConditionExpression: 'pk = :pk',
ExpressionAttributeValues: {
':pk': { S: `${said}::${activity}` }
}
};
const records = await runQuery(activityGet);
for (let record of records) {
record['subAreaName'] = newName;
if (record?.config?.subAreaName) {
record.config.subAreaName = newName;
}
await marshallAndPutItem(record);
}
}
console.log(`Updated ${orcs} - ${oldName} to ${newName} (Activity Records).`);
} catch (error) {
console.log(`Failed to update ${orcs} - ${oldName} to ${newName} (Activity records):`, error);
}
}

async function marshallAndPutItem(item) {
try {
const putItem = {
TableName: TABLE_NAME,
Item: marshall(item),
};
await dynamodb.putItem(putItem).promise();
} catch {
throw 'failed to put item';
}
}

module.exports = {
updateSubareaName,
marshallAndPutItem
};
59 changes: 59 additions & 0 deletions migrations/20240308143600-subAreaNameUpdates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { TABLE_NAME, getSubAreas, dynamodb } = require("../lambda/dynamoUtil");
const { updateSubareaName, marshallAndPutItem } = require("../migrations-data/updateSubareaName");


// update Diamond Head Subarea in Garibaldi to have the Backcountry Cabins activity
async function updateDiamondHead() {
const subareas = await getSubAreas('0007');
let diamondHead = subareas.find((s) => s.subAreaName === 'Diamond Head');
const said = diamondHead.sk;
// add activity to subarea
try {
// get subarea
let activities = diamondHead.activities.values;
if (activities.indexOf('Backcountry Cabins') === -1) {
activities.push('Backcountry Cabins');
const activityUpdate = {
TableName: TABLE_NAME,
Key: {
pk: { S: diamondHead.pk },
sk: { S: diamondHead.sk }
},
UpdateExpression: 'SET activities = :activities',
ExpressionAttributeValues: {
':activities': { SS: activities }
}
};
await dynamodb.updateItem(activityUpdate).promise();
console.log('Updated Diamond Head activity');
} else {
console.log('Diamond Head already updated.');
}
} catch (error) {
console.log('Failed to update Diamond Head with Backcountry Cabins');
console.log('error:', error);
}

// add Diamond Head Backcountry Cabins config
try {
const item = {
pk: `config::${said}`,
sk: 'Backcountry Cabins',
subAreaId: said,
parkName: diamondHead.parkName,
orcs: diamondHead.orcs,
attendanceModifier: 3.2,
subAreaName: diamondHead.subAreaName,
};
await marshallAndPutItem(item);
console.log('Diamond Head config object created.');
} catch (error) {
console.log('Failed to update Diamond Head config object');
console.log('error:', error);
}
}

updateSubareaName('0007', 'Garibaldi Lake', 'Rubble Creek');
updateSubareaName('0041', 'Cultus East Group Use', 'Honeymoon Bay Group');
updateSubareaName('0041', 'Cultus West Group Use', 'Westside Group');
updateDiamondHead();
Loading