Skip to content

Commit

Permalink
feat: improve user profile query
Browse files Browse the repository at this point in the history
  • Loading branch information
supersonictw committed Oct 15, 2024
1 parent 698f079 commit 6f5b63d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ router.get("/users/:user_id",

// Check user exists by the ID
const user = await User.findById(userId).exec();

// Send response
if (user) {
res.send(user);
} else {
if (!user) {
res.sendStatus(StatusCodes.NOT_FOUND);
return;
}

// Send response
const userData = user.toObject();
res.send(userData);
},
);

Expand Down
53 changes: 53 additions & 0 deletions src/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,59 @@ router.patch("/me/email",
},
);

/**
* @openapi
* /users/{user_id}:
* get:
* summary: Get user by ID
* description: Get user public profile by ID
* tags:
* - admin
* security:
* - ApiKeyAuth: []
* parameters:
* - name: user_id
* in: path
* description: ID of the user to retrieve
* required: true
* schema:
* type: string
* format: objectId
* responses:
* 200:
* description: User public profile
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* 401:
* description: User not found
*/
router.get("/users/:user_id",
middlewareValidator.param("user_id").isMongoId().notEmpty(),
middlewareInspector,
middlewareRestrictor(10, 60, true),
async (req, res) => {
// Assign shortcuts
const userId = req.params.user_id;

// Check user exists by the ID
const user = await User.findById(userId).exec();
if (!user) {
res.sendStatus(StatusCodes.UNAUTHORIZED);
return;
}

// Send response
const userData = user.toObject();
res.send({
profile: {
nickname: userData.nickname,
},
});
},
);

/**
* @openapi
* /users:
Expand Down

0 comments on commit 6f5b63d

Please sign in to comment.