|
| 1 | +import { node } from 'cypher-query-builder'; |
| 2 | +import { readFile } from 'node:fs/promises'; |
| 3 | +import { BaseMigration, Migration } from '~/core/database'; |
| 4 | +import { apoc, variable } from '~/core/database/query'; |
| 5 | + |
| 6 | +interface ExternalDepartmentId { |
| 7 | + id: 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 | + '../../../../../.vscode/localFiles/All-Department-Ids-From-Intaact.csv', |
| 16 | + import.meta.url, |
| 17 | + ); |
| 18 | + const cordFilePath = new URL( |
| 19 | + '../../../../../.vscode/localFiles/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 [id, name] = row.split(','); |
| 31 | + if (id) { |
| 32 | + return { id, name }; |
| 33 | + } |
| 34 | + return []; |
| 35 | + }); |
| 36 | + |
| 37 | + const prunedIntaactList = intaactList.flatMap((row) => |
| 38 | + !cordRows.includes(row.id) ? row : [], |
| 39 | + ); |
| 40 | + |
| 41 | + await this.db |
| 42 | + .query() |
| 43 | + .unwind(prunedIntaactList, 'dept') |
| 44 | + .create(node('blacklist', 'BlacklistDepartmentId')) |
| 45 | + .setValues({ |
| 46 | + 'blacklist.id': variable(apoc.create.uuid()), |
| 47 | + 'blacklist.departmentId': variable('dept.id'), |
| 48 | + 'blacklist.departmentName': variable('dept.name'), |
| 49 | + 'blacklist.createdAt': Date.now(), |
| 50 | + 'blacklist.createdBy': 'bulk_import', |
| 51 | + }) |
| 52 | + .return('count(blacklist) as created') |
| 53 | + .executeAndLogStats(); |
| 54 | + } |
| 55 | +} |
0 commit comments