Skip to content
Open
Show file tree
Hide file tree
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
41 changes: 41 additions & 0 deletions generics/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,46 @@ function getEndDate(date, timeZoneDifference) {
return date;
}

/**
* Normalizes string values to lowercase.
*
* @function
* @name normalizeToLower
* @param {string | string[]} input
* - A single string
* - A comma-separated string
* - An array of strings
*
* @returns {string | string[]}
* Returns the normalized value in the same data structure.
*/

function normalizeToLower(input) {
// Case 1: Array of strings
if (Array.isArray(input)) {
return input.map(value =>
typeof value === 'string' ? value.toLowerCase() : value
)
}

// Case 2: String
if (typeof input === 'string') {
// Check if it's a comma-separated string
if (input.includes(',')) {
return input
.split(',')
.map(value => value.trim().toLowerCase())
.join(',')
}

// Normal single string
return input.toLowerCase()
}

// Fallback: return input as-is
return input
}

module.exports = {
camelCaseToTitleCase : camelCaseToTitleCase,
lowerCase : lowerCase,
Expand All @@ -367,4 +407,5 @@ module.exports = {
checkIfStringIsNumber : checkIfStringIsNumber,
getStartDate: getStartDate,
getEndDate: getEndDate,
normalizeToLower: normalizeToLower
};
44 changes: 44 additions & 0 deletions migrations/normaliseUserRoles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## Execution Guide

Follow the steps below to run the script.

### 1. Navigate to the Script Directory

Open a terminal and move to the directory where the script is located.

```bash
cd <path-to-script-directory>
```

### 2. Ensure Environment Variables Are Configured

Make sure a `.env` file exists at the project root and contains the MongoDB connection string.

Example:

```
MONGODB_URL=<mongodb-url>
```

### 3. Execute the Script

Run the script using Node.js.

```bash
node <script-file-name>.js
```

### 4. Verify Execution

While running, the script will log:

* Database connection confirmation
* Number of roles processed
* Skipped roles (already lowercase or duplicates)
* Updated role codes

Once finished, the console will display:

```
Script completed successfully
```
72 changes: 72 additions & 0 deletions migrations/normaliseUserRoles/normaliseUserRoles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
let _ = require("lodash");
const path = require("path");
let rootPath = path.join(__dirname, "../../");

require("dotenv").config({ path: rootPath + "/.env" });

let MongoClient = require("mongodb").MongoClient;

let mongoUrl = process.env.MONGODB_URL;
let dbName = mongoUrl.split("/").pop();
let url = mongoUrl.split(dbName)[0];

const BATCH_SIZE = 20;

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

try {

console.log("Connected to DB:", dbName);

let roles = await db.collection("userRoles").find({}).toArray();

let batches = _.chunk(roles, BATCH_SIZE);

console.log("Total roles:", roles.length);
console.log("Total batches:", batches.length);

for (let batch of batches) {

for (let role of batch) {

let originalCode = role.code;
let lowerCode = originalCode.toLowerCase();

// skip if already lowercase
if (originalCode === lowerCode) {
console.log(`Skipping ${originalCode} (already lowercase)`);
continue;
}

// check if lowercase already exists
let exists = await db.collection("userRoles").findOne({
code: lowerCode
});

if (exists) {
console.log(
`Skipping ${originalCode} → ${lowerCode} (already exists)`
);
continue;
}

// update code
await db.collection("userRoles").updateOne(
{ _id: role._id },
{ $set: { code: lowerCode } }
);

console.log(`Updated ${originalCode} → ${lowerCode}`);
}
}

console.log("Script completed successfully");

} catch (error) {
console.error("Error:", error);
} finally {
connection.close();
}
})();
5 changes: 5 additions & 0 deletions module/user-roles/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ module.exports = class UserRolesHelper {
) {
return new Promise(async (resolve, reject) => {
try {

if(filterQuery.code){
filterQuery.code = gen.utils.normalizeToLower(filterQuery.code)
}

let queryObject = (filterQuery != "all") ? filterQuery : {};

let projection = {}
Expand Down