Skip to content

Commit

Permalink
refactor(classes): Update Add Admin API endpoint to support multiple …
Browse files Browse the repository at this point in the history
…identifiers

- Modified the request body for the `/api/classes/:id/admins` endpoint.
- Changed the parameter from accepting only `userId` to `identifier`, which supports email, username, or user ID.
- Updated the endpoint documentation to reflect the change, allowing more flexible identification of users during admin addition.
  • Loading branch information
TKanX committed Oct 2, 2024
1 parent 3d6044e commit 53b6a25
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 47 deletions.
4 changes: 2 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1518,11 +1518,11 @@ The API uses JWT (JSON Web Token) for authentication. To access protected routes

```json
{
"userId": "user_id"
"identifier": "email or username or user_id"
}
```

> **Note:** The ID of the user to add as an admin.
> **Note:** The email, username, or ID of the user to add as an admin.
- **Response**:

Expand Down
24 changes: 12 additions & 12 deletions src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const validationUtils = require("../utils/validationUtils");

/**
* @function registerUser - Handle user registration request.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const registerUser = async (req, res) => {
const { email, callbackUrl } = req.body;
Expand Down Expand Up @@ -45,8 +45,8 @@ const registerUser = async (req, res) => {

/**
* @function completeRegistration - Handle user registration completion request.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const completeRegistration = async (req, res) => {
const { token, username, password } = req.body;
Expand Down Expand Up @@ -115,8 +115,8 @@ const completeRegistration = async (req, res) => {

/**
* @function loginUser - Handle user login request.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const loginUser = async (req, res) => {
const { identifier, password } = req.body;
Expand Down Expand Up @@ -186,8 +186,8 @@ const loginUser = async (req, res) => {

/**
* @function refreshToken - Handle refresh token request.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const refreshToken = async (req, res) => {
const { refreshToken } = req.body;
Expand Down Expand Up @@ -226,8 +226,8 @@ const refreshToken = async (req, res) => {

/**
* @function resetPassword - Handle password reset request.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const resetPassword = async (req, res) => {
const { email, callbackUrl } = req.body;
Expand Down Expand Up @@ -274,8 +274,8 @@ const resetPassword = async (req, res) => {

/**
* @function completeResetPassword - Handle password reset completion request.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const completeResetPassword = async (req, res) => {
const { token, password } = req.body;
Expand Down
49 changes: 26 additions & 23 deletions src/controllers/classController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const validationUtils = require("../utils/validationUtils");

/**
* @function createClass - Create a new class.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const createClass = async (req, res) => {
const teacherId = req.user.userId;
Expand Down Expand Up @@ -39,8 +39,8 @@ const createClass = async (req, res) => {

/**
* @function deleteClass - Delete a class by ID.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const deleteClass = async (req, res) => {
const teacherId = req.user.userId;
Expand Down Expand Up @@ -83,8 +83,8 @@ const deleteClass = async (req, res) => {

/**
* @function renameClass - Rename a class by ID.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const renameClass = async (req, res) => {
const userId = req.user.userId;
Expand Down Expand Up @@ -132,8 +132,8 @@ const renameClass = async (req, res) => {

/**
* @function getClasses - Get all classes by user ID (teacher or admin).
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const getClasses = async (req, res) => {
const userId = req.user.userId;
Expand All @@ -152,8 +152,8 @@ const getClasses = async (req, res) => {

/**
* @function getClass - Get a class by ID.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const getClass = async (req, res) => {
const userId = req.user.userId;
Expand Down Expand Up @@ -190,21 +190,24 @@ const getClass = async (req, res) => {

/**
* @function addAdmin - Add an admin to a class by ID.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const addAdmin = async (req, res) => {
const teacherId = req.user.userId;
const { id } = req.params;
const { userId } = req.body;
const { identifier } = req.body;

// Check if class ID is valid
if (!validationUtils.validateClassCode(id)) {
return res.badRequest("Invalid class ID.", "INVALID_CLASS_ID");
}

// Check if user ID is valid
const user = await userService.getUserById(userId);
const user =
(await userService.getUserById(identifier)) ||
(await userService.getUserByEmail(identifier)) ||
(await userService.getUserByUsername(identifier));
if (!user) {
return res.notFound("User not found.", "USER_NOT_FOUND");
}
Expand All @@ -225,7 +228,7 @@ const addAdmin = async (req, res) => {
}

// Add admin to class
const updatedClass = await classService.addAdmin(id, userId);
const updatedClass = await classService.addAdmin(id, user._id);
return res.success(updatedClass, "Admin added successfully.");
} catch (error) {
return res.internalServerError("Error adding admin.", "ADD_ADMIN_ERROR");
Expand All @@ -234,8 +237,8 @@ const addAdmin = async (req, res) => {

/**
* @function removeAdmin - Remove an admin from a class by ID.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const removeAdmin = async (req, res) => {
const teacherId = req.user.userId;
Expand Down Expand Up @@ -281,8 +284,8 @@ const removeAdmin = async (req, res) => {

/**
* @function addStudent - Add a student to a class by ID.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const addStudent = async (req, res) => {
const userId = req.user.userId;
Expand Down Expand Up @@ -330,8 +333,8 @@ const addStudent = async (req, res) => {

/**
* @function renameStudent - Rename a student in a class by ID.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const renameStudent = async (req, res) => {
const userId = req.user.userId;
Expand Down Expand Up @@ -388,8 +391,8 @@ const renameStudent = async (req, res) => {

/**
* @function deleteStudent - Delete a student from a class by ID.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
const deleteStudent = async (req, res) => {
const userId = req.user.userId;
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/problemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const problemGenerator = new ProblemGenerator();

/**
* @function getIndex - Get the problem generator index.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
getIndex = (req, res) => {
res.success(index, "Problem generator index retrieved successfully.");
};

/**
* @function generateProblem - Generate a new problem.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
*/
generateProblem = (req, res) => {
const topicsString = req.params[0];
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/authMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const publicRootRoutes = ["/api/problems"];

/**
* @function authMiddleware - Authenticate the user's JWT.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
* @param {Function} next - The next middleware function.
* @returns {Function} - The next middleware function.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/preprocessRequestDetailsMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

/**
* @function preprocessRequestDetailsMiddleware - Preprocess request details.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
* @param {Function} next - The next middleware function.
* @returns {Function} - The next middleware function.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/responseMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

/**
* @function responseMiddleware - Middleware for handling responses.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Request} req - The request object.
* @param {Response} res - The response object.
* @param {Function} next - The next middleware function.
* @returns {Function} - The next middleware function.
*/
Expand Down

0 comments on commit 53b6a25

Please sign in to comment.