Skip to content

Commit

Permalink
migration script for adding certificateTemplateId extension on missin…
Browse files Browse the repository at this point in the history
…g Documentrefences
  • Loading branch information
tareq89 committed Oct 29, 2024
1 parent deb153a commit e21b0ea
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 4 deletions.
106 changes: 102 additions & 4 deletions packages/client/src/utils/gateway-deprecated-do-not-use.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,7 @@ export interface GQLRoleInput {
export interface GQLBirth {
REGISTRATION_TARGET?: number
LATE_REGISTRATION_TARGET?: number
FEE?: GQLBirthFee
PRINT_IN_ADVANCE?: boolean
}

Expand All @@ -1341,11 +1342,13 @@ export interface GQLCurrency {

export interface GQLDeath {
REGISTRATION_TARGET?: number
FEE?: GQLDeathFee
PRINT_IN_ADVANCE?: boolean
}

export interface GQLMarriage {
REGISTRATION_TARGET?: number
FEE?: GQLMarriageFee
PRINT_IN_ADVANCE?: boolean
}

Expand All @@ -1358,6 +1361,7 @@ export interface GQLLoginBackground {
export interface GQLBirthInput {
REGISTRATION_TARGET?: number
LATE_REGISTRATION_TARGET?: number
FEE?: GQLBirthFeeInput
PRINT_IN_ADVANCE?: boolean
}

Expand All @@ -1373,11 +1377,13 @@ export interface GQLCurrencyInput {

export interface GQLDeathInput {
REGISTRATION_TARGET?: number
FEE?: GQLDeathFeeInput
PRINT_IN_ADVANCE?: boolean
}

export interface GQLMarriageInput {
REGISTRATION_TARGET?: number
FEE?: GQLMarriageFeeInput
PRINT_IN_ADVANCE?: boolean
}

Expand Down Expand Up @@ -1781,11 +1787,43 @@ export interface GQLLabelInput {
label: string
}

export interface GQLBirthFee {
ON_TIME?: number
LATE?: number
DELAYED?: number
}

export interface GQLDeathFee {
ON_TIME?: number
DELAYED?: number
}

export interface GQLMarriageFee {
ON_TIME?: number
DELAYED?: number
}

export const enum GQLImageFit {
FILL = 'FILL',
TILE = 'TILE'
}

export interface GQLBirthFeeInput {
ON_TIME?: number
LATE?: number
DELAYED?: number
}

export interface GQLDeathFeeInput {
ON_TIME?: number
DELAYED?: number
}

export interface GQLMarriageFeeInput {
ON_TIME?: number
DELAYED?: number
}

export interface GQLAuditLogItemBase {
time: string
ipAddress: string
Expand Down Expand Up @@ -1988,6 +2026,9 @@ export interface GQLResolver {
EventProgressData?: GQLEventProgressDataTypeResolver
WebhookPermission?: GQLWebhookPermissionTypeResolver
OIDPUserAddress?: GQLOIDPUserAddressTypeResolver
BirthFee?: GQLBirthFeeTypeResolver
DeathFee?: GQLDeathFeeTypeResolver
MarriageFee?: GQLMarriageFeeTypeResolver
AuditLogItemBase?: {
__resolveType: GQLAuditLogItemBaseTypeResolver
}
Expand Down Expand Up @@ -6693,6 +6734,7 @@ export interface OIDPUserInfoToUpdated_atResolver<
export interface GQLBirthTypeResolver<TParent = any> {
REGISTRATION_TARGET?: BirthToREGISTRATION_TARGETResolver<TParent>
LATE_REGISTRATION_TARGET?: BirthToLATE_REGISTRATION_TARGETResolver<TParent>
FEE?: BirthToFEEResolver<TParent>
PRINT_IN_ADVANCE?: BirthToPRINT_IN_ADVANCEResolver<TParent>
}

Expand All @@ -6710,6 +6752,10 @@ export interface BirthToLATE_REGISTRATION_TARGETResolver<
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface BirthToFEEResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface BirthToPRINT_IN_ADVANCEResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}
Expand Down Expand Up @@ -6745,6 +6791,7 @@ export interface CurrencyToLanguagesAndCountryResolver<

export interface GQLDeathTypeResolver<TParent = any> {
REGISTRATION_TARGET?: DeathToREGISTRATION_TARGETResolver<TParent>
FEE?: DeathToFEEResolver<TParent>
PRINT_IN_ADVANCE?: DeathToPRINT_IN_ADVANCEResolver<TParent>
}

Expand All @@ -6755,12 +6802,17 @@ export interface DeathToREGISTRATION_TARGETResolver<
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface DeathToFEEResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface DeathToPRINT_IN_ADVANCEResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface GQLMarriageTypeResolver<TParent = any> {
REGISTRATION_TARGET?: MarriageToREGISTRATION_TARGETResolver<TParent>
FEE?: MarriageToFEEResolver<TParent>
PRINT_IN_ADVANCE?: MarriageToPRINT_IN_ADVANCEResolver<TParent>
}

Expand All @@ -6771,6 +6823,10 @@ export interface MarriageToREGISTRATION_TARGETResolver<
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface MarriageToFEEResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface MarriageToPRINT_IN_ADVANCEResolver<
TParent = any,
TResult = any
Expand Down Expand Up @@ -6897,6 +6953,7 @@ export interface GQLCertificateTypeResolver<TParent = any> {
collector?: CertificateToCollectorResolver<TParent>
hasShowedVerifiedDocument?: CertificateToHasShowedVerifiedDocumentResolver<TParent>
payments?: CertificateToPaymentsResolver<TParent>
data?: CertificateToDataResolver<TParent>
}

export interface CertificateToCollectorResolver<TParent = any, TResult = any> {
Expand All @@ -6914,10 +6971,7 @@ export interface CertificateToPaymentsResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface CertificateToTemplateConfigResolver<
TParent = any,
TResult = any
> {
export interface CertificateToDataResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

Expand Down Expand Up @@ -8326,6 +8380,50 @@ export interface OIDPUserAddressToCountryResolver<
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface GQLBirthFeeTypeResolver<TParent = any> {
ON_TIME?: BirthFeeToON_TIMEResolver<TParent>
LATE?: BirthFeeToLATEResolver<TParent>
DELAYED?: BirthFeeToDELAYEDResolver<TParent>
}

export interface BirthFeeToON_TIMEResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface BirthFeeToLATEResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface BirthFeeToDELAYEDResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface GQLDeathFeeTypeResolver<TParent = any> {
ON_TIME?: DeathFeeToON_TIMEResolver<TParent>
DELAYED?: DeathFeeToDELAYEDResolver<TParent>
}

export interface DeathFeeToON_TIMEResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface DeathFeeToDELAYEDResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface GQLMarriageFeeTypeResolver<TParent = any> {
ON_TIME?: MarriageFeeToON_TIMEResolver<TParent>
DELAYED?: MarriageFeeToDELAYEDResolver<TParent>
}

export interface MarriageFeeToON_TIMEResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface MarriageFeeToDELAYEDResolver<TParent = any, TResult = any> {
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult
}

export interface GQLAuditLogItemBaseTypeResolver<TParent = any> {
(parent: TParent, context: any, info: GraphQLResolveInfo):
| 'UserAuditLogItemWithComposition'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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'

// Define an interface for your DocumentReference
interface DocumentReference {
_id: string
extension: Array<{ url: string; valueString?: string }>
type?: {
coding?: Array<{ system: string; code: string }>
}
}

export const up = async (db: Db, client: MongoClient) => {
const session = client.startSession()
try {
await session.withTransaction(async () => {
const collection = db.collection<DocumentReference>('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
}

if (certificateTemplateId) {
// Add the missing certificateTemplateId extension
await collection.updateOne(
{ _id: doc._id },
{
$push: {
extension: {
url: 'http://opencrvs.org/specs/extension/certificateTemplateId',
valueString: certificateTemplateId
}
}
}
)
console.log(
`Updated DocumentReference document with _id: ${doc._id} for missing certificateTemplateId with the extension value ${certificateTemplateId}`
)
}
}
}
})
} catch (error) {
console.error('Error occurred while updating document references:', error)
throw error
} finally {
session.endSession()
}
}

export const down = async (db: Db, client: MongoClient) => {
const session = client.startSession()
try {
await session.withTransaction(async () => {
const collection = db.collection<DocumentReference>('DocumentReference')

// Remove the certificateTemplateId extension for each certificate type
await collection.updateMany(
{
extension: {
$elemMatch: {
url: 'http://opencrvs.org/specs/extension/certificateTemplateId'
}
}
},
{
$pull: {
extension: {
url: 'http://opencrvs.org/specs/extension/certificateTemplateId'
}
}
}
)
console.log('Reverted certificateTemplateId extension from all documents')
})
} catch (error) {
console.error('Error occurred while reverting document references:', error)
throw error
} finally {
session.endSession()
}
}

0 comments on commit e21b0ea

Please sign in to comment.