Implement inserting notifications when a monitor is created, update validation
This commit is contained in:
1 parent
40c550e81f
commit
09edaebd98
3 files changed
+17
-5
No files matched your search
@@ -2,13 +2,14 @@ const {
|
|||||||
getMonitorByIdParamValidation,
|
getMonitorByIdParamValidation,
|
||||||
getMonitorByIdQueryValidation,
|
getMonitorByIdQueryValidation,
|
||||||
getMonitorsByUserIdValidation,
|
getMonitorsByUserIdValidation,
|
||||||
monitorValidation,
|
createMonitorBodyValidation,
|
||||||
editMonitorBodyValidation,
|
editMonitorBodyValidation,
|
||||||
getMonitorsByUserIdQueryValidation,
|
getMonitorsByUserIdQueryValidation,
|
||||||
} = require("../validation/joi");
|
} = require("../validation/joi");
|
||||||
|
|
||||||
const SERVICE_NAME = "monitorController";
|
const SERVICE_NAME = "monitorController";
|
||||||
const { errorMessages, successMessages } = require("../utils/messages");
|
const { errorMessages, successMessages } = require("../utils/messages");
|
||||||
|
const { runInNewContext } = require("vm");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all monitors
|
* Returns all monitors
|
||||||
@@ -118,7 +119,7 @@ const getMonitorsByUserId = async (req, res, next) => {
|
|||||||
|
|
||||||
const createMonitor = async (req, res, next) => {
|
const createMonitor = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
await monitorValidation.validateAsync(req.body);
|
await createMonitorBodyValidation.validateAsync(req.body);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.status = 422;
|
error.status = 422;
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
@@ -130,7 +131,16 @@ const createMonitor = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const notifications = req.body.notifications;
|
||||||
const monitor = await req.db.createMonitor(req, res);
|
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
|
// Add monitor to job queue
|
||||||
req.jobQueue.addJob(monitor._id, monitor);
|
req.jobQueue.addJob(monitor._id, monitor);
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ const getAllMonitors = async (req, res) => {
|
|||||||
*/
|
*/
|
||||||
const getMonitorById = async (req, res) => {
|
const getMonitorById = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const { monitorId } = req.params;
|
const { monitorId } = req.params;
|
||||||
let { status, limit, sortOrder } = req.query;
|
let { status, limit, sortOrder } = req.query;
|
||||||
|
|
||||||
@@ -140,6 +139,8 @@ const getMonitorsByUserId = async (req, res) => {
|
|||||||
const createMonitor = async (req, res) => {
|
const createMonitor = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const monitor = new Monitor({ ...req.body });
|
const monitor = new Monitor({ ...req.body });
|
||||||
|
// Remove notifications fom monitor as they aren't needed here
|
||||||
|
delete monitor.notifications;
|
||||||
monitor.userId = req.user._id;
|
monitor.userId = req.user._id;
|
||||||
await monitor.save();
|
await monitor.save();
|
||||||
return monitor;
|
return monitor;
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ const getMonitorsByUserIdQueryValidation = joi.object({
|
|||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
const monitorValidation = joi.object({
|
const createMonitorBodyValidation = joi.object({
|
||||||
_id: joi.string(),
|
_id: joi.string(),
|
||||||
userId: joi.string().required(),
|
userId: joi.string().required(),
|
||||||
name: joi.string().required(),
|
name: joi.string().required(),
|
||||||
@@ -153,6 +153,7 @@ const monitorValidation = joi.object({
|
|||||||
url: joi.string().required(),
|
url: joi.string().required(),
|
||||||
isActive: joi.boolean(),
|
isActive: joi.boolean(),
|
||||||
interval: joi.number(),
|
interval: joi.number(),
|
||||||
|
notifications: joi.array().items(joi.object()),
|
||||||
});
|
});
|
||||||
|
|
||||||
const editMonitorBodyValidation = joi.object({
|
const editMonitorBodyValidation = joi.object({
|
||||||
@@ -286,7 +287,7 @@ module.exports = {
|
|||||||
inviteRoleValidation,
|
inviteRoleValidation,
|
||||||
inviteBodyValidation,
|
inviteBodyValidation,
|
||||||
inviteVerifciationBodyValidation,
|
inviteVerifciationBodyValidation,
|
||||||
monitorValidation,
|
createMonitorBodyValidation,
|
||||||
getMonitorByIdParamValidation,
|
getMonitorByIdParamValidation,
|
||||||
getMonitorByIdQueryValidation,
|
getMonitorByIdQueryValidation,
|
||||||
getMonitorsByUserIdValidation,
|
getMonitorsByUserIdValidation,
|
||||||
|
|||||||
Reference in new issue
Block a user