-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7637 from opencrvs/auto-pr-release-v1.6.0-7632-10317
🍒 Merge changes from PR #7632 to release-v1.6.0
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
.../migrations/hearth/20240919155854-change-practitioner-family-name-from-array-to-string.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } }, [ | ||
{ | ||
$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) => {} |