|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +import { node } from 'cypher-query-builder'; |
| 4 | +import { readFile } from 'node:fs/promises'; |
| 5 | +import { BaseMigration, Migration } from '~/core/database'; |
| 6 | + |
| 7 | +interface ExternalDepartmentId { |
| 8 | + id: string; |
| 9 | + name?: string; |
| 10 | +} |
| 11 | + |
| 12 | +@Migration('2025-09-17T09:00:00') |
| 13 | +export class CreateUsedDeptIdListMigration extends BaseMigration { |
| 14 | + async up() { |
| 15 | + const intaactFilePath = new URL( |
| 16 | + '../../../../../.vscode/localFiles/All-Department-Ids-From-Intaact.csv', |
| 17 | + import.meta.url, |
| 18 | + ); |
| 19 | + const cordFilePath = new URL( |
| 20 | + '../../../../../.vscode/localFiles/prod_deptIds_ids_only.csv', |
| 21 | + import.meta.url, |
| 22 | + ); |
| 23 | + |
| 24 | + const intaactFileContent = await readFile(intaactFilePath, 'utf-8'); |
| 25 | + const cordFileContent = await readFile(cordFilePath, 'utf-8'); |
| 26 | + |
| 27 | + const intaactRows = intaactFileContent.trim().split(/\r?\n/).slice(1); // Skip header |
| 28 | + const cordRows = cordFileContent.trim().split(/\r?\n/).slice(1); // Skip header |
| 29 | + |
| 30 | + const intaactList: ExternalDepartmentId[] = intaactRows.flatMap((row) => { |
| 31 | + const [id, name] = row.split(','); |
| 32 | + if (id) { |
| 33 | + return { id, name }; |
| 34 | + } |
| 35 | + return []; |
| 36 | + }); |
| 37 | + |
| 38 | + const prunedIntaactList = intaactList.flatMap((row) => |
| 39 | + !cordRows.includes(row.id) ? row : [], |
| 40 | + ); |
| 41 | + |
| 42 | + await this.db |
| 43 | + .query() |
| 44 | + .unwind(prunedIntaactList, 'dept') |
| 45 | + .create( |
| 46 | + node('blacklist', 'BlacklistDepartmentId', { |
| 47 | + id: 'apoc.create.uuid()', |
| 48 | + departmentId: 'dept.id', |
| 49 | + departmentName: 'dept.name', |
| 50 | + createdAt: 'datetime()', |
| 51 | + createdBy: 'bulk_import', |
| 52 | + }), |
| 53 | + ) |
| 54 | + .return('count(blacklist) as created') |
| 55 | + .executeAndLogStats(); |
| 56 | + } |
| 57 | +} |
0 commit comments