add route, controller, and db method for getting all status pages by ID

This commit is contained in:
Alex Holliday committed 2025-02-10 13:41:21 -08:00
1 parent f78343d03f
commit 48cfeb1558
6 files changed
+64 -9

No files matched your search

+16 -1
View File
@@ -2,6 +2,7 @@ import { handleError, handleValidationError } from "./controllerUtils.js";
import {
createStatusPageBodyValidation,
getStatusPageParamValidation,
getStatusPageQueryValidation,
imageValidation,
} from "../validation/joi.js";
import { successMessages, errorMessages } from "../utils/messages.js";
@@ -73,13 +74,14 @@ class StatusPageController {
getStatusPageByUrl = async (req, res, next) => {
try {
await getStatusPageParamValidation.validateAsync(req.params);
await getStatusPageQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const statusPage = await this.db.getStatusPageByUrl(req.params.url);
const statusPage = await this.db.getStatusPageByUrl(req.params.url, req.query.type);
return res.success({
msg: successMessages.STATUS_PAGE_BY_URL,
data: statusPage,
@@ -89,6 +91,19 @@ class StatusPageController {
}
};
getStatusPagesByTeamId = async (req, res, next) => {
try {
const teamId = req.params.teamId;
const statusPages = await this.db.getStatusPagesByTeamId(teamId);
return res.success({
msg: successMessages.STATUS_PAGE_BY_TEAM_ID,
data: statusPages,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "getStatusPageByUrl"));
}
};
deleteStatusPage = async (req, res, next) => {
try {
await this.db.deleteStatusPage(req.params.url);
+6
View File
@@ -14,6 +14,12 @@ const StatusPageSchema = mongoose.Schema(
immutable: true,
required: true,
},
type: {
type: String,
required: true,
default: "uptime",
enum: ["uptime", "distributed"],
},
companyName: {
type: String,
required: true,
+33 -8
View File
@@ -35,9 +35,13 @@ const updateStatusPage = async (statusPageData, image) => {
contentType: image.mimetype,
};
}
const statusPage = await StatusPage.findOneAndUpdate({}, statusPageData, {
new: true,
});
const statusPage = await StatusPage.findOneAndUpdate(
{ url: statusPageData.url },
statusPageData,
{
new: true,
}
);
return statusPage;
} catch (error) {
@@ -47,10 +51,14 @@ const updateStatusPage = async (statusPageData, image) => {
}
};
const getStatusPageByUrl = async (url) => {
const getStatusPageByUrl = async (url, type) => {
try {
const statusPage = await StatusPage.aggregate([{ $match: { url } }]);
return statusPage[0];
if (type === "distributed") {
const statusPage = await StatusPage.aggregate([{ $match: { url } }]);
return statusPage[0];
} else {
return getStatusPage(url);
}
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getStatusPageByUrl";
@@ -58,10 +66,26 @@ const getStatusPageByUrl = async (url) => {
}
};
const getStatusPage = async () => {
const getStatusPagesByTeamId = async (teamId) => {
try {
const statusPages = await StatusPage.find({ teamId });
if (statusPages.length === 0) {
const error = new Error(errorMessages.STATUS_PAGE_NOT_FOUND);
error.status = 404;
throw error;
}
return statusPages;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getStatusPagesByTeamId";
throw error;
}
};
const getStatusPage = async (url) => {
try {
const statusPageQuery = await StatusPage.aggregate([
{ $match: { url: "/status/public" } },
{ $match: { url: url } },
{
$set: {
originalMonitors: "$monitors",
@@ -167,6 +191,7 @@ const deleteStatusPage = async (url) => {
export {
createStatusPage,
updateStatusPage,
getStatusPagesByTeamId,
getStatusPage,
getStatusPageByUrl,
deleteStatusPage,
+2
View File
@@ -13,6 +13,8 @@ class StatusPageRoutes {
initRoutes() {
this.router.get("/", this.statusPageController.getStatusPage);
this.router.get("/:url", this.statusPageController.getStatusPageByUrl);
this.router.get("/team/:teamId", this.statusPageController.getStatusPagesByTeamId);
this.router.post(
"/",
upload.single("logo"),
+1
View File
@@ -139,6 +139,7 @@ const successMessages = {
STATUS_PAGE_CREATE: "Status page created successfully",
STATUS_PAGE_DELETE: "Status page deleted successfully",
STATUS_PAGE_UPDATE: "Status page updated successfully",
STATUS_PAGE_BY_TEAM_ID: "Got status pages by team id successfully",
// Docker
DOCKER_SUCCESS: "Docker container status fetched successfully",
+6
View File
@@ -418,9 +418,14 @@ const getStatusPageParamValidation = joi.object({
url: joi.string().required(),
});
const getStatusPageQueryValidation = joi.object({
type: joi.string().valid("uptime", "distributed").required(),
});
const createStatusPageBodyValidation = joi.object({
userId: joi.string().required(),
teamId: joi.string().required(),
type: joi.string().valid("uptime", "distributed").required(),
companyName: joi.string().required(),
url: joi.string().required(),
timezone: joi.string().optional(),
@@ -521,5 +526,6 @@ export {
updateAppSettingsBodyValidation,
createStatusPageBodyValidation,
getStatusPageParamValidation,
getStatusPageQueryValidation,
imageValidation,
};