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

added find by url endpoints for models #357

Merged
merged 9 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion backend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ TEST_DB_USERNAME=user123
TEST_DB_PASSWORD=password123
TEST_DB_NAME=onboarding_db
TEST_DB_HOST=localhost
TEST_DB_PORT=5432
TEST_DB_PORT=5432
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:22
FROM node:22-alpine3.20

WORKDIR /app

Expand Down
2 changes: 2 additions & 0 deletions backend/config/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = {
popups: [userRole.ADMIN],
hints: [userRole.ADMIN],
banners: [userRole.ADMIN],
links: [userRole.ADMIN],
tours: [userRole.ADMIN],
}
}
};
8 changes: 4 additions & 4 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const hint = require('./src/routes/hint.routes');
const tourRoutes = require('./src/routes/tour.routes');
const linkRoutes = require('./src/routes/link.routes');
const helperLinkRoutes = require('./src/routes/helperLink.routes');
const guideRoutes = require('./src/routes/guide.routes');

const app = express();

Expand All @@ -30,16 +31,15 @@ app.use(bodyParser.json({ limit: MAX_FILE_SIZE }));
app.use(jsonErrorMiddleware);
// app.use(fileSizeValidator);

const { sequelize, Team } = require("./src/models");
const config = require("./config/config");
const { sequelize } = require("./src/models");

sequelize
.authenticate()
.then(() => console.log('Database connected...'))
.catch((err) => console.log('Error: ' + err));

sequelize
.sync({ force: true })
.sync({ force: false })
.then(() => console.log("Models synced with the database..."))
.catch((err) => console.log("Error syncing models: " + err));

Expand All @@ -50,7 +50,7 @@ app.use('/api/popup', popup);
app.use('/api/popup_log', popup_log);
app.use('/api/banner', banner);
app.use('/api/team', teamRoutes);
// app.use('/api/tours', tourRoutes);
app.use('/api/guide', guideRoutes);
app.use('/api/hint', hint);
app.use('/api/tour', tourRoutes);
app.use('/api/link', linkRoutes);
Expand Down
38 changes: 27 additions & 11 deletions backend/src/controllers/banner.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 { checkColorFieldsFail } =require("../utils/guide.helper");
const { checkColorFieldsFail } = require("../utils/guide.helper");

