-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update location imports for latest data format
- Loading branch information
1 parent
5572bf5
commit 5d0baeb
Showing
3 changed files
with
148 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
resources-domain/resources-service/scripts/khp/importLocationsCsv20241115.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |