Skip to content

Commit 6bde658

Browse files
authored
Fix migration of PractitionerRole and history to match new role names (#9991)
* Modified and added migration to set new role name for role and history Nobody has gone live with v1.7 yet so the changes to the migration will still work on all live installations * Update changelog
1 parent 5ea8dae commit 6bde658

File tree

4 files changed

+112
-6
lines changed

4 files changed

+112
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [1.7.4](https://github.com/opencrvs/opencrvs-core/compare/v1.7.3...v1.7.4)
4+
5+
### Bug fixes
6+
7+
- Fixed historical roles displaying incorrectly in task history after migration to v1.7 [#9989](https://github.com/opencrvs/opencrvs-core/issues/9989)
8+
39
## [1.7.3](https://github.com/opencrvs/opencrvs-core/compare/v1.7.2...v1.7.3)
410

511
### New features
@@ -10,7 +16,6 @@
1016
- Fix international to local number conversion from failing if the number was already local [#9634](https://github.com/opencrvs/opencrvs-core/issues/9634)
1117
- Pre-select default certificate option in print certificate collector form [#9935](https://github.com/opencrvs/opencrvs-core/issues/9935)
1218

13-
1419
## [1.7.2](https://github.com/opencrvs/opencrvs-core/compare/v1.7.1...v1.7.2)
1520

1621
### New features

packages/migration/src/migrations/hearth/20240812142943-update-practitioner-role-collection-for-new-userroles.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
1010
*/
1111

12+
import { transformRoleCodes } from '../../utils/role-helper.js'
1213
import { Db, MongoClient } from 'mongodb'
1314

1415
export const up = async (db: Db, client: MongoClient) => {
@@ -18,11 +19,12 @@ export const up = async (db: Db, client: MongoClient) => {
1819
.toArray()
1920

2021
for (const doc of documents) {
21-
const filteredCode = doc.code.filter((c: any) =>
22-
c.coding.find(
23-
(cod: any) => cod.system !== 'http://opencrvs.org/specs/types'
24-
)
25-
)
22+
if (!doc.code) {
23+
console.warn(`Document ${doc._id}: No code field, skipping`)
24+
continue
25+
}
26+
27+
const filteredCode = transformRoleCodes(doc)
2628

2729
await db
2830
.collection('PractitionerRole')
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* OpenCRVS is also distributed under the terms of the Civil Registration
7+
* & Healthcare Disclaimer located at http://opencrvs.org/license.
8+
*
9+
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
10+
*/
11+
12+
import { transformRoleCodes } from '../../utils/role-helper.js'
13+
import { Db, MongoClient } from 'mongodb'
14+
15+
export const up = async (db: Db, client: MongoClient) => {
16+
const documents = await db
17+
.collection('PractitionerRole_history')
18+
.find({ 'code.coding.system': 'http://opencrvs.org/specs/types' })
19+
.toArray()
20+
21+
for (const doc of documents) {
22+
if (!doc.code) {
23+
console.warn(`Document ${doc._id}: No code field, skipping`)
24+
continue
25+
}
26+
27+
const filteredCode = transformRoleCodes(doc)
28+
29+
await db
30+
.collection('PractitionerRole_history')
31+
.updateOne({ _id: doc._id }, { $set: { code: filteredCode } })
32+
}
33+
console.log('Documents updated.')
34+
}
35+
36+
export const down = async (db: Db, client: MongoClient) => {}

packages/migration/src/utils/role-helper.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,66 @@ export const updatePractitionerRoleCodeAggregate = [
145145
}
146146
}
147147
]
148+
149+
/**
150+
* Transform role code based on the english label to match how the scripts will generate new roles from aliases
151+
*/
152+
export const transformRoleCodes = (doc: any) => {
153+
const updatedCode = [...doc.code]
154+
155+
// Find the types system code and extract the English label
156+
const typesCodeEntry = doc.code.find((c: any) =>
157+
c?.coding?.find((cod: any) => cod?.system === typeSystemUrl)
158+
)
159+
160+
const typesCoding = typesCodeEntry?.coding?.find(
161+
(cod: any) => cod?.system === typeSystemUrl
162+
)
163+
164+
if (typesCoding?.code) {
165+
try {
166+
const labelArray = JSON.parse(typesCoding.code)
167+
168+
const englishLabel = labelArray?.find(
169+
(item: any) => item?.lang === 'en' && item?.label
170+
)
171+
172+
if (englishLabel?.label) {
173+
const transformedLabel = englishLabel.label
174+
.toUpperCase()
175+
.replace(/ /g, '_')
176+
177+
// Find and update the roles system code
178+
const rolesCodeIndex = updatedCode.findIndex((c: any) =>
179+
c?.coding?.find((cod: any) => cod?.system === roleSystemUrl)
180+
)
181+
182+
if (rolesCodeIndex !== -1) {
183+
const rolesCodingIndex = updatedCode[rolesCodeIndex].coding.findIndex(
184+
(cod: any) => cod?.system === roleSystemUrl
185+
)
186+
187+
if (rolesCodingIndex !== -1) {
188+
updatedCode[rolesCodeIndex].coding[rolesCodingIndex].code =
189+
transformedLabel
190+
}
191+
}
192+
} else {
193+
console.warn(
194+
`Document ${doc._id}: No valid English label found in types code`
195+
)
196+
}
197+
} catch (parseError) {
198+
console.warn(
199+
`Document ${doc._id}: Could not parse types code as JSON, skipping role update:`,
200+
parseError
201+
)
202+
}
203+
}
204+
205+
// Remove the types system from the code array
206+
const filteredCode = updatedCode.filter((c: any) =>
207+
c?.coding?.find((cod: any) => cod?.system !== typeSystemUrl)
208+
)
209+
return filteredCode
210+
}

0 commit comments

Comments
 (0)