import type * as plugins from '../ts/plugins.js'; import { JOURNAL_ENTRY_COLLECTION, JOURNAL_SEQUENCE_INDEX_NAME, } from '../ts/skr.classes.journalentry.js'; import { OPOS_META_COLLECTION } from '../ts/skr.opos.js'; import { OposMetaDuplicatesError, type IOposMetaDuplicate } from '../ts/skr.errors.js'; import type { ISkrMigrationStep } from './helpers.migration.js'; import type { IStorageTopologyReport } from './classes.migrationrecord.js'; /** The text index SmartData 7 created over `@searchable()` fields; skr searches in memory. */ const LEGACY_TEXT_INDEX_NAME = 'smartdata_text_index'; const LEGACY_TEXT_INDEX_COLLECTIONS = ['Account', 'Transaction', JOURNAL_ENTRY_COLLECTION]; const collectionExists = async ( db: plugins.nosqldb.SmartdataDb, name: string, ): Promise => (await db.mongoDb.listCollections({ name }, { nameOnly: true }).toArray()).length > 0; /** The keys held by more than one dunning record. Pure — the records come from the caller. */ export const findOposMetaDuplicates = ( records: ReadonlyArray<{ skrType: string; accountNumber: string; reference: string }>, ): IOposMetaDuplicate[] => { const counts = new Map(); for (const record of records) { const key = JSON.stringify([record.skrType, record.accountNumber, record.reference]); const entry = counts.get(key) ?? { skrType: record.skrType, accountNumber: record.accountNumber, reference: record.reference, count: 0, }; entry.count += 1; counts.set(key, entry); } return [...counts.values()].filter((entry) => entry.count > 1); }; /** * Refuses when one open item holds more than one dunning record: the unique * index OposMeta declares cannot be created over them, and skr never picks or * deletes financial metadata on its own. */ const assertOposMetaUnique = async (db: plugins.nosqldb.SmartdataDb): Promise => { if (!(await collectionExists(db, OPOS_META_COLLECTION))) { return; } const records = await db.mongoDb .collection<{ skrType: string; accountNumber: string; reference: string }>(OPOS_META_COLLECTION) .find({}, { projection: { _id: 0, skrType: 1, accountNumber: 1, reference: 1 } }) .toArray(); const duplicates = findOposMetaDuplicates(records); if (duplicates.length > 0) { throw new OposMetaDuplicatesError(duplicates); } }; /** * Brings the collection topology to what the `@lossless.org/client` models * expect: SmartData 7's text indexes go (skr declares no searchable field), * the journal's partial unique sequence index — which a model cannot declare — * is created here, and OposMeta is checked for duplicates before its model * creates the unique index it declares. */ export const storageTopologyStep: ISkrMigrationStep = { id: 'storage.2-lossless-client-topology', description: 'Drop the SmartData 7 text indexes, create the partial unique sequence index of the journal, ' + 'and check OposMeta for duplicate dunning records', run: async (db, { signal }): Promise => { const textIndexesDropped: string[] = []; for (const name of LEGACY_TEXT_INDEX_COLLECTIONS) { if (!(await collectionExists(db, name))) continue; const collection = db.mongoDb.collection(name); const indexes = await collection.indexes(); if (indexes.some((index) => index.name === LEGACY_TEXT_INDEX_NAME)) { signal.throwIfAborted(); await collection.dropIndex(LEGACY_TEXT_INDEX_NAME); textIndexesDropped.push(name); } } // only chained entries: legacy entries carry no sequence number until the // ledger unification appends them to the chain signal.throwIfAborted(); await db.mongoDb.collection(JOURNAL_ENTRY_COLLECTION).createIndex( { skrType: 1, fiscalYear: 1, sequenceNumber: 1 }, { name: JOURNAL_SEQUENCE_INDEX_NAME, unique: true, partialFilterExpression: { schemaVersion: 2, sequenceNumber: { $exists: true } }, }, ); await assertOposMetaUnique(db); return { kind: 'storage-topology', textIndexesDropped, sequenceIndexEnsured: true }; }, };