mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-05-20 08:28:48 -05:00
update backend for inserting maintenance windows
This commit is contained in:
@@ -1,17 +1,15 @@
|
||||
const {
|
||||
createMaintenanceWindowParamValidation,
|
||||
createMaintenanceWindowBodyValidation,
|
||||
getMaintenanceWindowsByUserIdParamValidation,
|
||||
getMaintenanceWindowsByMonitorIdParamValidation,
|
||||
} = require("../validation/joi");
|
||||
|
||||
const {successMessages} = require("../utils/messages")
|
||||
const { successMessages } = require("../utils/messages");
|
||||
|
||||
const SERVICE_NAME = "maintenanceWindowController";
|
||||
|
||||
const createMaintenanceWindow = async (req, res, next) => {
|
||||
const createMaintenanceWindows = async (req, res, next) => {
|
||||
try {
|
||||
await createMaintenanceWindowParamValidation.validateAsync(req.params);
|
||||
await createMaintenanceWindowBodyValidation.validateAsync(req.body);
|
||||
} catch (error) {
|
||||
error.status = 422;
|
||||
@@ -21,23 +19,21 @@ const createMaintenanceWindow = async (req, res, next) => {
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const data = {
|
||||
monitorId: req.params.monitorId,
|
||||
...req.body,
|
||||
};
|
||||
|
||||
if (data.oneTime === true) {
|
||||
data.expiry = data.end;
|
||||
}
|
||||
|
||||
const maintenanceWindow = await req.db.createMaintenanceWindow(data);
|
||||
|
||||
const monitorIds = req.body.monitors;
|
||||
const dbTransactions = monitorIds.map((monitorId) => {
|
||||
return req.db.createMaintenanceWindow({
|
||||
monitorId,
|
||||
active: req.body.active ? req.body.active : true,
|
||||
repeat: req.body.repeat,
|
||||
start: req.body.start,
|
||||
end: req.body.end,
|
||||
});
|
||||
});
|
||||
await Promise.all(dbTransactions);
|
||||
return res.status(201).json({
|
||||
success: true,
|
||||
msg: successMessages.MAINTENANCE_WINDOW_CREATE,
|
||||
data: maintenanceWindow,
|
||||
});
|
||||
} catch (error) {
|
||||
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||
@@ -113,7 +109,7 @@ const getMaintenanceWindowsByMonitorId = async (req, res, next) => {
|
||||
}
|
||||
};
|
||||
module.exports = {
|
||||
createMaintenanceWindow,
|
||||
createMaintenanceWindows,
|
||||
getMaintenanceWindowsByUserId,
|
||||
getMaintenanceWindowsByMonitorId,
|
||||
};
|
||||
|
||||
@@ -3,18 +3,14 @@ const maintenanceWindowController = require("../controllers/maintenanceWindowCon
|
||||
const { verifyOwnership } = require("../middleware/verifyOwnership");
|
||||
const Monitor = require("../db/models/Monitor");
|
||||
|
||||
router.post("/", maintenanceWindowController.createMaintenanceWindows);
|
||||
|
||||
router.get(
|
||||
"/monitor/:monitorId",
|
||||
verifyOwnership(Monitor, "monitorId"),
|
||||
maintenanceWindowController.getMaintenanceWindowsByMonitorId
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/monitor/:monitorId",
|
||||
verifyOwnership(Monitor, "monitorId"),
|
||||
maintenanceWindowController.createMaintenanceWindow
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/user/:userId",
|
||||
maintenanceWindowController.getMaintenanceWindowsByUserId
|
||||
|
||||
@@ -361,16 +361,14 @@ const deletePageSpeedCheckParamValidation = joi.object({
|
||||
//****************************************
|
||||
// MaintenanceWindowValidation
|
||||
//****************************************
|
||||
const createMaintenanceWindowParamValidation = joi.object({
|
||||
monitorId: joi.string().required(),
|
||||
});
|
||||
|
||||
const createMaintenanceWindowBodyValidation = joi.object({
|
||||
monitors: joi.array().items(joi.string()).required(),
|
||||
userId: joi.string().required(),
|
||||
active: joi.boolean().required(),
|
||||
oneTime: joi.boolean().required(),
|
||||
active: joi.boolean(),
|
||||
start: joi.date().required(),
|
||||
end: joi.date().required(),
|
||||
repeat: joi.number().required(),
|
||||
expiry: joi.date(),
|
||||
});
|
||||
|
||||
@@ -447,7 +445,6 @@ module.exports = {
|
||||
createPageSpeedCheckParamValidation,
|
||||
deletePageSpeedCheckParamValidation,
|
||||
createPageSpeedCheckBodyValidation,
|
||||
createMaintenanceWindowParamValidation,
|
||||
createMaintenanceWindowBodyValidation,
|
||||
getMaintenanceWindowsByUserIdParamValidation,
|
||||
getMaintenanceWindowsByMonitorIdParamValidation,
|
||||
|
||||
Reference in New Issue
Block a user