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

Banner validations #450

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
317 changes: 111 additions & 206 deletions backend/src/controllers/banner.controller.js
Original file line number Diff line number Diff line change
@@ -1,236 +1,141 @@
const bannerService = require("../service/banner.service.js");
const { internalServerError } = require("../utils/errors.helper");
const { validateCloseButtonAction } = require("../utils/guide.helper");
const {
validatePosition,
validateUrl,
validateRelativeUrl,
} = require("../utils/banner.helper");
const { checkColorFieldsFail } = require("../utils/guide.helper");
const db = require("../models/index.js");
const { ErrorHandler } = require("../utils/banner.helper.js");
const Banner = db.Banner;

class BannerController {
async addBanner(req, res) {
const userId = req.user.id;
const {
position,
closeButtonAction,
fontColor,
backgroundColor,
actionUrl,
url,
} = req.body;

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

if (
!validatePosition(position) ||
!validateCloseButtonAction(closeButtonAction)
) {
return res.status(400).json({
errors: [{ msg: "Invalid value entered" }],
});
}

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 = { fontColor, backgroundColor };
const colorCheck = checkColorFieldsFail(colorFields, res);
if (colorCheck) {
return colorCheck;
}

try {
const newBannerData = { ...req.body, createdBy: userId };
const newBanner = await bannerService.createBanner(newBannerData);
res.status(201).json(newBanner);
} catch (err) {
console.log(err);
const { statusCode, payload } = internalServerError(
"CREATE_BANNER_ERROR",
err.message
);
res.status(statusCode).json(payload);
}
return await ErrorHandler.handleAsync(
res,
async () => {
const newBannerData = {
...req.body,
createdBy: req.user.id,
};
const newBanner = await bannerService.createBanner(newBannerData);
return res.status(201).json({
success: true,
data: newBanner,
});
},
"CREATE_BANNER_ERROR"
);
}

async deleteBanner(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 bannerService.deleteBanner(id);
return await ErrorHandler.handleAsync(
res,
async () => {
const { id } = req.params;
const deletionResult = await bannerService.deleteBanner(id);

if (!deletionResult) {
return res.status(404).json({
success: false,
error: "Banner not found",
});
}

if (!deletionResult) {
return res.status(400).json({
errors: [{ msg: "Banner with the specified id does not exist" }],
return res.status(200).json({
success: true,
message: `Banner with ID ${id} deleted successfully`,
});
}

res
.status(200)
.json({ message: `Banner with ID ${id} deleted successfully` });
} catch (err) {
const { statusCode, payload } = internalServerError(
"DELETE_BANNER_ERROR",
err.message
);
res.status(statusCode).json(payload);
}
},
"DELETE_BANNER_ERROR"
);
}

async editBanner(req, res) {
try {
const { id } = req.params;
const {
fontColor,
backgroundColor,
url,
position,
closeButtonAction,
actionUrl,
} = req.body;

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

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

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

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 }] });
return await ErrorHandler.handleAsync(
res,
async () => {
const { id } = req.params;
const updatedBanner = await bannerService.updateBanner(id, req.body);

if (!updatedBanner) {
return res.status(404).json({
success: false,
error: "Banner not found",
});
}
}

const colorFields = { fontColor, backgroundColor };
const colorCheck = checkColorFieldsFail(colorFields, res);
if (colorCheck) {
return colorCheck;
}

const updatedBanner = await bannerService.updateBanner(id, req.body);
res.status(200).json(updatedBanner);
} catch (err) {
const { statusCode, payload } = internalServerError(
"EDIT_BANNER_ERROR",
err.message
);
res.status(statusCode).json(payload);
}
return res.status(200).json({
success: true,
data: updatedBanner,
});
},
"EDIT_BANNER_ERROR"
);
}

async getAllBanners(req, res) {
try {
const banners = await bannerService.getAllBanners();
res.status(200).json(banners);
} catch (err) {
const { statusCode, payload } = internalServerError(
"GET_ALL_BANNERS_ERROR",
err.message
);
res.status(statusCode).json(payload);
}
return await ErrorHandler.handleAsync(
res,
async () => {
const banners = await bannerService.getAllBanners();
return res.status(200).json({
success: true,
data: banners,
});
},
"GET_ALL_BANNERS_ERROR"
);
}

async getBanners(req, res) {
try {
const userId = req.user.id;
const banners = await bannerService.getBanners(userId);
res.status(200).json(banners);
} catch (err) {
const { statusCode, payload } = internalServerError(
"GET_BANNERS_ERROR",
err.message
);
res.status(statusCode).json(payload);
}
return await ErrorHandler.handleAsync(
res,
async () => {
const userId = req.user.id;
const banners = await bannerService.getBanners(userId);
return res.status(200).json({
success: true,
data: banners,
});
},
"GET_BANNERS_ERROR"
);
}

async getBannerById(req, res) {
try {
const { id } = req.params;

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

const banner = await bannerService.getBannerById(id);

if (!banner) {
return res.status(404).json({ errors: [{ msg: "Banner not found" }] });
}
return await ErrorHandler.handleAsync(
res,
async () => {
const { id } = req.params;
const banner = await bannerService.getBannerById(id);

if (!banner) {
return res.status(404).json({
success: false,
error: "Banner not found",
});
}

res.status(200).json(banner);
} catch (err) {
const { statusCode, payload } = internalServerError(
"GET_BANNER_BY_ID_ERROR",
err.message
);
res.status(statusCode).json(payload);
}
return res.status(200).json({
success: true,
data: banner,
});
},
"GET_BANNER_BY_ID_ERROR"
);
}
async getBannerByUrl(req, res) {
try {
const { url } = req.body;

if (!url || typeof url !== "string") {
return res
.status(400)
.json({ errors: [{ msg: "URL is missing or invalid" }] });
}
return await ErrorHandler.handleAsync(
res,
async () => {
const { url } = req.body;

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

const banner = await bannerService.getBannerByUrl(url);
res.status(200).json({ banner });
} catch (error) {
const { payload, statusCode } = internalServerError(
"GET_BANNER_BY_URL_ERROR",
error.message
);
res.status(statusCode).json(payload);
}
const banner = await bannerService.getBannerByUrl(url);
return res.status(200).json({ banner });
},
"GET_BANNER_BY_URL_ERROR"
);
}
}

Expand Down
9 changes: 5 additions & 4 deletions backend/src/routes/banner.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ const bannerController = require("../controllers/banner.controller.js");
const authenticateJWT = require("../middleware/auth.middleware");
const settings = require('../../config/settings');
const accessGuard = require('../middleware/accessGuard.middleware');
const {addBannerValidation, deleteBannerValidation, editBannerValidation, getBannerByIdValidation} = require('../utils/banner.helper.js')

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

router.post("/add_banner", authenticateJWT, accessGuard(teamPermissions.banners), bannerController.addBanner);
router.delete("/delete_banner/:id", authenticateJWT, accessGuard(teamPermissions.banners), bannerController.deleteBanner);
router.put("/edit_banner/:id", authenticateJWT, accessGuard(teamPermissions.banners), bannerController.editBanner);
router.post("/add_banner", authenticateJWT, accessGuard(teamPermissions.banners), addBannerValidation, bannerController.addBanner);
router.delete("/delete_banner/:id", authenticateJWT, accessGuard(teamPermissions.banners), deleteBannerValidation, bannerController.deleteBanner);
router.put("/edit_banner/:id", authenticateJWT, accessGuard(teamPermissions.banners), editBannerValidation, bannerController.editBanner);
router.get("/all_banners", authenticateJWT, bannerController.getAllBanners);
router.get("/banners", authenticateJWT, bannerController.getBanners);
router.get("/get_banner/:id", authenticateJWT, bannerController.getBannerById);
router.get("/get_banner/:id", authenticateJWT, getBannerByIdValidation, bannerController.getBannerById);
router.get("/get_banner_by_url", bannerController.getBannerByUrl);

module.exports = router;
1 change: 1 addition & 0 deletions backend/src/service/banner.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class BannerService {
async getAllBanners() {
return await Banner.findAll({
include: [{ model: db.User, as: "creator" }],

});
}

Expand Down
Loading
Loading