Skip to content

Commit

Permalink
chore!: amedn migration to update role in related person entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil20 committed Sep 23, 2024
1 parent 43ad15c commit 2134d4a
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@

import { Db, MongoClient } from 'mongodb'

const USER_MGNT_MONGO_URL =
process.env.USER_MGNT_MONGO_URL || 'mongodb://localhost/user-mgnt'

export const up = async (db: Db, client: MongoClient) => {
const userManagementClient = new MongoClient(USER_MGNT_MONGO_URL)
const userMgntDb = (await userManagementClient.connect()).db()

const documents = await db
.collection('PractitionerRole')
.find({ 'code.coding.system': 'http://opencrvs.org/specs/types' })
Expand All @@ -29,6 +35,27 @@ export const up = async (db: Db, client: MongoClient) => {
.updateOne({ _id: doc._id }, { $set: { code: filteredCode } })
}
console.log('Documents updated.')

const relatedPersons = await db.collection('RelatedPerson').find().toArray()

for (const person of relatedPersons) {
const relationship = person.relationship.coding[0].code
if (relationship === 'PRINT_IN_ADVANCE') {
const userRole = person.relationship.text
const formattedUserRole = userRole.toUpperCase().replace(' ', '_')

const userRoleId = userMgntDb.collection('users').find({
role: formattedUserRole
})

await db
.collection('PractitionerRole')
.updateOne(
{ _id: person._id },
{ $set: { 'resource.relationship.text': userRoleId } }
)
}
}
}

export const down = async (db: Db, client: MongoClient) => {}

0 comments on commit 2134d4a

Please sign in to comment.