Skip to content

Commit

Permalink
Merge branch 'develop' into include-userData-in-progress-response
Browse files Browse the repository at this point in the history
  • Loading branch information
Achintya-Chatterjee authored Dec 28, 2024
2 parents e5fb0a2 + cb683fb commit 4699602
Show file tree
Hide file tree
Showing 12 changed files with 987 additions and 23 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;
22 changes: 21 additions & 1 deletion routes/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const { cacheResponse, invalidateCache } = require("../utils/cache");
const { ALL_TASKS } = require("../constants/cacheKeys");
const { verifyCronJob } = require("../middlewares/authorizeBot");
const { CLOUDFLARE_WORKER, CRON_JOB_HANDLER } = require("../constants/bot");
const { devFlagMiddleware } = require("../middlewares/devFlag");
const { userAuthorization } = require("../middlewares/userAuthorization");

const oldAuthorizationMiddleware = authorizeRoles([APPOWNER, SUPERUSER]);
const newAuthorizationMiddleware = authorizeAndAuthenticate(
Expand Down Expand Up @@ -63,8 +65,26 @@ router.patch(
updateSelfTask,
tasks.updateTaskStatus,
assignTask
); // this route is being deprecated in favor of /tasks/:id/status.
router.patch(
"/:id/status",
authenticate,
devFlagMiddleware,
invalidateCache({ invalidationKeys: [ALL_TASKS] }),
updateSelfTask,
tasks.updateTaskStatus,
assignTask
);
router.patch("/assign/self", authenticate, invalidateCache({ invalidationKeys: [ALL_TASKS] }), tasks.assignTask); // this route is being deprecated in favor of /assign/:userId.

router.patch(
"/assign/:userId",
authenticate,
devFlagMiddleware,
userAuthorization,
invalidateCache({ invalidationKeys: [ALL_TASKS] }),
tasks.assignTask
);
router.patch("/assign/self", authenticate, invalidateCache({ invalidationKeys: [ALL_TASKS] }), tasks.assignTask);

router.get("/users/discord", verifyCronJob, getUsersValidator, tasks.getUsersHandler);

Expand Down
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
18 changes: 18 additions & 0 deletions test/fixtures/tasks/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,23 @@ module.exports = () => {
createdAt: 1644753600,
updatedAt: 1644753600,
},
{
title: "Test task",
type: "feature",
endsOn: 1234,
startedOn: 4567,
status: "AVAILABLE",
percentCompleted: 0,
category: "FRONTEND",
level: 3,
participants: [],
completionAward: { [DINERO]: 3, [NEELAM]: 300 },
lossRate: { [DINERO]: 1 },
priority: "HIGH",
isNoteworthy: true,
assignee: false,
createdAt: 1644753600,
updatedAt: 1644753600,
},
];
};
27 changes: 27 additions & 0 deletions test/fixtures/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,32 @@ module.exports = () => {
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1667685133/profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar.jpg",
},
},
{
username: "vikasinghdon",
first_name: "Vikas",
last_name: "Singh",
discordId: "yashu_yashu",
yoe: 10,
img: "./img.png",
linkedin_id: "destroyer",
github_id: "pickme",
github_display_name: "Vikas Singh",
phone: "1234567890",
email: "[email protected]",
joined_discord: "2024-07-16T18:21:09.278000+00:00",
status: "idle",
tokens: {
githubAccessToken: "githubAccessToken",
},
roles: {
super_user: false,
archived: false,
in_discord: true,
},
picture: {
publicId: "",
url: "",
},
},
];
};
Loading

0 comments on commit 4699602

Please sign in to comment.