class BannerController {
async addBanner(req, res) {
Expand All @@ -27,7 +27,7 @@ class BannerController {

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

try {
const newBannerData = { ...req.body, createdBy: userId };
Expand All @@ -47,7 +47,7 @@ class BannerController {
try {
const { id } = req.params;

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

Expand Down Expand Up @@ -77,31 +77,31 @@ class BannerController {
try {
const { id } = req.params;
const { fontColor, backgroundColor, url, position, closeButtonAction, bannerText } = 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" }] });
}

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

const updatedBanner = await bannerService.updateBanner(id, req.body);
res.status(200).json(updatedBanner);
} catch (err) {
Expand All @@ -125,7 +125,7 @@ class BannerController {
res.status(statusCode).json(payload);
}
}

async getBanners(req, res) {
try {
const userId = req.user.id;
Expand All @@ -144,7 +144,7 @@ class BannerController {
try {
const { id } = req.params;

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

Expand All @@ -163,7 +163,23 @@ class BannerController {
res.status(statusCode).json(payload);
}
}
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" }] });
}

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

module.exports = new BannerController();
28 changes: 28 additions & 0 deletions backend/src/controllers/guide.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const bannerService = require("../service/banner.service");
const linkService = require("../service/link.service");
const popupService = require("../service/popup.service");

class GuideController {
async getGuidesByUrl(req, res) {
try {
const { url } = req.body;

if (!url || typeof url !== 'string') {
return res.status(400).json({ errors: [{ msg: "URL is missing or invalid" }] });
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
const [banner, popup, link] = await Promise.all([
bannerService.getBannerByUrl(url),
popupService.getPopupByUrl(url),
linkService.getLinkByUrl(url),
]);
res.status(200).json({ banner, popup, link });
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
internalServerError(
"GET_GUIDES_BY_URL_ERROR",
error.message,
);
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
}
}

module.exports = new GuideController();
20 changes: 18 additions & 2 deletions backend/src/controllers/hint.controller.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const HintService = require("../service/hint.service");
const { internalServerError } = require("../utils/errors.helper");
const validateHintData = require("../utils/hint.helper");
const db = require("../models");
const Hint = db.Hint;

class HintController {
async addHint(req, res) {
Expand Down Expand Up @@ -147,6 +145,24 @@ class HintController {
res.status(statusCode).json(payload);
}
}

async getHintByUrl(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 hint = await HintService.getHintByUrl(url);
res.status(200).json({ hint });
} catch (error) {
internalServerError(
"GET_HINT_BY_URL_ERROR",
error.message,
);
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
}
};
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = new HintController();
19 changes: 19 additions & 0 deletions backend/src/controllers/link.controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const bannerService = require("../service/banner.service");
const linkService = require("../service/link.service");
const { internalServerError } = require("../utils/errors.helper");
const { validateUrl } = require("../utils/link.helper");
Expand Down Expand Up @@ -165,6 +166,24 @@ class LinkController {
res.status(statusCode).json(payload);
}
}

async getLinkByUrl(req, res) {
try {
const { url } = req.body;

if (!url || typeof url !== 'string') {
return res.status(400).json({ errors: [{ msg: "URL is missing or invalid" }] });
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

const link = await linkService.getLinkByUrl(url);
res.status(200).json({ link });
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
internalServerError(
"GET_LINK_BY_URL_ERROR",
error.message,
);
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
};
}

module.exports = new LinkController();
16 changes: 16 additions & 0 deletions backend/src/controllers/popup.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,23 @@ class PopupController {
res.status(statusCode).json(payload);
}
}
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) {
internalServerError(
"GET_POPUP_BY_URL_ERROR",
error.message,
);
}
};

}

Expand Down
18 changes: 18 additions & 0 deletions backend/src/controllers/tour.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ class TourController {
res.status(statusCode).json(payload);
}
}

async getTourByUrl(req, res) {
try {
const { url } = req.body;

if (!url || typeof url !== 'string') {
return res.status(400).json({ errors: [{ msg: "URL is missing or invalid" }] });
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

const tour = await tourService.getTourByUrl(url);
res.status(200).json({ tour });
} catch (error) {
internalServerError(
"GET_TOUR_BY_URL_ERROR",
error.message,
);
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
};
}

module.exports = new TourController();
1 change: 1 addition & 0 deletions backend/src/routes/banner.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ router.put("/edit_banner/:id", authenticateJWT, accessGuard(teamPermissions.bann
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_by_url", bannerController.getBannerByUrl);
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

module.exports = router;
9 changes: 9 additions & 0 deletions backend/src/routes/guide.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require("express");
const guideController = require("../controllers/guide.controller.js");

const router = express.Router();

router.get("/get_guides_by_url", guideController.getGuidesByUrl);
// router.get("/get_incomplete_guides_by_url", guideController.getIncompleteGuidesByUrl);
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

module.exports = router;
1 change: 1 addition & 0 deletions backend/src/routes/hint.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ router.put("/edit_hint/:hintId", authenticateJWT, accessGuard(teamPermissions.hi
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);

module.exports = router;
18 changes: 10 additions & 8 deletions backend/src/routes/link.routes.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const express = require("express");
const linkController = require("../controllers/link.controller");
const authenticateJWT = require("../middleware/auth.middleware");
const settings = require('../../config/settings');
const accessGuard = require('../middleware/accessGuard.middleware');

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

router.use(authenticateJWT);

router.route("/add_link").post(linkController.addLink);
router.route("/get_links").get(linkController.getLinksByHelperId);
router.route("/all_links").get(linkController.getAllLinks);
router.route("/get_link/:id").get(linkController.getLinksById);
router.route("/edit_link/:id").put(linkController.editLink);
router.route("/delete_link/:id").delete(linkController.deleteLink);
router.post("/add_link", authenticateJWT, accessGuard(teamPermissions.links), linkController.addLink);
router.get("/get_links", authenticateJWT, linkController.getLinksByHelperId);
router.get("/all_links", authenticateJWT, linkController.getAllLinks);
router.get("/get_link/:id", authenticateJWT, linkController.getLinksById);
router.put("/edit_link/:id", authenticateJWT, accessGuard(teamPermissions.links), linkController.editLink);
router.delete("/delete_link/:id", authenticateJWT, accessGuard(teamPermissions.links), linkController.deleteLink);
router.get("/get_link_by_url", linkController.getLinkByUrl);

module.exports = router;
1 change: 1 addition & 0 deletions backend/src/routes/popup.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ router.put("/edit_popup/:id", authenticateJWT, accessGuard(teamPermissions.popup
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);

module.exports = router;
21 changes: 7 additions & 14 deletions backend/src/routes/tour.routes.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
const express = require("express");
const tourController = require("../controllers/tour.controller");
const authenticateJWT = require("../middleware/auth.middleware");
const settings = require('../../config/settings');
const accessGuard = require('../middleware/accessGuard.middleware');

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

// Create a new tour
router.post("/add_tour", authenticateJWT, tourController.addTour);

// Delete a tour by ID
router.delete("/delete_tour/:id", authenticateJWT, tourController.deleteTour);

// Edit a tour by ID
router.put("/edit_tour/:id", authenticateJWT, tourController.editTour);

// Get all tours
router.post("/add_tour", authenticateJWT, accessGuard(teamPermissions.tours), tourController.addTour);
router.delete("/delete_tour/:id", authenticateJWT, accessGuard(teamPermissions.tours), tourController.deleteTour);
router.put("/edit_tour/:id", authenticateJWT, accessGuard(teamPermissions.tours), tourController.editTour);
router.get("/all_tours", authenticateJWT, tourController.getAllTours);

// Get tours created by the authenticated user
router.get("/tours", authenticateJWT, tourController.getTours);

// Get a specific tour by ID
router.get("/get_tour/:id", authenticateJWT, tourController.getTourById);
// router.get("/get_tour_by_url", tourController.getTourByUrl);
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

module.exports = router;
Loading
Loading