From cf7a5826b8ae02f5569b5aa9a44a38f00ec3495f Mon Sep 17 00:00:00 2001 From: tareq89 Date: Thu, 31 Oct 2024 17:08:31 +0600 Subject: [PATCH] refactored certificateTemplateId migration from code to aggregation --- ...d-certificateTemplateId-to-missing-docs.ts | 160 ++++++++++++------ 1 file changed, 105 insertions(+), 55 deletions(-) diff --git a/packages/migration/src/migrations/hearth/20241029171702-add-certificateTemplateId-to-missing-docs.ts b/packages/migration/src/migrations/hearth/20241029171702-add-certificateTemplateId-to-missing-docs.ts index e6afe82568f..5a85744f527 100644 --- a/packages/migration/src/migrations/hearth/20241029171702-add-certificateTemplateId-to-missing-docs.ts +++ b/packages/migration/src/migrations/hearth/20241029171702-add-certificateTemplateId-to-missing-docs.ts @@ -23,56 +23,90 @@ export const up = async (db: Db, client: MongoClient) => { const session = client.startSession() try { await session.withTransaction(async () => { - const collection = db.collection('DocumentReference') - const documents = await collection.find({}).toArray() - - for (const doc of documents) { - // Check if certificateTemplateId extension already exists - const hasCertificateTemplateId = doc.extension.some( - (ext) => - ext.url === - 'http://opencrvs.org/specs/extension/certificateTemplateId' - ) - - if (!hasCertificateTemplateId) { - // Determine the certificate type based on `code` - const certType = doc.type?.coding?.find((x) => - x.system.includes('certificate-type') - )?.code - - let certificateTemplateId: string | null = null - - switch (certType) { - case 'BIRTH': - certificateTemplateId = 'birth-certificate' - break - case 'DEATH': - certificateTemplateId = 'death-certificate' - break - case 'MARRIAGE': - certificateTemplateId = 'marriage-certificate' - break + const bulkOps = [ + { + $match: { + 'extension.url': { + $ne: 'http://opencrvs.org/specs/extension/certificateTemplateId' + } } - - if (certificateTemplateId) { - // Add the missing certificateTemplateId extension - await collection.updateOne( - { _id: doc._id }, - { - $push: { - extension: { - url: 'http://opencrvs.org/specs/extension/certificateTemplateId', - valueString: certificateTemplateId + }, + { + $set: { + certType: { + $arrayElemAt: [ + { + $filter: { + input: '$type.coding', + as: 'coding', + cond: { + $regexMatch: { + input: '$$coding.system', + regex: /certificate-type/ + } + } } - } + }, + 0 + ] + } + } + }, + { + $set: { + certificateTemplateId: { + $switch: { + branches: [ + { + case: { $eq: ['$certType.code', 'BIRTH'] }, + then: 'birth-certificate' + }, + { + case: { $eq: ['$certType.code', 'DEATH'] }, + then: 'death-certificate' + }, + { + case: { $eq: ['$certType.code', 'MARRIAGE'] }, + then: 'marriage-certificate' + } + ], + default: null } - ) - console.log( - `Updated DocumentReference document with _id: ${doc._id} for missing certificateTemplateId with the extension value ${certificateTemplateId}` - ) + } + } + }, + { + $match: { + certificateTemplateId: { $ne: null } + } + }, + { + $set: { + extension: { + $concatArrays: [ + '$extension', + [ + { + url: 'http://opencrvs.org/specs/extension/certificateTemplateId', + valueString: '$certificateTemplateId' + } + ] + ] + } + } + }, + { + $unset: ['certType', 'certificateTemplateId'] + }, + { + $merge: { + into: 'DocumentReference', + whenMatched: 'replace' } } - } + ] + + await db.collection('DocumentReference').aggregate(bulkOps).toArray() }) } catch (error) { console.error('Error occurred while updating document references:', error) @@ -86,25 +120,41 @@ export const down = async (db: Db, client: MongoClient) => { const session = client.startSession() try { await session.withTransaction(async () => { - const collection = db.collection('DocumentReference') - - // Remove the certificateTemplateId extension for each certificate type - await collection.updateMany( + const bulkOps = [ { - extension: { - $elemMatch: { - url: 'http://opencrvs.org/specs/extension/certificateTemplateId' + $match: { + extension: { + $elemMatch: { + url: 'http://opencrvs.org/specs/extension/certificateTemplateId' + } } } }, { - $pull: { + $set: { extension: { - url: 'http://opencrvs.org/specs/extension/certificateTemplateId' + $filter: { + input: '$extension', + as: 'ext', + cond: { + $ne: [ + '$$ext.url', + 'http://opencrvs.org/specs/extension/certificateTemplateId' + ] + } + } } } + }, + { + $merge: { + into: 'DocumentReference', + whenMatched: 'merge', + whenNotMatched: 'discard' + } } - ) + ] + await db.collection('DocumentReference').aggregate(bulkOps).toArray() console.log('Reverted certificateTemplateId extension from all documents') }) } catch (error) {