Skip to content
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
55 changes: 55 additions & 0 deletions src/migrations/normalizeOrgIdInCollections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* name : correctOrgIdValuesInCollections.js
* description : Normalize orgId/orgIds fields in given collections
* author : vishnu
*/

require('dotenv').config({ path: '../.env' })
const MongoClient = require('mongodb').MongoClient
const _ = require('lodash')

const mongoUrl = process.env.MONGODB_URL
const dbName = mongoUrl.split('/').pop()
const url = mongoUrl.split(dbName)[0]

// Collections with "orgId" (string) field
const singleOrgIdCollections = ['entities', 'entityTypes', 'userRoleExtension']

// Normalize function
function normalizeOrgId(orgId) {
return orgId.trim().toLowerCase().replace(/\s+/g, '_')
}

;(async () => {
const connection = await MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
const db = connection.db(dbName)

try {
// Process single orgId collections
for (const collectionName of singleOrgIdCollections) {
console.log(`\nProcessing collection: ${collectionName}`)
const orgIds = await db.collection(collectionName).distinct('orgId')
console.log('orgIds:', orgIds)

for (const originalOrgId of orgIds) {
if (typeof originalOrgId !== 'string' || originalOrgId.toUpperCase() === 'ALL') continue

const normalizedOrgId = normalizeOrgId(originalOrgId)
if (normalizedOrgId !== originalOrgId) {
const result = await db
.collection(collectionName)
.updateMany({ orgId: originalOrgId }, { $set: { orgId: normalizedOrgId } })
console.log(
`Updated ${result.modifiedCount} documents in ${collectionName} from '${originalOrgId}' to '${normalizedOrgId}'`
)
}
}
}

console.log('\nNormalization completed!')
connection.close()
} catch (error) {
console.error('Error occurred:', error)
connection.close()
}
})()