|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 4 | +import { BaseMigration, Migration } from '~/core/database'; |
| 5 | + |
| 6 | +interface ExternalDepartmentId { |
| 7 | + departmentId: string; |
| 8 | + name?: string; |
| 9 | +} |
| 10 | + |
| 11 | +@Migration('2025-09-17T09:00:00') |
| 12 | +export class CreateUsedDeptIdListMigration extends BaseMigration { |
| 13 | + async up() { |
| 14 | + const intaactFilePath = new URL( |
| 15 | + '../../../../../All-Department-Ids-From-Intaact.csv', |
| 16 | + import.meta.url, |
| 17 | + ); |
| 18 | + const cordFilePath = new URL( |
| 19 | + '../../../../../prod_deptIds_ids_only.csv', |
| 20 | + import.meta.url, |
| 21 | + ); |
| 22 | + |
| 23 | + const intaactFileContent = await readFile(intaactFilePath, 'utf-8'); |
| 24 | + const cordFileContent = await readFile(cordFilePath, 'utf-8'); |
| 25 | + |
| 26 | + const intaactRows = intaactFileContent.trim().split(/\r?\n/).slice(1); // Skip header |
| 27 | + const cordRows = cordFileContent.trim().split(/\r?\n/).slice(1); // Skip header |
| 28 | + |
| 29 | + const intaactList: ExternalDepartmentId[] = intaactRows.flatMap((row) => { |
| 30 | + const [departmentId, name] = row.split(','); |
| 31 | + if (departmentId) { |
| 32 | + return { departmentId, name }; |
| 33 | + } |
| 34 | + return []; |
| 35 | + }); |
| 36 | + |
| 37 | + const prunedIntaactList = intaactList.flatMap((row) => |
| 38 | + !cordRows.includes(row.departmentId) ? row : [], |
| 39 | + ); |
| 40 | + |
| 41 | + const header = ['Department ID', 'Department Name']; |
| 42 | + const lines = prunedIntaactList.map( |
| 43 | + (row) => `${row.departmentId},${row.name ?? ''}`, |
| 44 | + ); |
| 45 | + const csvContent = [header.join(','), ...lines].join('\n'); |
| 46 | + |
| 47 | + await writeFile( |
| 48 | + 'Pruned-Department-Ids-Not-In-CORD.csv', |
| 49 | + csvContent, |
| 50 | + 'utf-8', |
| 51 | + ); |
| 52 | + } |
| 53 | +} |
0 commit comments