Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(practitioner): migrate Practitioner.name.family to unified type #7632

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* OpenCRVS is also distributed under the terms of the Civil Registration
* & Healthcare Disclaimer located at http://opencrvs.org/license.
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/

import { Db, MongoClient } from 'mongodb'

export const up = async (db: Db, client: MongoClient) => {
// In most cases the family name is a string, but in some cases it is an array (1.2.x versions at least)
const session = client.startSession()
try {
await session.withTransaction(async () => {
await db
.collection('Practitioner')
.updateMany({ 'name.family': { $type: 'array' } }, [
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is a simpler way of doing it, I will appreciate tips.

{
$set: {
name: {
$map: {
input: '$name',
as: 'n',
in: {
$mergeObjects: [
'$$n',
{
family: {
$cond: {
if: { $isArray: '$$n.family' },
then: { $arrayElemAt: ['$$n.family', 0] },
else: '$$n.family'
}
}
}
]
}
}
}
}
}
])
})
} catch (err) {
console.log('Error occurred while updating practitioner family name', err)
} finally {
await session.endSession()
}
}

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