Skip to content

Commit

Permalink
Updated /users/:userId PATCH Endpoint (#2316)
Browse files Browse the repository at this point in the history
* updated userId route for self patch request based on userid

* removed console line

* added feature flag

* return res fix
  • Loading branch information
vikasosmium authored Dec 27, 2024
1 parent 7269b1f commit 439658f
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 15 deletions.
28 changes: 28 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const config = require("config");
const { generateUniqueUsername } = require("../services/users");
const userService = require("../services/users");
const discordDeveloperRoleId = config.get("discordDeveloperRoleId");
const usersCollection = firestore.collection("users");

const verifyUser = async (req, res) => {
const userId = req.userData.id;
Expand Down Expand Up @@ -714,6 +715,12 @@ const updateUser = async (req, res) => {
const { id: profileDiffId, message } = req.body;
const devFeatureFlag = req.query.dev === "true";
let profileDiffData;

const userDoc = await usersCollection.doc(req.params.userId).get();
if (!userDoc.exists) {
return res.boom.notFound("The User doesn't exist.");
}

if (devFeatureFlag) {
profileDiffData = await profileDiffsQuery.fetchProfileDiffUnobfuscated(profileDiffId);
} else {
Expand Down Expand Up @@ -1096,6 +1103,26 @@ const updateUsernames = async (req, res) => {
}
};

const updateProfile = async (req, res) => {
try {
const { id: currentUserId, roles = {} } = req.userData;
const isSelf = req.params.userId === currentUserId;
const isSuperUser = roles[ROLES.SUPERUSER];
const profile = req.query.profile === "true";

if (isSelf && profile && req.query.dev === "true") {
return await updateSelf(req, res);
} else if (isSuperUser) {
return await updateUser(req, res);
}

return res.boom.badRequest("Invalid Request.");
} catch (err) {
logger.error(`Error in updateUserStatusController: ${err}`);
return res.boom.badImplementation("An unexpected error occurred.");
}
};

module.exports = {
verifyUser,
generateChaincode,
Expand Down Expand Up @@ -1128,4 +1155,5 @@ module.exports = {
isDeveloper,
getIdentityStats,
updateUsernames,
updateProfile,
};
10 changes: 10 additions & 0 deletions middlewares/conditionalMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const conditionalMiddleware = (validator) => {
return async (req, res, next) => {
if (req.params.userId === req.userData.id && req.query.profile === "true") {
return validator(req, res, next);
}
next();
};
};

module.exports = conditionalMiddleware;
5 changes: 3 additions & 2 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ const { Services } = require("../constants/bot");
const authenticateProfile = require("../middlewares/authenticateProfile");
const { devFlagMiddleware } = require("../middlewares/devFlag");
const { userAuthorization } = require("../middlewares/userAuthorization");
const conditionalMiddleware = require("../middlewares/conditionalMiddleware");

router.post("/", authorizeAndAuthenticate([ROLES.SUPERUSER], [Services.CRON_JOB_HANDLER]), users.markUnverified);
router.post("/update-in-discord", authenticate, authorizeRoles([SUPERUSER]), users.setInDiscordScript);
router.post("/verify", authenticate, users.verifyUser);
router.get("/userId/:userId", users.getUserById);
router.patch("/self", authenticate, userValidator.updateUser, users.updateSelf);
router.patch("/self", authenticate, userValidator.updateUser, users.updateSelf); // this route is being deprecated soon, please use alternate available `/users/:userId?profile=true` PATCH endpoint.
router.get("/", authenticateProfile(authenticate), userValidator.getUsers, users.getUsers);
router.get("/self", authenticate, users.getSelfDetails);
router.get("/isDeveloper", authenticate, users.isDeveloper);
Expand Down Expand Up @@ -75,7 +76,7 @@ router.patch(
router.get("/picture/:id", authenticate, authorizeRoles([SUPERUSER]), users.getUserImageForVerification);
router.patch("/profileURL", authenticate, userValidator.updateProfileURL, users.profileURL);
router.patch("/rejectDiff", authenticate, authorizeRoles([SUPERUSER]), users.rejectProfileDiff);
router.patch("/:userId", authenticate, authorizeRoles([SUPERUSER]), users.updateUser);
router.patch("/:userId", authenticate, conditionalMiddleware(userValidator.updateUser), users.updateProfile);
router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers);
module.exports = router;
router.post("/batch-username-update", authenticate, authorizeRoles([SUPERUSER]), users.updateUsernames);
Expand Down
Loading

0 comments on commit 439658f

Please sign in to comment.