2024-05-10 13:59:33 -07:00
const {
2024-07-26 18:56:39 -07:00
getMonitorByIdParamValidation ,
getMonitorByIdQueryValidation ,
2024-08-26 19:38:24 -07:00
getMonitorsByTeamIdValidation ,
2024-07-31 12:12:05 -07:00
createMonitorBodyValidation ,
2024-07-24 12:08:42 -07:00
editMonitorBodyValidation ,
2024-09-05 14:31:29 -07:00
getMonitorsAndSummaryByTeamIdParamValidation ,
2024-09-05 12:16:29 -07:00
getMonitorsAndSummaryByTeamIdQueryValidation ,
2024-08-26 19:38:24 -07:00
getMonitorsByTeamIdQueryValidation ,
2024-08-28 15:21:59 -07:00
pauseMonitorParamValidation ,
2024-09-03 15:07:11 -07:00
getMonitorStatsByIdParamValidation ,
getMonitorStatsByIdQueryValidation ,
2024-09-03 15:11:17 -07:00
getCertificateParamValidation ,
2024-05-10 13:59:33 -07:00
} = require ( "../validation/joi" );
2024-05-21 11:23:39 -07:00
2024-08-12 15:15:13 -07:00
const sslChecker = require ( "ssl-checker" );
2024-05-24 13:44:42 -07:00
const SERVICE_NAME = "monitorController" ;
2024-06-14 13:45:45 -07:00
const { errorMessages , successMessages } = require ( "../utils/messages" );
2024-09-11 11:03:39 +08:00
const jwt = require ( "jsonwebtoken" );
const { getTokenFromHeaders } = require ( "../utils/utils" );
2024-09-26 10:42:03 +08:00
const logger = require ( "../utils/logger" );
2024-09-11 11:03:39 +08:00
2024-05-21 10:11:16 -07:00
/**
* Returns all monitors
* @async
* @param {Express.Request} req
* @param {Express.Response} res
2024-10-01 10:29:32 +08:00
* @param {function} next
2024-05-21 10:11:16 -07:00
* @returns {Promise<Express.Response>}
* @throws {Error}
*/
2024-05-27 10:47:14 -07:00
const getAllMonitors = async ( req , res , next ) => {
2024-05-10 13:59:33 -07:00
try {
2024-05-13 14:21:27 -07:00
const monitors = await req . db . getAllMonitors ();
2024-06-14 13:45:45 -07:00
return res . json ({
success : true ,
msg : successMessages . MONITOR_GET_ALL ,
data : monitors ,
});
2024-05-10 13:59:33 -07:00
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "getAllMonitors" ) : null ;
2024-05-27 10:47:14 -07:00
next ( error );
2024-05-10 13:59:33 -07:00
}
2024-05-10 13:36:04 -07:00
};
2024-08-12 12:05:57 -07:00
/**
* Returns monitor stats for monitor with matching ID
* @async
* @param {Express.Request} req
* @param {Express.Response} res
2024-10-01 10:29:32 +08:00
* @param {function} next
2024-08-12 12:05:57 -07:00
* @returns {Promise<Express.Response>}
* @throws {Error}
*/
const getMonitorStatsById = async ( req , res , next ) => {
try {
2024-09-03 15:07:11 -07:00
await getMonitorStatsByIdParamValidation . validateAsync ( req . params );
await getMonitorStatsByIdQueryValidation . validateAsync ( req . query );
2024-08-12 12:05:57 -07:00
} catch ( error ) {
error . status = 422 ;
error . message =
error . details ? .[ 0 ] ? . message || error . message || "Validation Error" ;
next ( error );
return ;
}
try {
const monitorStats = await req . db . getMonitorStatsById ( req );
return res . json ({
success : true ,
2024-10-01 10:55:51 +08:00
msg : successMessages . MONITOR_STATS_BY_ID ,
2024-08-12 12:05:57 -07:00
data : monitorStats ,
});
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "getMonitorStatsById" ) : null ;
2024-08-12 12:05:57 -07:00
next ( error );
}
};
2024-08-12 15:15:13 -07:00
const getMonitorCertificate = async ( req , res , next ) => {
try {
2024-09-03 15:11:17 -07:00
await getCertificateParamValidation . validateAsync ( req . params );
2024-08-12 15:15:13 -07:00
} catch ( error ) {
error . status = 422 ;
error . message =
error . details ? .[ 0 ] ? . message || error . message || "Validation Error" ;
next ( error );
}
try {
2024-08-14 19:02:11 -07:00
const { monitorId } = req . params ;
const monitor = await req . db . getMonitorById ( monitorId );
2024-08-12 15:15:13 -07:00
const monitorUrl = new URL ( monitor . url );
const certificate = await sslChecker ( monitorUrl . hostname );
if ( certificate && certificate . validTo ) {
return res . json ({
success : true ,
msg : successMessages . MONITOR_CERTIFICATE ,
data : {
certificateDate : new Date ( certificate . validTo ). toLocaleDateString (),
},
});
} else {
return res . json ({
success : true ,
msg : successMessages . MONITOR_CERTIFICATE ,
data : { certificateDate : "N/A" },
});
}
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined
? ( error . method = "getMonitorCertificate" )
: null ;
2024-08-12 15:15:13 -07:00
next ( error );
}
};
2024-05-21 10:11:16 -07:00
/**
2024-10-01 10:29:32 +08:00
* Retrieves a monitor by its ID.
2024-05-21 10:11:16 -07:00
* @async
2024-10-01 10:29:32 +08:00
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.monitorId - The ID of the monitor to be retrieved.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message, and the retrieved monitor data.
* @throws {Error} If there is an error during the process, especially if the monitor is not found (404) or if there is a validation error (422).
2024-05-21 10:11:16 -07:00
*/
2024-05-27 10:47:14 -07:00
const getMonitorById = async ( req , res , next ) => {
try {
2024-07-26 18:56:39 -07:00
await getMonitorByIdParamValidation . validateAsync ( req . params );
await getMonitorByIdQueryValidation . validateAsync ( req . query );
2024-05-27 10:47:14 -07:00
} catch ( error ) {
error . status = 422 ;
2024-07-30 13:36:53 -07:00
error . message =
error . details ? .[ 0 ] ? . message || error . message || "Validation Error" ;
2024-05-27 10:47:14 -07:00
next ( error );
return ;
2024-05-10 13:36:04 -07:00
}
2024-05-10 13:59:33 -07:00
try {
2024-08-28 20:07:22 -07:00
const monitor = await req . db . getMonitorById ( req . params . monitorId );
2024-05-24 13:44:42 -07:00
if ( ! monitor ) {
2024-06-14 13:45:45 -07:00
const error = new Error ( errorMessages . MONITOR_GET_BY_ID );
2024-05-27 10:47:14 -07:00
error . status = 404 ;
2024-05-24 13:44:42 -07:00
}
2024-06-14 13:45:45 -07:00
return res . json ({
success : true ,
2024-10-01 10:55:51 +08:00
msg : successMessages . MONITOR_GET_BY_ID ,
2024-06-14 13:45:45 -07:00
data : monitor ,
});
2024-05-10 13:59:33 -07:00
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "getMonitorById" ) : null ;
2024-05-27 10:47:14 -07:00
next ( error );
2024-05-10 13:59:33 -07:00
}
};
2024-05-21 10:11:16 -07:00
/**
2024-10-01 10:29:32 +08:00
* Retrieves all monitors and a summary for a team based on the team ID.
2024-09-05 11:17:31 -07:00
* @async
2024-10-01 10:29:32 +08:00
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.teamId - The ID of the team.
* @property {Object} req.query - The query parameters of the request.
* @property {string} req.query.type - The type of the request.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message, and the data containing the monitors and summary for the team.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
2024-09-05 11:17:31 -07:00
*/
2024-09-05 12:16:29 -07:00
const getMonitorsAndSummaryByTeamId = async ( req , res , next ) => {
2024-09-05 11:17:31 -07:00
try {
2024-09-05 14:31:29 -07:00
await getMonitorsAndSummaryByTeamIdParamValidation . validateAsync (
2024-09-05 12:16:29 -07:00
req . params
);
await getMonitorsAndSummaryByTeamIdQueryValidation . validateAsync ( req . query );
2024-09-05 11:17:31 -07:00
//validation
} catch ( error ) {
error . status = 422 ;
error . service = SERVICE_NAME ;
2024-10-01 10:29:32 +08:00
error . method === undefined ? error . method = "getMonitorsAndSummaryByTeamId" : null ;
2024-09-05 11:17:31 -07:00
error . message =
error . details ? .[ 0 ] ? . message || error . message || "Validation Error" ;
next ( error );
return ;
}
try {
2024-09-05 12:16:29 -07:00
const { teamId } = req . params ;
const { type } = req . query ;
const monitorsSummary = await req . db . getMonitorsAndSummaryByTeamId (
teamId ,
type
);
return res . status ( 200 ). json ({
success : true ,
msg : successMessages . MONITOR_GET_BY_USER_ID ( teamId ),
data : monitorsSummary ,
});
2024-09-05 11:17:31 -07:00
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined
? ( error . method = "getMonitorsAndSummaryByTeamId" )
: null ;
2024-09-05 11:17:31 -07:00
next ( error );
}
};
/**
2024-10-01 10:29:32 +08:00
* Retrieves all monitors associated with a team by the team's ID.
2024-05-21 10:11:16 -07:00
* @async
2024-10-01 10:29:32 +08:00
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.teamId - The ID of the team.
* @property {Object} req.query - The query parameters of the request.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message, and the data containing the monitors for the team.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
2024-05-21 10:11:16 -07:00
*/
2024-08-26 19:38:24 -07:00
const getMonitorsByTeamId = async ( req , res , next ) => {
2024-05-10 13:59:33 -07:00
try {
2024-08-26 19:38:24 -07:00
await getMonitorsByTeamIdValidation . validateAsync ( req . params );
await getMonitorsByTeamIdQueryValidation . validateAsync ( req . query );
2024-07-30 13:36:53 -07:00
} catch ( error ) {
error . status = 422 ;
error . service = SERVICE_NAME ;
error . message =
error . details ? .[ 0 ] ? . message || error . message || "Validation Error" ;
next ( error );
return ;
}
try {
2024-08-26 19:38:24 -07:00
const teamId = req . params . teamId ;
const monitors = await req . db . getMonitorsByTeamId ( req , res );
2024-05-10 13:59:33 -07:00
return res . json ({
success : true ,
2024-08-26 19:38:24 -07:00
msg : successMessages . MONITOR_GET_BY_USER_ID ( teamId ),
2024-05-10 13:59:33 -07:00
data : monitors ,
});
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "getMonitorsByTeamId" ) : null ;
2024-05-27 10:47:14 -07:00
next ( error );
2024-05-10 13:59:33 -07:00
}
2024-05-10 13:36:04 -07:00
};
2024-05-21 11:23:39 -07:00
/**
2024-10-01 10:29:32 +08:00
* Creates a new monitor and adds it to the job queue.
2024-05-21 11:23:39 -07:00
* @async
2024-10-01 10:29:32 +08:00
* @param {Object} req - The Express request object.
* @property {Object} req.body - The body of the request.
* @property {Array} req.body.notifications - The notifications associated with the monitor.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message indicating the creation of the monitor, and the created monitor data.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
2024-05-21 11:23:39 -07:00
*/
2024-05-27 10:47:14 -07:00
const createMonitor = async ( req , res , next ) => {
try {
2024-07-31 12:12:05 -07:00
await createMonitorBodyValidation . validateAsync ( req . body );
2024-05-27 10:47:14 -07:00
} catch ( error ) {
error . status = 422 ;
error . service = SERVICE_NAME ;
2024-07-30 13:36:53 -07:00
error . message =
error . details ? .[ 0 ] ? . message || error . message || "Validation Error" ;
2024-05-27 10:47:14 -07:00
next ( error );
return ;
2024-05-21 11:23:39 -07:00
}
try {
2024-07-31 12:12:05 -07:00
const notifications = req . body . notifications ;
2024-05-21 11:23:39 -07:00
const monitor = await req . db . createMonitor ( req , res );
2024-07-31 12:12:05 -07:00
2024-07-31 13:11:20 -07:00
if ( notifications && notifications . length !== 0 ) {
2024-10-01 10:29:32 +08:00
monitor . notifications = await Promise . all (
notifications . map ( async ( notification ) => {
notification . monitorId = monitor . _id ;
await req . db . createNotification ( notification );
})
2024-07-31 13:11:20 -07:00
);
2024-08-30 18:00:04 -07:00
await monitor . save ();
2024-07-31 13:11:20 -07:00
}
2024-06-13 13:05:41 -07:00
// Add monitor to job queue
req . jobQueue . addJob ( monitor . _id , monitor );
2024-06-14 13:45:45 -07:00
return res . status ( 201 ). json ({
success : true ,
msg : successMessages . MONITOR_CREATE ,
data : monitor ,
});
2024-05-21 11:23:39 -07:00
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "createMonitor" ) : null ;
2024-05-27 10:47:14 -07:00
next ( error );
2024-05-21 11:23:39 -07:00
}
};
/**
2024-10-01 10:29:32 +08:00
* Deletes a monitor by its ID and also deletes associated checks, alerts, and notifications.
2024-05-21 11:23:39 -07:00
* @async
2024-10-01 10:29:32 +08:00
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.monitorId - The ID of the monitor to be deleted.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status and a message indicating the deletion of the monitor.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422) or an error in deleting associated records.
2024-05-21 11:23:39 -07:00
*/
2024-05-27 10:47:14 -07:00
const deleteMonitor = async ( req , res , next ) => {
try {
2024-07-30 13:36:53 -07:00
await getMonitorByIdParamValidation . validateAsync ( req . params );
2024-05-27 10:47:14 -07:00
} catch ( error ) {
error . status = 422 ;
error . service = SERVICE_NAME ;
2024-07-30 13:36:53 -07:00
error . message =
error . details ? .[ 0 ] ? . message || error . message || "Validation Error" ;
2024-05-27 10:47:14 -07:00
next ( error );
return ;
2024-05-21 11:23:39 -07:00
}
2024-05-27 10:47:14 -07:00
2024-05-21 11:23:39 -07:00
try {
2024-05-27 10:47:14 -07:00
const monitor = await req . db . deleteMonitor ( req , res , next );
2024-07-30 13:36:53 -07:00
// Delete associated checks,alerts,and notifications
2024-09-26 10:42:03 +08:00
try {
await req . jobQueue . deleteJob ( monitor );
await req . db . deleteChecks ( monitor . _id );
await req . db . deletePageSpeedChecksByMonitorId ( monitor . _id );
await req . db . deleteNotificationsByMonitorId ( monitor . _id );
} catch ( error ) {
logger . error (
`Error deleting associated records for monitor ${ monitor . _id } with name ${ monitor . name } ` ,
{
2024-09-26 11:01:38 +08:00
method : "deleteMonitor" ,
2024-09-26 10:42:03 +08:00
service : SERVICE_NAME ,
error : error . message ,
}
);
}
2024-06-14 13:45:45 -07:00
return res
. status ( 200 )
. json ({ success : true , msg : successMessages . MONITOR_DELETE });
2024-05-21 13:52:28 -07:00
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "deleteMonitor" ) : null ;
2024-05-27 10:47:14 -07:00
next ( error );
2024-05-21 13:52:28 -07:00
}
};
2024-10-01 10:29:32 +08:00
/**
* Deletes all monitors associated with a team.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.headers - The headers of the request.
* @property {string} req.headers.authorization - The authorization header containing the JWT token.
* @param {Object} res - The Express response object.
* @param {function} next
* @returns {Object} The response object with a success status and a message indicating the number of deleted monitors.
* @throws {Error} If there is an error during the deletion process.
*/
const deleteAllMonitors = async ( req , res , next ) => {
2024-06-18 11:42:25 -07:00
try {
2024-09-11 11:22:33 +08:00
const token = getTokenFromHeaders ( req . headers );
2024-09-26 14:37:11 +08:00
const { jwtSecret } = req . settingsService . getSettings ();
const { teamId } = jwt . verify ( token , jwtSecret );
2024-09-11 11:22:33 +08:00
const { monitors , deletedCount } = await req . db . deleteAllMonitors ( teamId );
await monitors . forEach ( async ( monitor ) => {
await req . jobQueue . deleteJob ( monitor );
await req . db . deleteChecks ( monitor . _id );
await req . db . deleteAlertByMonitorId ( monitor . _id );
await req . db . deletePageSpeedChecksByMonitorId ( monitor . _id );
await req . db . deleteNotificationsByMonitorId ( monitor . _id );
});
2024-06-18 11:42:25 -07:00
return res
. status ( 200 )
2024-09-11 11:22:33 +08:00
. json ({ success : true , msg : `Deleted ${ deletedCount } monitors` });
2024-06-18 11:42:25 -07:00
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "deleteAllMonitors" ) : null ;
2024-06-18 11:42:25 -07:00
next ( error );
}
};
2024-05-21 13:52:28 -07:00
/**
2024-10-01 10:29:32 +08:00
* Edits a monitor by its ID, updates its notifications, and updates its job in the job queue.
2024-05-21 13:52:28 -07:00
* @async
2024-10-01 10:29:32 +08:00
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.monitorId - The ID of the monitor to be edited.
* @property {Object} req.body - The body of the request.
* @property {Array} req.body.notifications - The notifications to be associated with the monitor.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message indicating the editing of the monitor, and the edited monitor data.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
2024-05-21 13:52:28 -07:00
*/
2024-05-24 13:44:42 -07:00
const editMonitor = async ( req , res , next ) => {
2024-05-27 10:47:14 -07:00
try {
2024-07-30 13:36:53 -07:00
await getMonitorByIdParamValidation . validateAsync ( req . params );
2024-07-24 12:08:42 -07:00
await editMonitorBodyValidation . validateAsync ( req . body );
2024-05-27 10:47:14 -07:00
} catch ( error ) {
error . status = 422 ;
error . service = SERVICE_NAME ;
2024-07-30 13:36:53 -07:00
error . message =
error . details ? .[ 0 ] ? . message || error . message || "Validation Error" ;
2024-05-27 10:47:14 -07:00
next ( error );
return ;
2024-05-21 13:52:28 -07:00
}
try {
2024-08-16 15:26:26 -07:00
const { monitorId } = req . params ;
const monitorBeforeEdit = await req . db . getMonitorById ( monitorId );
2024-07-31 13:59:14 -07:00
// Get notifications from the request body
const notifications = req . body . notifications ;
2024-08-28 15:21:59 -07:00
const editedMonitor = await req . db . editMonitor ( monitorId , req . body );
2024-07-31 13:59:14 -07:00
await req . db . deleteNotificationsByMonitorId ( editedMonitor . _id );
if ( notifications && notifications . length !== 0 ) {
await Promise . all (
notifications . map ( async ( notification ) => {
notification . monitorId = editedMonitor . _id ;
await req . db . createNotification ( notification );
})
);
}
2024-07-03 13:55:44 -07:00
// Delete the old job(editedMonitor has the same ID as the old monitor)
2024-07-31 13:29:04 -07:00
await req . jobQueue . deleteJob ( monitorBeforeEdit );
2024-07-03 13:55:44 -07:00
// Add the new job back to the queue
2024-07-03 23:19:12 -07:00
await req . jobQueue . addJob ( editedMonitor . _id , editedMonitor );
2024-06-17 12:08:41 -07:00
return res . status ( 200 ). json ({
success : true ,
msg : successMessages . MONITOR_EDIT ,
data : editedMonitor ,
});
2024-05-21 11:23:39 -07:00
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "editMonitor" ) : null ;
2024-05-27 10:47:14 -07:00
next ( error );
2024-05-21 11:23:39 -07:00
}
};
2024-10-01 10:29:32 +08:00
/**
* Pauses or resumes a monitor based on its current state.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.params - The parameters of the request.
* @property {string} req.params.monitorId - The ID of the monitor to be paused or resumed.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message indicating the new state of the monitor, and the updated monitor data.
* @throws {Error} If there is an error during the process.
*/
2024-08-28 15:21:59 -07:00
const pauseMonitor = async ( req , res , next ) => {
try {
await pauseMonitorParamValidation . validateAsync ( req . params );
} catch ( error ) {
error . status = 422 ;
error . service = SERVICE_NAME ;
error . message =
error . details ? .[ 0 ] ? . message || error . message || "Validation Error" ;
next ( error );
}
try {
const monitor = await req . db . getMonitorById ( req . params . monitorId );
if ( monitor . isActive ) {
await req . jobQueue . deleteJob ( monitor );
} else {
await req . jobQueue . addJob ( monitor . _id , monitor );
}
monitor . isActive = ! monitor . isActive ;
2024-09-03 15:43:12 -07:00
monitor . status = undefined ;
2024-08-30 13:03:59 -07:00
monitor . save ();
2024-08-28 15:21:59 -07:00
return res . status ( 200 ). json ({
success : true ,
2024-08-30 13:03:59 -07:00
msg : monitor . isActive
2024-08-28 15:21:59 -07:00
? successMessages . MONITOR_RESUME
: successMessages . MONITOR_PAUSE ,
2024-08-30 13:03:59 -07:00
data : monitor ,
2024-08-28 15:21:59 -07:00
});
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "pauseMonitor" ) : null ;
2024-08-28 15:21:59 -07:00
next ( error );
}
};
2024-10-01 10:29:32 +08:00
/**
* Adds demo monitors for a team.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.headers - The headers of the request.
* @property {string} req.headers.authorization - The authorization header containing the JWT token.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message indicating the addition of demo monitors, and the number of demo monitors added.
* @throws {Error} If there is an error during the process.
*/
2024-09-11 11:03:39 +08:00
const addDemoMonitors = async ( req , res , next ) => {
try {
const token = getTokenFromHeaders ( req . headers );
2024-09-26 14:37:11 +08:00
const { jwtSecret } = req . settingsService . getSettings ();
const { _id , teamId } = jwt . verify ( token , jwtSecret );
2024-09-11 11:03:39 +08:00
const demoMonitors = await req . db . addDemoMonitors ( _id , teamId );
await demoMonitors . forEach ( async ( monitor ) => {
await req . jobQueue . addJob ( monitor . _id , monitor );
});
return res . status ( 200 ). json ({
success : true ,
message : successMessages . MONITOR_DEMO_ADDED ,
data : demoMonitors . length ,
});
} catch ( error ) {
2024-09-15 12:16:20 +08:00
error . service === undefined ? ( error . service = SERVICE_NAME ) : null ;
error . method === undefined ? ( error . method = "addDemoMonitors" ) : null ;
2024-09-11 11:03:39 +08:00
next ( error );
}
};
2024-05-21 11:23:39 -07:00
module . exports = {
getAllMonitors ,
2024-08-12 12:05:57 -07:00
getMonitorStatsById ,
2024-08-12 15:15:13 -07:00
getMonitorCertificate ,
2024-05-21 11:23:39 -07:00
getMonitorById ,
2024-09-05 11:17:31 -07:00
getMonitorsAndSummaryByTeamId ,
2024-08-26 19:38:24 -07:00
getMonitorsByTeamId ,
2024-05-21 11:23:39 -07:00
createMonitor ,
deleteMonitor ,
2024-06-18 11:42:25 -07:00
deleteAllMonitors ,
2024-05-21 13:52:28 -07:00
editMonitor ,
2024-08-28 15:21:59 -07:00
pauseMonitor ,
2024-09-11 11:03:39 +08:00
addDemoMonitors ,
2024-05-21 11:23:39 -07:00
};