Skip to content

Instantly share code, notes, and snippets.

@nahidakbar
Last active September 9, 2024 04:20
Show Gist options
  • Save nahidakbar/8b4021d905f1bdb53e87e78e77f694d4 to your computer and use it in GitHub Desktop.
Save nahidakbar/8b4021d905f1bdb53e87e78e77f694d4 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const path = require("path");
const FILE_REGEXP = /^([a-z]+)-([a-z_]+)-.*[.]csv$/;
const filename = process.argv[process.argv.length - 1] || '';
const basename = path.basename(filename);
if (!filename || !basename || !basename.match(FILE_REGEXP)) {
throw Error('A csv file input must be specified of the format <centreId>-<parkerType>-<date>.csv. E.g. kotara-westfield_secondary-2021-01-01.csv');
}
const [centreId, parkerType] = basename.match(FILE_REGEXP).slice(1, 3);
let headers, content = fs.readFileSync(filename, 'utf8');
[headers, ...content] = content.split('\n').map(row => row.split(','));
const scentregroupIdColumnIndex = headers.indexOf('profileintegrationid');
if (scentregroupIdColumnIndex === -1) {
throw Error('profileintegrationid column not found in csv file');
}
const scentregroupIds = content.map(row => row[scentregroupIdColumnIndex]).filter(id => id).sort().filter((id, index, arr) => arr.indexOf(id) === index);
const updateRequests = scentregroupIds.map(scentregroupId => ({
scentregroupId,
centreId,
parkerType
}))
console.log(JSON.stringify(updateRequests, null, 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment