update status-page backend
This commit is contained in:
1 parent
27ec0eabb2
commit
d853c50840
5 files changed
+68
-24
No files matched your search
@@ -2,6 +2,7 @@ import { handleError, handleValidationError } from "./controllerUtils.js";
|
|||||||
import {
|
import {
|
||||||
createStatusPageBodyValidation,
|
createStatusPageBodyValidation,
|
||||||
getStatusPageParamValidation,
|
getStatusPageParamValidation,
|
||||||
|
imageValidation,
|
||||||
} from "../validation/joi.js";
|
} from "../validation/joi.js";
|
||||||
import { successMessages } from "../utils/messages.js";
|
import { successMessages } from "../utils/messages.js";
|
||||||
|
|
||||||
@@ -15,13 +16,14 @@ class StatusPageController {
|
|||||||
createStatusPage = async (req, res, next) => {
|
createStatusPage = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await createStatusPageBodyValidation.validateAsync(req.body);
|
await createStatusPageBodyValidation.validateAsync(req.body);
|
||||||
|
await imageValidation.validateAsync(req.file);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const statusPage = await this.db.createStatusPage(req.body);
|
const statusPage = await this.db.createStatusPage(req.body, req.file);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.STATUS_PAGE_CREATE,
|
msg: successMessages.STATUS_PAGE_CREATE,
|
||||||
data: statusPage,
|
data: statusPage,
|
||||||
@@ -30,17 +32,9 @@ class StatusPageController {
|
|||||||
next(handleError(error, SERVICE_NAME, "createStatusPage"));
|
next(handleError(error, SERVICE_NAME, "createStatusPage"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
getStatusPageByUrl = async (req, res, next) => {
|
getStatusPage = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await getStatusPageParamValidation.validateAsync(req.params);
|
const statusPage = await this.db.getStatusPage();
|
||||||
} catch (error) {
|
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { url } = req.params;
|
|
||||||
const statusPage = await this.db.getStatusPageByUrl(url);
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: successMessages.STATUS_PAGE_BY_URL,
|
msg: successMessages.STATUS_PAGE_BY_URL,
|
||||||
data: statusPage,
|
data: statusPage,
|
||||||
|
|||||||
@@ -23,11 +23,6 @@ const StatusPageSchema = mongoose.Schema(
|
|||||||
required: true,
|
required: true,
|
||||||
default: "#4169E1",
|
default: "#4169E1",
|
||||||
},
|
},
|
||||||
theme: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
default: "light",
|
|
||||||
},
|
|
||||||
monitors: [
|
monitors: [
|
||||||
{
|
{
|
||||||
type: mongoose.Schema.Types.ObjectId,
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
@@ -35,6 +30,22 @@ const StatusPageSchema = mongoose.Schema(
|
|||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
logo: {
|
||||||
|
data: Buffer,
|
||||||
|
contentType: String,
|
||||||
|
},
|
||||||
|
isPublished: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
|
showCharts: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true,
|
||||||
|
},
|
||||||
|
showUptimePercentage: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,9 +3,15 @@ import { errorMessages } from "../../../utils/messages.js";
|
|||||||
|
|
||||||
const SERVICE_NAME = "statusPageModule";
|
const SERVICE_NAME = "statusPageModule";
|
||||||
|
|
||||||
const createStatusPage = async (statusPageData) => {
|
const createStatusPage = async (statusPageData, image) => {
|
||||||
try {
|
try {
|
||||||
const statusPage = new StatusPage({ ...statusPageData });
|
const statusPage = new StatusPage({ ...statusPageData });
|
||||||
|
if (image) {
|
||||||
|
statusPage.logo = {
|
||||||
|
data: image.buffer,
|
||||||
|
contentType: image.mimetype,
|
||||||
|
};
|
||||||
|
}
|
||||||
await statusPage.save();
|
await statusPage.save();
|
||||||
return statusPage;
|
return statusPage;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -20,9 +26,9 @@ const createStatusPage = async (statusPageData) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStatusPageByUrl = async (url) => {
|
const getStatusPage = async () => {
|
||||||
try {
|
try {
|
||||||
const statusPage = await StatusPage.findOne({ url });
|
const statusPage = await StatusPage.findOne();
|
||||||
if (statusPage === null || statusPage === undefined) {
|
if (statusPage === null || statusPage === undefined) {
|
||||||
const error = new Error(errorMessages.STATUS_PAGE_NOT_FOUND);
|
const error = new Error(errorMessages.STATUS_PAGE_NOT_FOUND);
|
||||||
error.status = 404;
|
error.status = 404;
|
||||||
@@ -37,4 +43,4 @@ const getStatusPageByUrl = async (url) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export { createStatusPage, getStatusPageByUrl };
|
export { createStatusPage, getStatusPage };
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
|
|
||||||
import { verifyJWT } from "../middleware/verifyJWT.js";
|
import { verifyJWT } from "../middleware/verifyJWT.js";
|
||||||
|
import multer from "multer";
|
||||||
|
const upload = multer();
|
||||||
|
|
||||||
class StatusPageRoutes {
|
class StatusPageRoutes {
|
||||||
constructor(statusPageController) {
|
constructor(statusPageController) {
|
||||||
@@ -10,8 +11,13 @@ class StatusPageRoutes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
initRoutes() {
|
initRoutes() {
|
||||||
this.router.get("/:url", this.statusPageController.getStatusPageByUrl);
|
this.router.get("/", this.statusPageController.getStatusPage);
|
||||||
this.router.post("/:url", verifyJWT, this.statusPageController.createStatusPage);
|
this.router.post(
|
||||||
|
"/",
|
||||||
|
upload.single("logo"),
|
||||||
|
verifyJWT,
|
||||||
|
this.statusPageController.createStatusPage
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getRouter() {
|
getRouter() {
|
||||||
|
|||||||
@@ -423,7 +423,6 @@ const createStatusPageBodyValidation = joi.object({
|
|||||||
url: joi.string().required(),
|
url: joi.string().required(),
|
||||||
timezone: joi.string().required(),
|
timezone: joi.string().required(),
|
||||||
color: joi.string().required(),
|
color: joi.string().required(),
|
||||||
theme: joi.string().required(),
|
|
||||||
monitors: joi
|
monitors: joi
|
||||||
.array()
|
.array()
|
||||||
.items(joi.string().pattern(/^[0-9a-fA-F]{24}$/))
|
.items(joi.string().pattern(/^[0-9a-fA-F]{24}$/))
|
||||||
@@ -434,8 +433,35 @@ const createStatusPageBodyValidation = joi.object({
|
|||||||
"array.empty": "At least one monitor is required",
|
"array.empty": "At least one monitor is required",
|
||||||
"any.required": "Monitors are required",
|
"any.required": "Monitors are required",
|
||||||
}),
|
}),
|
||||||
|
isPublished: joi.boolean(),
|
||||||
|
showCharts: joi.boolean(),
|
||||||
|
showUptimePercentage: joi.boolean(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const imageValidation = joi
|
||||||
|
.object({
|
||||||
|
fieldname: joi.string().required(),
|
||||||
|
originalname: joi.string().required(),
|
||||||
|
encoding: joi.string().required(),
|
||||||
|
mimetype: joi
|
||||||
|
.string()
|
||||||
|
.valid("image/jpeg", "image/png", "image/jpg")
|
||||||
|
.required()
|
||||||
|
.messages({
|
||||||
|
"string.valid": "File must be a valid image (jpeg, jpg, or png)",
|
||||||
|
}),
|
||||||
|
size: joi.number().max(3145728).required().messages({
|
||||||
|
"number.max": "File size must be less than 3MB",
|
||||||
|
}),
|
||||||
|
buffer: joi.binary().required(),
|
||||||
|
destination: joi.string(),
|
||||||
|
filename: joi.string(),
|
||||||
|
path: joi.string(),
|
||||||
|
})
|
||||||
|
.messages({
|
||||||
|
"any.required": "Image file is required",
|
||||||
|
});
|
||||||
|
|
||||||
export {
|
export {
|
||||||
roleValidatior,
|
roleValidatior,
|
||||||
loginValidation,
|
loginValidation,
|
||||||
@@ -493,4 +519,5 @@ export {
|
|||||||
updateAppSettingsBodyValidation,
|
updateAppSettingsBodyValidation,
|
||||||
createStatusPageBodyValidation,
|
createStatusPageBodyValidation,
|
||||||
getStatusPageParamValidation,
|
getStatusPageParamValidation,
|
||||||
|
imageValidation,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user