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