add route, controller and DB methods for updating status page

This commit is contained in:
Alex Holliday
2025-02-05 10:55:08 -08:00
parent ff47af95fa
commit 76b21f1d8c
4 changed files with 60 additions and 2 deletions

View File

@@ -33,6 +33,26 @@ class StatusPageController {
}
};
updateStatusPage = async (req, res, next) => {
try {
await createStatusPageBodyValidation.validateAsync(req.body);
await imageValidation.validateAsync(req.file);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
const statusPage = await this.db.updateStatusPage(req.body, req.file);
return res.success({
msg: successMessages.STATUS_PAGE_UPDATE,
data: statusPage,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "updateStatusPage"));
}
};
getStatusPage = async (req, res, next) => {
try {
const statusPage = await this.db.getStatusPage();

View File

@@ -27,6 +27,26 @@ const createStatusPage = async (statusPageData, image) => {
}
};
const updateStatusPage = async (statusPageData, image) => {
try {
if (image) {
statusPageData.logo = {
data: image.buffer,
contentType: image.mimetype,
};
}
const statusPage = await StatusPage.findOneAndUpdate({}, statusPageData, {
new: true,
});
return statusPage;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "updateStatusPage";
throw error;
}
};
const getStatusPage = async () => {
try {
const statusPageQuery = await StatusPage.aggregate([
@@ -79,7 +99,18 @@ const getStatusPage = async () => {
},
{
$project: {
statusPage: 1,
statusPage: {
_id: 1,
color: 1,
companyName: 1,
isPublished: 1,
logo: 1,
originalMonitors: 1,
showCharts: 1,
showUptimePercentage: 1,
timezone: 1,
url: 1,
},
monitors: {
$sortArray: {
input: "$monitors",
@@ -122,4 +153,4 @@ const deleteStatusPage = async () => {
}
};
export { createStatusPage, getStatusPage, deleteStatusPage };
export { createStatusPage, updateStatusPage, getStatusPage, deleteStatusPage };

View File

@@ -18,6 +18,12 @@ class StatusPageRoutes {
verifyJWT,
this.statusPageController.createStatusPage
);
this.router.put(
"/",
upload.single("logo"),
verifyJWT,
this.statusPageController.updateStatusPage
);
this.router.delete("/", verifyJWT, this.statusPageController.deleteStatusPage);
}

View File

@@ -137,6 +137,7 @@ const successMessages = {
STATUS_PAGE_BY_URL: "Got status page by url successfully",
STATUS_PAGE_CREATE: "Status page created successfully",
STATUS_PAGE_DELETE: "Status page deleted successfully",
STATUS_PAGE_UPDATE: "Status page updated successfully",
// Docker
DOCKER_SUCCESS: "Docker container status fetched successfully",