Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implements a new GET route /users?profile=true #2201

Merged
merged 12 commits into from
Oct 16, 2024
Merged
41 changes: 40 additions & 1 deletion controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const { getUserStatus } = require("../models/userStatus");
const config = require("config");
const { generateUniqueUsername } = require("../services/users");
const discordDeveloperRoleId = config.get("discordDeveloperRoleId");
const authService = require("../services/authService");

const verifyUser = async (req, res) => {
const userId = req.userData.id;
Expand Down Expand Up @@ -113,6 +114,39 @@ const getUsers = async (req, res) => {
});
}

if (req.query.profile) {
vikasosmium marked this conversation as resolved.
Show resolved Hide resolved
if (dev) {
let result, user;
let token = req.cookies[config.get("userToken.cookieName")];

// Extract token from authorization header if not in production and no token in cookies
if (process.env.NODE_ENV !== "production" && !token) {
token = req.headers.authorization ? req.headers.authorization.split(" ")[1] : null;
vikasosmium marked this conversation as resolved.
Show resolved Hide resolved
}

// Verify the token
let userId;
try {
({ userId } = authService.verifyAuthToken(token));
} catch (error) {
logger.error(`Token verification failed: ${error}`);
return res.boom.unauthorized("Unauthenticated User");
}
vikasosmium marked this conversation as resolved.
Show resolved Hide resolved
vikasosmium marked this conversation as resolved.
Show resolved Hide resolved

try {
result = await dataAccess.retrieveUsers({ id: userId });
user = result.user;
vikasosmium marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
logger.error(`Error while fetching user: ${error}`);
return res.boom.serverUnavailable(INTERNAL_SERVER_ERROR);
}

return res.send(user);
} else {
return res.boom.badRequest("Route not found");
}
}

if (!transformedQuery?.days && transformedQuery?.filterBy === "unmerged_prs") {
return res.boom.badRequest(`Days is required for filterBy ${transformedQuery?.filterBy}`);
}
Expand Down Expand Up @@ -377,7 +411,11 @@ const getSelfDetails = async (req, res) => {
const user = await dataAccess.retrieveUsers({
userdata: req.userData,
});
return res.send(user);
return res.send({
message:
"This API endpoint is scheduled for deprecation and will soon be discontinued. Please update your integrations to use the new endpoint: '/users?profile=true'⚠️🔴",
vikasosmium marked this conversation as resolved.
Show resolved Hide resolved
user: user,
});
}
return res.boom.notFound("User doesn't exist");
} catch (error) {
Expand All @@ -393,6 +431,7 @@ const getSelfDetails = async (req, res) => {
* @param req.body {Object} - User object
* @param res {Object} - Express response object
*/

const updateSelf = async (req, res) => {
try {
const { id: userId, roles: userRoles, discordId } = req.userData;
Expand Down
1 change: 1 addition & 0 deletions middlewares/validators/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ async function getUsers(req, res, next) {
}),
query: joi.string().optional(),
q: joi.string().optional(),
profile: joi.string().optional(),
filterBy: joi.string().optional(),
days: joi.string().optional(),
dev: joi.string().optional(),
Expand Down
57 changes: 57 additions & 0 deletions test/integration/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,63 @@ describe("Users", function () {
return done();
});
});

it("Should return the logged user's details", function (done) {
chai
.request(app)
.get("/users?profile=true&dev=true")
.set("cookie", `${cookieName}=${jwt}`)
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(200);
expect(res.body).to.be.a("object");
expect(res.body).to.not.have.property("phone");
expect(res.body).to.not.have.property("email");
expect(res.body).to.not.have.property("chaincode");

return done();
});
});

vikasosmium marked this conversation as resolved.
Show resolved Hide resolved
it("Should throw an error when there is no feature flag given", function (done) {
chai
.request(app)
.get("/users?profile=true")
.set("cookie", `${cookieName}=${jwt}`)
.end((err, res) => {
if (err) {
return done(err);
}
expect(res).to.have.status(400);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Route not found");
return done();
});
});

it("Should return 401 if not logged in", function (done) {
chai
.request(app)
.get("/users?profile=true&dev=true")
.set("cookie", `${cookieName}=invalid_token`)
.end((err, res) => {
if (err) {
return done();
}

expect(res).to.have.status(401);
expect(res.body).to.be.an("object");
expect(res.body).to.eql({
statusCode: 401,
error: "Unauthorized",
message: "Unauthenticated User",
});

return done();
});
});
});

describe("GET /users/self", function () {
Expand Down