Skip to content

Commit 0edca3d

Browse files
authored
Fix Username Migration script to run on users with no first/last name (#2130)
1 parent 82a78d9 commit 0edca3d

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

models/users.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,10 @@ const updateUsersWithNewUsernames = async () => {
963963
const snapshot = await userModel.get();
964964

965965
const nonMemberUsers = snapshot.docs.filter((doc) => {
966-
const roles = doc.data().roles;
967-
return !(roles?.member === true || roles?.super_user === true);
966+
const userData = doc.data();
967+
const roles = userData.roles;
968+
969+
return !(roles?.member === true || roles?.super_user === true || userData.incompleteUserDetails === true);
968970
});
969971

970972
const summary = {
@@ -985,10 +987,14 @@ const updateUsersWithNewUsernames = async () => {
985987
const userData = userDoc.data();
986988
const id = userDoc.id;
987989

988-
const firstName = userData.first_name.split(" ")[0].toLowerCase();
989-
const lastName = userData.last_name.toLowerCase();
990-
const fullName = `${firstName}-${lastName}`;
990+
const firstName = userData.first_name?.split(" ")[0]?.toLowerCase();
991+
const lastName = userData.last_name?.toLowerCase();
991992

993+
if (!firstName || !lastName) {
994+
return;
995+
}
996+
997+
const fullName = `${firstName}-${lastName}`;
992998
if (!nameToUsersMap.has(fullName)) {
993999
nameToUsersMap.set(fullName, []);
9941000
}

routes/users.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ router.patch("/rejectDiff", authenticate, authorizeRoles([SUPERUSER]), users.rej
6767
router.patch("/:userId", authenticate, authorizeRoles([SUPERUSER]), users.updateUser);
6868
router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers);
6969
module.exports = router;
70-
router.post("/migration/update-usernames", authenticate, authorizeRoles([SUPERUSER]), users.updateUsernames);
70+
router.post("/batch-username-update", authenticate, authorizeRoles([SUPERUSER]), users.updateUsernames);
7171
module.exports = router;

test/integration/users.test.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,7 +2428,7 @@ describe("Users", function () {
24282428
it("should run the migration and update usernames successfully", async function () {
24292429
const res = await chai
24302430
.request(app)
2431-
.post("/users/migration/update-usernames")
2431+
.post("/users/batch-username-update")
24322432
.set("cookie", `${cookieName}=${superUserAuthToken}`)
24332433
.send();
24342434

@@ -2438,7 +2438,7 @@ describe("Users", function () {
24382438
it("should not update usernames for super_user or member", async function () {
24392439
const res = await chai
24402440
.request(app)
2441-
.post("/users/migration/update-usernames")
2441+
.post("/users/batch-username-update")
24422442
.set("cookie", `${cookieName}=${superUserAuthToken}`)
24432443
.send();
24442444

@@ -2450,31 +2450,11 @@ describe("Users", function () {
24502450
it("should return 401 for unauthorized user attempting migration", async function () {
24512451
const res = await chai
24522452
.request(app)
2453-
.post("/users/migration/update-usernames")
2453+
.post("/users/batch-username-update")
24542454
.set("cookie", `${cookieName}=${jwt}`)
24552455
.send();
24562456

24572457
expect(res).to.have.status(401);
24582458
});
2459-
2460-
it("should update username for users without member and super user true", async function () {
2461-
const userWithoutRoles = userData[2];
2462-
const userId = await addUser(userWithoutRoles);
2463-
2464-
const res = await chai
2465-
.request(app)
2466-
.post("/users/migration/update-usernames")
2467-
.set("cookie", `${cookieName}=${superUserAuthToken}`)
2468-
.send();
2469-
2470-
expect(res).to.have.status(200);
2471-
expect(res.body.totalUpdatedUsernames).to.be.greaterThan(0);
2472-
2473-
const updatedUser = await firestore.collection("users").doc(userId).get();
2474-
2475-
expect(updatedUser.data().username).to.include(
2476-
`${userWithoutRoles.first_name.toLowerCase()}-${userWithoutRoles.last_name.toLowerCase()}-1`
2477-
);
2478-
});
24792459
});
24802460
});

0 commit comments

Comments
 (0)