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

389 refactoring validations in hint routes with express validator #490

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 20 additions & 47 deletions backend/src/controllers/hint.controller.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
const HintService = require("../service/hint.service");
const { internalServerError } = require("../utils/errors.helper");
const validateHintData = require("../utils/hint.helper");
const HintService = require('../service/hint.service');
const { internalServerError } = require('../utils/errors.helper');

class HintController {
async addHint(req, res) {
const userId = req.user.id;

const validationErrors = validateHintData(req.body);

if (validationErrors.length > 0) {
return res.status(400).json({ errors: validationErrors });
}

try {
const hint = await HintService.createHint({
...req.body,
Expand All @@ -22,8 +15,8 @@ class HintController {
} catch (error) {
console.error(`CREATE_HINT_ERROR: ${error.message}`);
const { statusCode, payload } = internalServerError(
"CREATE_HINT_ERROR",
"An unexpected error occurred while creating the hint"
'CREATE_HINT_ERROR',
'An unexpected error occurred while creating the hint'
);
res.status(statusCode).json(payload);
}
Expand All @@ -38,8 +31,8 @@ class HintController {
} catch (error) {
console.error(`GET_HINTS_ERROR: ${error.message}`);
const { statusCode, payload } = internalServerError(
"GET_HINTS_ERROR",
"An unexpected error occurred while retrieving hints"
'GET_HINTS_ERROR',
'An unexpected error occurred while retrieving hints'
);
res.status(statusCode).json(payload);
}
Expand All @@ -52,8 +45,8 @@ class HintController {
} catch (error) {
console.error(`GET_ALL_HINTS_ERROR: ${error.message}`);
const { statusCode, payload } = internalServerError(
"GET_ALL_HINTS_ERROR",
"An unexpected error occurred while retrieving hints"
'GET_ALL_HINTS_ERROR',
'An unexpected error occurred while retrieving hints'
);
res.status(statusCode).json(payload);
}
Expand All @@ -62,25 +55,21 @@ class HintController {
async getHintById(req, res) {
const { hintId } = req.params;

if (Number.isNaN(Number(hintId)) || hintId.trim() === "") {
return res.status(400).json({ errors: [{ msg: "Invalid hint ID" }] });
}

try {
const hint = await HintService.getHintById(hintId);

if (!hint) {
return res.status(404).json({
errors: [{ msg: "Hint not found" }],
errors: [{ msg: 'Hint not found' }],
});
}

return res.status(200).json(hint);
} catch (error) {
console.error(`GET_HINT_BY_ID_ERROR: ${error.message}`);
const { statusCode, payload } = internalServerError(
"GET_HINT_BY_ID_ERROR",
"An unexpected error occurred while retrieving the hint"
'GET_HINT_BY_ID_ERROR',
'An unexpected error occurred while retrieving the hint'
);
res.status(statusCode).json(payload);
}
Expand All @@ -89,18 +78,12 @@ class HintController {
async updateHint(req, res) {
const { hintId } = req.params;

const validationErrors = validateHintData(req.body);

if (validationErrors.length > 0) {
return res.status(400).json({ errors: validationErrors });
}

try {
const hint = await HintService.getHintById(hintId);

if (!hint) {
return res.status(404).json({
errors: [{ msg: "Hint not found" }],
errors: [{ msg: 'Hint not found' }],
});
}

Expand All @@ -110,8 +93,8 @@ class HintController {
} catch (error) {
console.error(`UPDATE_HINT_ERROR: ${error.message}`);
const { statusCode, payload } = internalServerError(
"UPDATE_HINT_ERROR",
"An unexpected error occurred while updating the hint"
'UPDATE_HINT_ERROR',
'An unexpected error occurred while updating the hint'
);
res.status(statusCode).json(payload);
}
Expand All @@ -120,16 +103,12 @@ class HintController {
async deleteHint(req, res) {
const { hintId } = req.params;

if (isNaN(hintId) || hintId.trim() === "") {
return res.status(400).json({ errors: [{ msg: "Invalid hint ID" }] });
}

try {
const deleted = await HintService.deleteHint(hintId);

if (!deleted) {
return res.status(404).json({
errors: [{ msg: "Hint not found" }],
errors: [{ msg: 'Hint not found' }],
});
}

Expand All @@ -139,8 +118,8 @@ class HintController {
} catch (error) {
console.error(`DELETE_HINT_ERROR: ${error.message}`);
const { statusCode, payload } = internalServerError(
"DELETE_HINT_ERROR",
"An unexpected error occurred while deleting the hint"
'DELETE_HINT_ERROR',
'An unexpected error occurred while deleting the hint'
);
res.status(statusCode).json(payload);
}
Expand All @@ -150,19 +129,13 @@ class HintController {
try {
const { url } = req.body;

if (!url || typeof url !== 'string') {
return res.status(400).json({ errors: [{ msg: "URL is missing or invalid" }] });
}

const hint = await HintService.getHintByUrl(url);
res.status(200).json({ hint });
} catch (error) {
internalServerError(
"GET_HINT_BY_URL_ERROR",
error.message,
);
const {payload, statusCode} =internalServerError('GET_HINT_BY_URL_ERROR', error.message);
res.status(statusCode).json(payload);
}
};
}
}

module.exports = new HintController();
30 changes: 19 additions & 11 deletions backend/src/routes/hint.routes.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
const express = require("express");
const hintController = require("../controllers/hint.controller");
const authenticateJWT = require("../middleware/auth.middleware");
const express = require('express');
const hintController = require('../controllers/hint.controller');
const authenticateJWT = require('../middleware/auth.middleware');
const settings = require('../../config/settings');
const accessGuard = require('../middleware/accessGuard.middleware');
const { hintValidator, paramIdValidator, bodyUrlValidator } = require('../utils/hint.helper');
const { handleValidationErrors } = require('../middleware/validation.middleware');

const router = express.Router();
const teamPermissions = settings.team.permissions;

router.post("/add_hint", authenticateJWT, accessGuard(teamPermissions.hints), hintController.addHint);
router.delete("/delete_hint/:hintId", authenticateJWT, accessGuard(teamPermissions.hints), hintController.deleteHint);
router.put("/edit_hint/:hintId", authenticateJWT, accessGuard(teamPermissions.hints), hintController.updateHint);
router.get("/all_hints", authenticateJWT, hintController.getAllHints);
router.get("/hints", authenticateJWT, hintController.getHints);
router.get("/get_hint/:hintId", authenticateJWT, hintController.getHintById);
router.get("/get_hint_by_url", hintController.getHintByUrl);
router.get('/get_hint_by_url', bodyUrlValidator, handleValidationErrors, hintController.getHintByUrl);

module.exports = router;
router.use(authenticateJWT);

router.get('/all_hints', hintController.getAllHints);
router.get('/hints', hintController.getHints);
router.get('/get_hint/:hintId', paramIdValidator, handleValidationErrors, hintController.getHintById);

router.use(accessGuard(teamPermissions.hints));

router.post('/add_hint', hintValidator, handleValidationErrors, hintController.addHint);
router.delete('/delete_hint/:hintId', paramIdValidator, handleValidationErrors, hintController.deleteHint);
router.put('/edit_hint/:hintId', paramIdValidator, hintValidator, handleValidationErrors, hintController.updateHint);

module.exports = router;
6 changes: 3 additions & 3 deletions backend/src/test/e2e/auth.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import chai from "./index.mjs";

const user = mocks.UserBuilder.user;

describe("E2e tests auth", () => {
describe.only("E2e tests auth", () => {
DeboraSerra marked this conversation as resolved.
Show resolved Hide resolved
describe("POST /api/auth/register", () => {
beforeEach(async () => {
const dbReadyOptions = {
Expand Down Expand Up @@ -273,7 +273,7 @@ describe("E2e tests auth", () => {
});
});
});
describe("POST /api/auth/forget-password", () => {
describe.skip("POST /api/auth/forget-password", () => {
beforeEach(async () => {
await chai.request
.execute(app)
Expand Down Expand Up @@ -317,7 +317,7 @@ describe("E2e tests auth", () => {
});
});
});
describe("POST /api/auth/reset-password", () => {
describe.skip("POST /api/auth/reset-password", () => {
let token;
beforeEach(async () => {
await chai.request
Expand Down
Loading
Loading