Skip to content

Commit

Permalink
Merge pull request #382 from bluewave-labs/375-add-url-to-banner-and-…
Browse files Browse the repository at this point in the history
…popup

375 add url to banner and popup
  • Loading branch information
DeboraSerra authored Dec 16, 2024
2 parents a758ca4 + 7b232c7 commit 823ec6c
Show file tree
Hide file tree
Showing 16 changed files with 861 additions and 469 deletions.
18 changes: 18 additions & 0 deletions backend/migrations/20241210225728-add-action-url-banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn("banners", "actionUrl", {
type: Sequelize.STRING,
allowNull: true,
validate: {
isUrl: true,
},
});
},

async down(queryInterface, Sequelize) {
await queryInterface.removeColumn("banners", "actionUrl");
},
};
18 changes: 18 additions & 0 deletions backend/migrations/20241210225734-add-action-url-popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn("popup", "actionUrl", {
type: Sequelize.STRING,
allowNull: true,
validate: {
isUrl: true,
},
});
},

async down(queryInterface, Sequelize) {
await queryInterface.removeColumn("popup", "actionUrl");
},
};
134 changes: 93 additions & 41 deletions backend/src/controllers/banner.controller.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,61 @@
const bannerService = require("../service/banner.service.js");
const { internalServerError } = require("../utils/errors.helper");
const { validateCloseButtonAction } = require("../utils/guide.helper");
const { validatePosition } = require("../utils/banner.helper");
const {
validatePosition,
validateUrl,
validateRelativeUrl,
} = require("../utils/banner.helper");
const { checkColorFieldsFail } = require("../utils/guide.helper");

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

if (!position || !closeButtonAction) {
return res
.status(400)
.json({
errors: [{ msg: "position and closeButtonAction are required" }],
});
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 (
!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 };
const colorCheck = checkColorFieldsFail(colorFields, res);
if (colorCheck) {
return colorCheck;
}

try {
const newBannerData = { ...req.body, createdBy: userId };
Expand All @@ -37,7 +65,7 @@ class BannerController {
console.log(err);
const { statusCode, payload } = internalServerError(
"CREATE_BANNER_ERROR",
err.message,
err.message
);
res.status(statusCode).json(payload);
}
Expand All @@ -54,11 +82,9 @@ class BannerController {
const deletionResult = await bannerService.deleteBanner(id);

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

res
Expand All @@ -67,7 +93,7 @@ class BannerController {
} catch (err) {
const { statusCode, payload } = internalServerError(
"DELETE_BANNER_ERROR",
err.message,
err.message
);
res.status(statusCode).json(payload);
}
Expand All @@ -76,14 +102,19 @@ class BannerController {
async editBanner(req, res) {
try {
const { id } = req.params;
const { fontColor, backgroundColor, url, position, closeButtonAction, bannerText } = req.body;
const {
fontColor,
backgroundColor,
url,
position,
closeButtonAction,
actionUrl,
} = req.body;

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

if (!validatePosition(position)) {
Expand All @@ -98,16 +129,34 @@ class BannerController {
.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 }] });
}
}

const colorFields = { fontColor, backgroundColor };
const colorCheck = checkColorFieldsFail(colorFields, res)
if (colorCheck) { return colorCheck };
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,
err.message
);
res.status(statusCode).json(payload);
}
Expand All @@ -120,7 +169,7 @@ class BannerController {
} catch (err) {
const { statusCode, payload } = internalServerError(
"GET_ALL_BANNERS_ERROR",
err.message,
err.message
);
res.status(statusCode).json(payload);
}
Expand All @@ -133,8 +182,8 @@ class BannerController {
res.status(200).json(banners);
} catch (err) {
const { statusCode, payload } = internalServerError(
"GET\_BANNERS_ERROR",
err.message,
"GET_BANNERS_ERROR",
err.message
);
res.status(statusCode).json(payload);
}
Expand All @@ -158,7 +207,7 @@ class BannerController {
} catch (err) {
const { statusCode, payload } = internalServerError(
"GET_BANNER_BY_ID_ERROR",
err.message,
err.message
);
res.status(statusCode).json(payload);
}
Expand All @@ -167,19 +216,22 @@ class BannerController {
try {
const { url } = req.body;

if (!url || typeof url !== 'string' ) {
return res.status(400).json({ errors: [{ msg: "URL is missing or invalid" }] });
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});
res.status(200).json({ banner });
} catch (error) {
internalServerError(
const { payload, statusCode } = internalServerError(
"GET_BANNER_BY_URL_ERROR",
error.message,
error.message
);
res.status(statusCode).json(payload);
}
};
}
}

module.exports = new BannerController();
Loading

0 comments on commit 823ec6c

Please sign in to comment.