From 0edca3dfb08ed50b857125ca8474cb70d073fdd5 Mon Sep 17 00:00:00 2001 From: Vinit khandal <111434418+vinit717@users.noreply.github.com> Date: Sun, 8 Sep 2024 16:22:15 +0530 Subject: [PATCH] Fix Username Migration script to run on users with no first/last name (#2130) --- models/users.js | 16 +++++++++++----- routes/users.js | 2 +- test/integration/users.test.js | 26 +++----------------------- 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/models/users.js b/models/users.js index acf5c9382..3d14c7636 100644 --- a/models/users.js +++ b/models/users.js @@ -963,8 +963,10 @@ const updateUsersWithNewUsernames = async () => { const snapshot = await userModel.get(); const nonMemberUsers = snapshot.docs.filter((doc) => { - const roles = doc.data().roles; - return !(roles?.member === true || roles?.super_user === true); + const userData = doc.data(); + const roles = userData.roles; + + return !(roles?.member === true || roles?.super_user === true || userData.incompleteUserDetails === true); }); const summary = { @@ -985,10 +987,14 @@ const updateUsersWithNewUsernames = async () => { const userData = userDoc.data(); const id = userDoc.id; - const firstName = userData.first_name.split(" ")[0].toLowerCase(); - const lastName = userData.last_name.toLowerCase(); - const fullName = `${firstName}-${lastName}`; + const firstName = userData.first_name?.split(" ")[0]?.toLowerCase(); + const lastName = userData.last_name?.toLowerCase(); + if (!firstName || !lastName) { + return; + } + + const fullName = `${firstName}-${lastName}`; if (!nameToUsersMap.has(fullName)) { nameToUsersMap.set(fullName, []); } diff --git a/routes/users.js b/routes/users.js index 43b6dfd56..32c906c2d 100644 --- a/routes/users.js +++ b/routes/users.js @@ -67,5 +67,5 @@ router.patch("/rejectDiff", authenticate, authorizeRoles([SUPERUSER]), users.rej router.patch("/:userId", authenticate, authorizeRoles([SUPERUSER]), users.updateUser); router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers); module.exports = router; -router.post("/migration/update-usernames", authenticate, authorizeRoles([SUPERUSER]), users.updateUsernames); +router.post("/batch-username-update", authenticate, authorizeRoles([SUPERUSER]), users.updateUsernames); module.exports = router; diff --git a/test/integration/users.test.js b/test/integration/users.test.js index 93be6f94a..6e891e23e 100644 --- a/test/integration/users.test.js +++ b/test/integration/users.test.js @@ -2428,7 +2428,7 @@ describe("Users", function () { it("should run the migration and update usernames successfully", async function () { const res = await chai .request(app) - .post("/users/migration/update-usernames") + .post("/users/batch-username-update") .set("cookie", `${cookieName}=${superUserAuthToken}`) .send(); @@ -2438,7 +2438,7 @@ describe("Users", function () { it("should not update usernames for super_user or member", async function () { const res = await chai .request(app) - .post("/users/migration/update-usernames") + .post("/users/batch-username-update") .set("cookie", `${cookieName}=${superUserAuthToken}`) .send(); @@ -2450,31 +2450,11 @@ describe("Users", function () { it("should return 401 for unauthorized user attempting migration", async function () { const res = await chai .request(app) - .post("/users/migration/update-usernames") + .post("/users/batch-username-update") .set("cookie", `${cookieName}=${jwt}`) .send(); expect(res).to.have.status(401); }); - - it("should update username for users without member and super user true", async function () { - const userWithoutRoles = userData[2]; - const userId = await addUser(userWithoutRoles); - - const res = await chai - .request(app) - .post("/users/migration/update-usernames") - .set("cookie", `${cookieName}=${superUserAuthToken}`) - .send(); - - expect(res).to.have.status(200); - expect(res.body.totalUpdatedUsernames).to.be.greaterThan(0); - - const updatedUser = await firestore.collection("users").doc(userId).get(); - - expect(updatedUser.data().username).to.include( - `${userWithoutRoles.first_name.toLowerCase()}-${userWithoutRoles.last_name.toLowerCase()}-1` - ); - }); }); });