Skip to content

Commit

Permalink
Merge pull request #549 from Anurag9977/refactor-popup-validations
Browse files Browse the repository at this point in the history
Refactor Popup Validations
  • Loading branch information
erenfn authored Feb 24, 2025
2 parents 2982976 + 51c719c commit 458e104
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 493 deletions.
147 changes: 0 additions & 147 deletions backend/src/controllers/popup.controller.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,9 @@
const popupService = require("../service/popup.service");
const { internalServerError } = require("../utils/errors.helper");
const { validateCloseButtonAction, validateRepetitionOption } = require("../utils/guide.helper");
const {
validatePopupSize,
validateUrl,
validateRelativeUrl,
} = require("../utils/popup.helper");
const { checkColorFieldsFail } = require("../utils/guide.helper");

class PopupController {
async addPopup(req, res) {
const userId = req.user.id;
const {
popupSize,
closeButtonAction,
repetitionType,
headerBackgroundColor,
headerColor,
textColor,
buttonBackgroundColor,
buttonTextColor,
actionUrl,
url,
} = req.body;

if (!popupSize || !closeButtonAction) {
return res.status(400).json({
errors: [{ msg: "popupSize and closeButtonAction are required" }],
});
}

if (
!validatePopupSize(popupSize) ||
!validateCloseButtonAction(closeButtonAction) ||
!validateRepetitionOption(repetitionType)
) {
return res.status(400).json({
errors: [{ msg: "Invalid value for popupSize or closeButtonAction or repetitionType" }],
});
}

if (actionUrl) {
try {
validateUrl(actionUrl, "actionUrl");
} catch (err) {
return res.status(400).json({ errors: [{ msg: err.message }] });
}
}

if (url) {
try {
validateRelativeUrl(url, "url");
} catch (err) {
return res.status(400).json({ errors: [{ msg: err.message }] });
}
}


const colorFields = {
headerBackgroundColor,
headerColor,
textColor,
buttonBackgroundColor,
buttonTextColor,
};
const colorCheck = checkColorFieldsFail(colorFields, res);
if (colorCheck) {
return colorCheck;
}

try {
const newPopupData = { ...req.body, createdBy: userId };
Expand All @@ -86,19 +22,13 @@ class PopupController {
async deletePopup(req, res) {
try {
const { id } = req.params;

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

const deletionResult = await popupService.deletePopup(id);

if (!deletionResult) {
return res.status(400).json({
errors: [{ msg: "Popup with the specified id does not exist" }],
});
}

res
.status(200)
.json({ message: `Popup with ID ${id} deleted successfully` });
Expand All @@ -114,71 +44,6 @@ class PopupController {
async editPopup(req, res) {
try {
const { id } = req.params;
const {
popupSize,
closeButtonAction,
repetitionType,
headerBackgroundColor,
headerColor,
textColor,
buttonBackgroundColor,
buttonTextColor,
actionUrl,
url,
} = req.body;

if (!popupSize || !closeButtonAction) {
return res.status(400).json({
errors: [{ msg: "popupSize and closeButtonAction are required" }],
});
}

if (!validatePopupSize(popupSize)) {
return res
.status(400)
.json({ errors: [{ msg: "Invalid value for popupSize" }] });
}

if (!validateCloseButtonAction(closeButtonAction)) {
return res
.status(400)
.json({ errors: [{ msg: "Invalid value for closeButtonAction" }] });
}

if (!validateRepetitionOption(repetitionType)) {
return res
.status(400)
.json({ errors: [{ msg: "Invalid value for repetition" }] });
}

if (actionUrl) {
try {
validateUrl(actionUrl, "actionUrl");
} catch (err) {
return res.status(400).json({ errors: [{ msg: err.message }] });
}
}

if (url) {
try {
validateRelativeUrl(url, "url");
} catch (err) {
return res.status(400).json({ errors: [{ msg: err.message }] });
}
}

const colorFields = {
headerBackgroundColor,
headerColor,
textColor,
buttonBackgroundColor,
buttonTextColor,
};
const colorCheck = checkColorFieldsFail(colorFields, res);
if (colorCheck) {
return colorCheck;
}

const updatedPopup = await popupService.updatePopup(id, req.body);
res.status(200).json(updatedPopup);
} catch (err) {
Expand Down Expand Up @@ -220,11 +85,6 @@ class PopupController {
async getPopupById(req, res) {
try {
const { id } = req.params;

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

const popup = await popupService.getPopupById(id);

if (!popup) {
Expand All @@ -243,13 +103,6 @@ class PopupController {
async getPopupByUrl(req, res) {
try {
const { url } = req.body;

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

const popup = await popupService.getPopupByUrl(url);
res.status(200).json({ popup });
} catch (error) {
Expand Down
12 changes: 7 additions & 5 deletions backend/src/routes/popup.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ const popupController = require("../controllers/popup.controller");
const authenticateJWT = require("../middleware/auth.middleware");
const settings = require('../../config/settings');
const accessGuard = require('../middleware/accessGuard.middleware');
const { addOrUpdatePopupValidation, getPopupByUrlValidation, deleteOrGetPopupByIdValidation } = require("../utils/popup.helper");
const { handleValidationErrors } = require("../middleware/validation.middleware");

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

router.post("/add_popup", authenticateJWT, accessGuard(teamPermissions.popups), popupController.addPopup);
router.delete("/delete_popup/:id", authenticateJWT, accessGuard(teamPermissions.popups), popupController.deletePopup);
router.put("/edit_popup/:id", authenticateJWT, accessGuard(teamPermissions.popups), popupController.editPopup);
router.post("/add_popup", authenticateJWT, accessGuard(teamPermissions.popups),addOrUpdatePopupValidation, handleValidationErrors, popupController.addPopup);
router.delete("/delete_popup/:id", authenticateJWT, accessGuard(teamPermissions.popups), deleteOrGetPopupByIdValidation, handleValidationErrors, popupController.deletePopup);
router.put("/edit_popup/:id", authenticateJWT, accessGuard(teamPermissions.popups), addOrUpdatePopupValidation, handleValidationErrors,popupController.editPopup);
router.get("/all_popups", authenticateJWT, popupController.getAllPopups);
router.get("/popups", authenticateJWT, popupController.getPopups);
router.get("/get_popup/:id", authenticateJWT, popupController.getPopupById);
router.get("/get_popup_by_url", popupController.getPopupByUrl);
router.get("/get_popup/:id", authenticateJWT,deleteOrGetPopupByIdValidation, handleValidationErrors ,popupController.getPopupById);
router.get("/get_popup_by_url", getPopupByUrlValidation, handleValidationErrors, popupController.getPopupByUrl);

module.exports = router;
Loading

0 comments on commit 458e104

Please sign in to comment.