add route, controller, and db method for getting all status pages by ID
This commit is contained in:
1 parent
f78343d03f
commit
48cfeb1558
6 files changed
+64
-9
No files matched your search
@@ -2,6 +2,7 @@ import { handleError, handleValidationError } from "./controllerUtils.js";
|
|||||||
import {
|
import {
|
||||||
createStatusPageBodyValidation,
|
createStatusPageBodyValidation,
|
||||||
getStatusPageParamValidation,
|
getStatusPageParamValidation,
|
||||||
|
getStatusPageQueryValidation,
|
||||||
imageValidation,
|
imageValidation,
|
||||||
} from "../validation/joi.js";
|
} from "../validation/joi.js";
|
||||||
import { successMessages, errorMessages } from "../utils/messages.js";
|
import { successMessages, errorMessages } from "../utils/messages.js";
|
||||||
@@ -73,13 +74,14 @@ class StatusPageController {
|
|||||||
getStatusPageByUrl = async (req, res, next) => {
|
getStatusPageByUrl = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getStatusPageParamValidation.validateAsync(req.params);
|
await getStatusPageParamValidation.validateAsync(req.params);
|
||||||
|
await getStatusPageQueryValidation.validateAsync(req.query);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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({
|
return res.success({
|
||||||
msg: successMessages.STATUS_PAGE_BY_URL,
|
msg: successMessages.STATUS_PAGE_BY_URL,
|
||||||
data: statusPage,
|
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) => {
|
deleteStatusPage = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await this.db.deleteStatusPage(req.params.url);
|
await this.db.deleteStatusPage(req.params.url);
|
||||||
|
|||||||
@@ -14,6 +14,12 @@ const StatusPageSchema = mongoose.Schema(
|
|||||||
immutable: true,
|
immutable: true,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
type: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
default: "uptime",
|
||||||
|
enum: ["uptime", "distributed"],
|
||||||
|
},
|
||||||
companyName: {
|
companyName: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
|
|||||||
@@ -35,9 +35,13 @@ const updateStatusPage = async (statusPageData, image) => {
|
|||||||
contentType: image.mimetype,
|
contentType: image.mimetype,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const statusPage = await StatusPage.findOneAndUpdate({}, statusPageData, {
|
const statusPage = await StatusPage.findOneAndUpdate(
|
||||||
new: true,
|
{ url: statusPageData.url },
|
||||||
});
|
statusPageData,
|
||||||
|
{
|
||||||
|
new: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return statusPage;
|
return statusPage;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -47,10 +51,14 @@ const updateStatusPage = async (statusPageData, image) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStatusPageByUrl = async (url) => {
|
const getStatusPageByUrl = async (url, type) => {
|
||||||
try {
|
try {
|
||||||
const statusPage = await StatusPage.aggregate([{ $match: { url } }]);
|
if (type === "distributed") {
|
||||||
return statusPage[0];
|
const statusPage = await StatusPage.aggregate([{ $match: { url } }]);
|
||||||
|
return statusPage[0];
|
||||||
|
} else {
|
||||||
|
return getStatusPage(url);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "getStatusPageByUrl";
|
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 {
|
try {
|
||||||
const statusPageQuery = await StatusPage.aggregate([
|
const statusPageQuery = await StatusPage.aggregate([
|
||||||
{ $match: { url: "/status/public" } },
|
{ $match: { url: url } },
|
||||||
{
|
{
|
||||||
$set: {
|
$set: {
|
||||||
originalMonitors: "$monitors",
|
originalMonitors: "$monitors",
|
||||||
@@ -167,6 +191,7 @@ const deleteStatusPage = async (url) => {
|
|||||||
export {
|
export {
|
||||||
createStatusPage,
|
createStatusPage,
|
||||||
updateStatusPage,
|
updateStatusPage,
|
||||||
|
getStatusPagesByTeamId,
|
||||||
getStatusPage,
|
getStatusPage,
|
||||||
getStatusPageByUrl,
|
getStatusPageByUrl,
|
||||||
deleteStatusPage,
|
deleteStatusPage,
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ class StatusPageRoutes {
|
|||||||
initRoutes() {
|
initRoutes() {
|
||||||
this.router.get("/", this.statusPageController.getStatusPage);
|
this.router.get("/", this.statusPageController.getStatusPage);
|
||||||
this.router.get("/:url", this.statusPageController.getStatusPageByUrl);
|
this.router.get("/:url", this.statusPageController.getStatusPageByUrl);
|
||||||
|
this.router.get("/team/:teamId", this.statusPageController.getStatusPagesByTeamId);
|
||||||
|
|
||||||
this.router.post(
|
this.router.post(
|
||||||
"/",
|
"/",
|
||||||
upload.single("logo"),
|
upload.single("logo"),
|
||||||
|
|||||||
@@ -139,6 +139,7 @@ const successMessages = {
|
|||||||
STATUS_PAGE_CREATE: "Status page created successfully",
|
STATUS_PAGE_CREATE: "Status page created successfully",
|
||||||
STATUS_PAGE_DELETE: "Status page deleted successfully",
|
STATUS_PAGE_DELETE: "Status page deleted successfully",
|
||||||
STATUS_PAGE_UPDATE: "Status page updated successfully",
|
STATUS_PAGE_UPDATE: "Status page updated successfully",
|
||||||
|
STATUS_PAGE_BY_TEAM_ID: "Got status pages by team id successfully",
|
||||||
// Docker
|
// Docker
|
||||||
DOCKER_SUCCESS: "Docker container status fetched successfully",
|
DOCKER_SUCCESS: "Docker container status fetched successfully",
|
||||||
|
|
||||||
|
|||||||
@@ -418,9 +418,14 @@ const getStatusPageParamValidation = joi.object({
|
|||||||
url: joi.string().required(),
|
url: joi.string().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const getStatusPageQueryValidation = joi.object({
|
||||||
|
type: joi.string().valid("uptime", "distributed").required(),
|
||||||
|
});
|
||||||
|
|
||||||
const createStatusPageBodyValidation = joi.object({
|
const createStatusPageBodyValidation = joi.object({
|
||||||
userId: joi.string().required(),
|
userId: joi.string().required(),
|
||||||
teamId: joi.string().required(),
|
teamId: joi.string().required(),
|
||||||
|
type: joi.string().valid("uptime", "distributed").required(),
|
||||||
companyName: joi.string().required(),
|
companyName: joi.string().required(),
|
||||||
url: joi.string().required(),
|
url: joi.string().required(),
|
||||||
timezone: joi.string().optional(),
|
timezone: joi.string().optional(),
|
||||||
@@ -521,5 +526,6 @@ export {
|
|||||||
updateAppSettingsBodyValidation,
|
updateAppSettingsBodyValidation,
|
||||||
createStatusPageBodyValidation,
|
createStatusPageBodyValidation,
|
||||||
getStatusPageParamValidation,
|
getStatusPageParamValidation,
|
||||||
|
getStatusPageQueryValidation,
|
||||||
imageValidation,
|
imageValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user