Skip to content

Commit

Permalink
Update location imports for latest data format
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhand committed Nov 20, 2024
1 parent 5572bf5 commit 5d0baeb
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 17 deletions.
16 changes: 0 additions & 16 deletions resources-domain/lambdas/import-producer/src/khpMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,6 @@ const KHP_MAPPING_NODE_SITES: { children: MappingNode } = {
address2: translatableAttributeMapping(siteKey('location/address2')),
city: {
mappings: [
referenceAttributeMapping(siteKey('location/city'), 'cities', {
value: ctx =>
`CA/${lookupProvinceCode(ctx.parentValue.province)}/${
ctx.currentValue
}`,
}),
referenceAttributeMapping(
siteKey('location/region-city'),
'country/province/region/city',
Expand Down Expand Up @@ -486,16 +480,6 @@ export const KHP_MAPPING_NODE: MappingNode = {
}),
primaryLocationCity: {
mappings: [
referenceAttributeMapping('primaryLocationCity', 'cities', {
value: ctx => {
const { primaryLocationProvince } = ctx.rootResource;
return [
'CA',
lookupProvinceCode(primaryLocationProvince),
ctx.currentValue,
].join('/');
},
}),
referenceAttributeMapping(
'primaryLocationRegionCity',
'country/province/region/city',
Expand Down
2 changes: 1 addition & 1 deletion resources-domain/resources-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"es:delete-index:local": "cross-env AWS_REGION=us-east-1 SSM_ENDPOINT=http://localhost:4566 NODE_ENV=local ELASTICSEARCH_CONFIG_node=http://localhost:9200 tsx ./scripts/elasticsearch/delete-index.ts",
"build-scripts": "tsc --project tsconfig.scripts.json",
"reindex": "tsx ./scripts/elasticsearch/reindexResources.ts",
"importLocations": "tsx ./scripts/khp/importLocationsCsvNewFormat.ts"
"importLocations": "tsx ./scripts/khp/importLocationsCsv20241115.ts"
},
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* Copyright (C) 2021-2023 Technology Matters
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

// eslint-disable-next-line import/no-extraneous-dependencies
import { parse } from 'csv-parse';
import fs from 'fs';
import { pgp } from '../../src/connection-pool';

const CANADIAN_PROVINCE_NAME_CODE_MAP = {
Alberta: ['AB', 48],
'British Columbia': ['BC', 59],
Manitoba: ['MB', 46],
'New Brunswick': ['NB', 13],
'Newfoundland and Labrador': ['NL', 10],
'Northwest Territories': ['NT', 61],
'Nova Scotia': ['NS', 12],
Nunavut: ['NU', 62],
Ontario: ['ON', 35],
'Prince Edward Island': ['PE', 11],
Quebec: ['QC', 24],
Saskatchewan: ['SK', 47],
Yukon: ['YT', 60],
} as const;

const CANADIAN_PROVINCE_CODE_NAME_MAP = Object.fromEntries(
Object.entries(CANADIAN_PROVINCE_NAME_CODE_MAP).map(([name, [code]]) => [code, name]),
);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const CANADIAN_PROVINCE_CODE_FR_MAP = {
AB: 'Alberta',
BC: 'Colombie-Britannique',
NL: 'Terre-Neuve-et-Labrador',
PE: 'Île-du-Prince-Édouard',
NS: 'Nouvelle-Écosse',
NB: 'Nouveau-Brunswick',
ON: 'Ontario',
MB: 'Manitoba',
SK: 'Saskatchewan',
YT: 'Yukon',
NT: 'Territoires du Nord-Ouest',
NU: 'Nunavut',
QC: 'Québec',
} as const;

const main = async () => {
if (process.argv.length < 3) {
console.error('Usage: node importLocationsCsv.js <accountSid>');
process.exit(1);
}
const accountSid = process.argv[2];
const targetFilePath = `./reference-data/khp_cities_20241115_${accountSid}.sql`;
const sqlFile = fs.createWriteStream(targetFilePath);
const csvLines = fs
.createReadStream('./reference-data/khp_cities_20241115.csv')
.pipe(parse({ fromLine: 2 }));
/*
sqlFile.write(`--- PROVINCES ---\n\n`);
Object.entries(CANADIAN_PROVINCE_NAME_CODE_MAP).forEach(
([name, [code, geographicCode]]) => {
sqlFile.write(
pgp.as.format(
`
INSERT INTO resources."ResourceReferenceStringAttributeValues" ("accountSid", "list", "id", "value", "language", "info") VALUES ($<accountSid>, 'provinces', $<id>, $<value>, 'en', $<info>)
ON CONFLICT DO NOTHING;
INSERT INTO resources."ResourceReferenceStringAttributeValues" ("accountSid", "list", "id", "value", "language", "info") VALUES ($<accountSid>, 'provinces', $<idFr>, $<value>, 'fr', $<infoFr>)
ON CONFLICT DO NOTHING;
`,
{
accountSid,
id: `CA-${geographicCode}-en`,
idFr: `CA-${geographicCode}-fr`,
value: `CA/${code}`,
info: { name, geographicCode },
infoFr: { name: CANADIAN_PROVINCE_CODE_FR_MAP[code] },
},
),
);
provincesJson.push({
label: name,
value: `CA/${code}`,
});
},
);
*/
sqlFile.write('\n\n--- REGIONS AND CITIES ---\n\n');
sqlFile.write(`
DELETE FROM resources."ResourceReferenceStringAttributes" WHERE "accountSid" = '${accountSid}' AND "list" IN ('country/province/region', 'country/province/region/city');
DELETE FROM resources."ResourceReferenceStringAttributeValues" WHERE "accountSid" = '${accountSid}' AND "list" IN ('country/province/region', 'country/province/region/city');
`);

for await (const line of csvLines) {
const [, provinceCode, region, cityEn] = line as string[];
const province = CANADIAN_PROVINCE_CODE_NAME_MAP[provinceCode];
const sqlStatement = pgp.as.format(
`
INSERT INTO resources."ResourceReferenceStringAttributeValues" ("accountSid", "list", "id", "value", "language", "info") VALUES ($<accountSid>, 'country/province/region', $<regionId>, $<regionValue>, 'en', $<regionInfo>)
ON CONFLICT DO NOTHING;
INSERT INTO resources."ResourceReferenceStringAttributeValues" ("accountSid", "list", "id", "value", "language", "info") VALUES ($<accountSid>, 'country/province/region', $<regionId>, $<regionValue>, 'fr', $<regionInfo>)
ON CONFLICT DO NOTHING;
INSERT INTO resources."ResourceReferenceStringAttributeValues" ("accountSid", "list", "id", "value", "language", "info") VALUES ($<accountSid>, 'country/province/region/city', $<id>, $<value>, 'en', $<info>)
ON CONFLICT DO NOTHING;
INSERT INTO resources."ResourceReferenceStringAttributeValues" ("accountSid", "list", "id", "value", "language", "info") VALUES ($<accountSid>, 'country/province/region/city', $<id>, $<value>, 'fr', $<info>)
ON CONFLICT DO NOTHING;`,
{
accountSid: process.argv[2],
id: `CA-${provinceCode}-${region}-${cityEn}-en`,
legacyId: `CA-${provinceCode}-${cityEn}-en`,
regionId: `CA-${provinceCode}-${region}-en`,
regionIdFr: `CA-${provinceCode}-${region}-fr`,
value: `CA/${provinceCode}/${region}/${cityEn}`,
legacyValue: `CA/${provinceCode}/${cityEn}`,
regionValue: `CA/${provinceCode}/${region}`,
info: { name: cityEn, region, province },
regionInfo: { name: region, province },
},
);
sqlFile.write(sqlStatement);
}

sqlFile.write(`
--- START REIMPORT ---
DELETE FROM resources."Accounts" WHERE "accountSid" = '${accountSid}';
`);
sqlFile.end();
};

main().catch(err => {
console.error(err);
process.exit(1);
});

0 comments on commit 5d0baeb

Please sign in to comment.