mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-12 20:59:41 -06:00
Implement inserting notifications when a monitor is created, update validation
This commit is contained in:
@@ -2,13 +2,14 @@ const {
|
||||
getMonitorByIdParamValidation,
|
||||
getMonitorByIdQueryValidation,
|
||||
getMonitorsByUserIdValidation,
|
||||
monitorValidation,
|
||||
createMonitorBodyValidation,
|
||||
editMonitorBodyValidation,
|
||||
getMonitorsByUserIdQueryValidation,
|
||||
} = require("../validation/joi");
|
||||
|
||||
const SERVICE_NAME = "monitorController";
|
||||
const { errorMessages, successMessages } = require("../utils/messages");
|
||||
const { runInNewContext } = require("vm");
|
||||
|
||||
/**
|
||||
* Returns all monitors
|
||||
@@ -118,7 +119,7 @@ const getMonitorsByUserId = async (req, res, next) => {
|
||||
|
||||
const createMonitor = async (req, res, next) => {
|
||||
try {
|
||||
await monitorValidation.validateAsync(req.body);
|
||||
await createMonitorBodyValidation.validateAsync(req.body);
|
||||
} catch (error) {
|
||||
error.status = 422;
|
||||
error.service = SERVICE_NAME;
|
||||
@@ -130,7 +131,16 @@ const createMonitor = async (req, res, next) => {
|
||||
}
|
||||
|
||||
try {
|
||||
const notifications = req.body.notifications;
|
||||
const monitor = await req.db.createMonitor(req, res);
|
||||
|
||||
await Promise.all(
|
||||
notifications.map(async (notification) => {
|
||||
notification.monitorId = monitor._id;
|
||||
await req.db.createNotification(notification);
|
||||
})
|
||||
);
|
||||
|
||||
// Add monitor to job queue
|
||||
req.jobQueue.addJob(monitor._id, monitor);
|
||||
return res.status(201).json({
|
||||
|
||||
@@ -30,7 +30,6 @@ const getAllMonitors = async (req, res) => {
|
||||
*/
|
||||
const getMonitorById = async (req, res) => {
|
||||
try {
|
||||
|
||||
const { monitorId } = req.params;
|
||||
let { status, limit, sortOrder } = req.query;
|
||||
|
||||
@@ -140,6 +139,8 @@ const getMonitorsByUserId = async (req, res) => {
|
||||
const createMonitor = async (req, res) => {
|
||||
try {
|
||||
const monitor = new Monitor({ ...req.body });
|
||||
// Remove notifications fom monitor as they aren't needed here
|
||||
delete monitor.notifications;
|
||||
monitor.userId = req.user._id;
|
||||
await monitor.save();
|
||||
return monitor;
|
||||
|
||||
@@ -144,7 +144,7 @@ const getMonitorsByUserIdQueryValidation = joi.object({
|
||||
),
|
||||
});
|
||||
|
||||
const monitorValidation = joi.object({
|
||||
const createMonitorBodyValidation = joi.object({
|
||||
_id: joi.string(),
|
||||
userId: joi.string().required(),
|
||||
name: joi.string().required(),
|
||||
@@ -153,6 +153,7 @@ const monitorValidation = joi.object({
|
||||
url: joi.string().required(),
|
||||
isActive: joi.boolean(),
|
||||
interval: joi.number(),
|
||||
notifications: joi.array().items(joi.object()),
|
||||
});
|
||||
|
||||
const editMonitorBodyValidation = joi.object({
|
||||
@@ -286,7 +287,7 @@ module.exports = {
|
||||
inviteRoleValidation,
|
||||
inviteBodyValidation,
|
||||
inviteVerifciationBodyValidation,
|
||||
monitorValidation,
|
||||
createMonitorBodyValidation,
|
||||
getMonitorByIdParamValidation,
|
||||
getMonitorByIdQueryValidation,
|
||||
getMonitorsByUserIdValidation,
|
||||
|
||||
Reference in New Issue
Block a user