add an error service, base controller
This commit is contained in:
1 parent
0cfe9d60fb
commit
ddf3e8251d
20 files changed
+412
-301
No files matched your search
@@ -13,30 +13,31 @@ import {
|
||||
getHardwareDetailsByIdQueryValidation,
|
||||
} from "../validation/joi.js";
|
||||
import sslChecker from "ssl-checker";
|
||||
import { asyncHandler } from "../utils/errorUtils.js";
|
||||
import { fetchMonitorCertificate } from "./controllerUtils.js";
|
||||
import BaseController from "./baseController.js";
|
||||
|
||||
const SERVICE_NAME = "monitorController";
|
||||
class MonitorController {
|
||||
constructor({ db, settingsService, jobQueue, stringService, emailService, monitorService }) {
|
||||
class MonitorController extends BaseController {
|
||||
constructor({ db, settingsService, jobQueue, stringService, emailService, monitorService, errorService }) {
|
||||
super();
|
||||
this.db = db;
|
||||
this.settingsService = settingsService;
|
||||
this.jobQueue = jobQueue;
|
||||
this.stringService = stringService;
|
||||
this.emailService = emailService;
|
||||
this.monitorService = monitorService;
|
||||
this.errorService = errorService;
|
||||
this.asyncHandler = errorService.asyncHandler;
|
||||
}
|
||||
|
||||
async verifyTeamAccess(teamId, monitorId) {
|
||||
const monitor = await this.db.getMonitorById(monitorId);
|
||||
if (!monitor.teamId.equals(teamId)) {
|
||||
const error = new Error("Unauthorized");
|
||||
error.status = 403;
|
||||
throw error;
|
||||
throw this.errorService.createAuthorizationError();
|
||||
}
|
||||
}
|
||||
|
||||
getAllMonitors = asyncHandler(
|
||||
getAllMonitors = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
const monitors = await this.monitorService.getAllMonitors();
|
||||
return res.success({
|
||||
@@ -48,7 +49,7 @@ class MonitorController {
|
||||
"getAllMonitors"
|
||||
);
|
||||
|
||||
getUptimeDetailsById = asyncHandler(
|
||||
getUptimeDetailsById = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
const monitorId = req?.params?.monitorId;
|
||||
const dateRange = req?.query?.dateRange;
|
||||
@@ -57,7 +58,7 @@ class MonitorController {
|
||||
const teamId = req?.user?.teamId;
|
||||
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const data = await this.monitorService.getUptimeDetailsById({
|
||||
@@ -75,7 +76,7 @@ class MonitorController {
|
||||
"getUptimeDetailsById"
|
||||
);
|
||||
|
||||
getMonitorStatsById = asyncHandler(
|
||||
getMonitorStatsById = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await getMonitorStatsByIdParamValidation.validateAsync(req.params);
|
||||
await getMonitorStatsByIdQueryValidation.validateAsync(req.query);
|
||||
@@ -85,7 +86,7 @@ class MonitorController {
|
||||
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const monitorStats = await this.monitorService.getMonitorStatsById({
|
||||
@@ -116,7 +117,7 @@ class MonitorController {
|
||||
* @returns {Promise<Express.Response>}
|
||||
* @throws {Error} - Throws error if monitor not found or other database errors
|
||||
*/
|
||||
getHardwareDetailsById = asyncHandler(
|
||||
getHardwareDetailsById = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await getHardwareDetailsByIdParamValidation.validateAsync(req.params);
|
||||
await getHardwareDetailsByIdQueryValidation.validateAsync(req.query);
|
||||
@@ -125,7 +126,7 @@ class MonitorController {
|
||||
const dateRange = req?.query?.dateRange;
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const monitor = await this.monitorService.getHardwareDetailsById({
|
||||
@@ -143,8 +144,8 @@ class MonitorController {
|
||||
"getHardwareDetailsById"
|
||||
);
|
||||
|
||||
getMonitorCertificate = asyncHandler(
|
||||
async (req, res, next) => {
|
||||
getMonitorCertificate = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await getCertificateParamValidation.validateAsync(req.params);
|
||||
|
||||
const { monitorId } = req.params;
|
||||
@@ -162,14 +163,14 @@ class MonitorController {
|
||||
"getMonitorCertificate"
|
||||
);
|
||||
|
||||
getMonitorById = asyncHandler(
|
||||
getMonitorById = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await getMonitorByIdParamValidation.validateAsync(req.params);
|
||||
await getMonitorByIdQueryValidation.validateAsync(req.query);
|
||||
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const monitor = await this.monitorService.getMonitorById({ teamId, monitorId: req?.params?.monitorId });
|
||||
@@ -183,7 +184,7 @@ class MonitorController {
|
||||
"getMonitorById"
|
||||
);
|
||||
|
||||
createMonitor = asyncHandler(
|
||||
createMonitor = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await createMonitorBodyValidation.validateAsync(req.body);
|
||||
|
||||
@@ -201,30 +202,30 @@ class MonitorController {
|
||||
"createMonitor"
|
||||
);
|
||||
|
||||
createBulkMonitors = asyncHandler(
|
||||
createBulkMonitors = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
if (!req.file) {
|
||||
throw new Error("No file uploaded");
|
||||
throw this.errorService.createBadRequestError("No file uploaded");
|
||||
}
|
||||
|
||||
if (!req.file.mimetype.includes("csv")) {
|
||||
throw new Error("File is not a CSV");
|
||||
throw this.errorService.createBadRequestError("File is not a CSV");
|
||||
}
|
||||
|
||||
if (req.file.size === 0) {
|
||||
throw new Error("File is empty");
|
||||
throw this.errorService.createBadRequestError("File is empty");
|
||||
}
|
||||
|
||||
const userId = req?.user?._id;
|
||||
const teamId = req?.user?.teamId;
|
||||
|
||||
if (!userId || !teamId) {
|
||||
throw new Error("Missing userId or teamId");
|
||||
throw this.errorService.createBadRequestError("Missing userId or teamId");
|
||||
}
|
||||
|
||||
const fileData = req?.file?.buffer?.toString("utf-8");
|
||||
if (!fileData) {
|
||||
throw new Error("Cannot get file from buffer");
|
||||
throw this.errorService.createBadRequestError("Cannot get file from buffer");
|
||||
}
|
||||
|
||||
const monitors = await this.monitorService.createBulkMonitors({ fileData, userId, teamId });
|
||||
@@ -238,13 +239,13 @@ class MonitorController {
|
||||
"createBulkMonitors"
|
||||
);
|
||||
|
||||
deleteMonitor = asyncHandler(
|
||||
deleteMonitor = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await getMonitorByIdParamValidation.validateAsync(req.params);
|
||||
const monitorId = req.params.monitorId;
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const deletedMonitor = await this.monitorService.deleteMonitor({ teamId, monitorId });
|
||||
@@ -255,11 +256,11 @@ class MonitorController {
|
||||
"deleteMonitor"
|
||||
);
|
||||
|
||||
deleteAllMonitors = asyncHandler(
|
||||
deleteAllMonitors = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const deletedCount = await this.monitorService.deleteAllMonitors({ teamId });
|
||||
@@ -270,7 +271,7 @@ class MonitorController {
|
||||
"deleteAllMonitors"
|
||||
);
|
||||
|
||||
editMonitor = asyncHandler(
|
||||
editMonitor = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await getMonitorByIdParamValidation.validateAsync(req.params);
|
||||
await editMonitorBodyValidation.validateAsync(req.body);
|
||||
@@ -278,7 +279,7 @@ class MonitorController {
|
||||
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const editedMonitor = await this.monitorService.editMonitor({ teamId, monitorId, body: req.body });
|
||||
@@ -292,14 +293,14 @@ class MonitorController {
|
||||
"editMonitor"
|
||||
);
|
||||
|
||||
pauseMonitor = asyncHandler(
|
||||
pauseMonitor = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await pauseMonitorParamValidation.validateAsync(req.params);
|
||||
|
||||
const monitorId = req.params.monitorId;
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const monitor = await this.monitorService.pauseMonitor({ teamId, monitorId });
|
||||
@@ -313,7 +314,7 @@ class MonitorController {
|
||||
"pauseMonitor"
|
||||
);
|
||||
|
||||
addDemoMonitors = asyncHandler(
|
||||
addDemoMonitors = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
const { _id, teamId } = req.user;
|
||||
const demoMonitors = await this.monitorService.addDemoMonitors({ userId: _id, teamId });
|
||||
@@ -327,11 +328,11 @@ class MonitorController {
|
||||
"addDemoMonitors"
|
||||
);
|
||||
|
||||
sendTestEmail = asyncHandler(
|
||||
sendTestEmail = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
const { to } = req.body;
|
||||
if (!to || typeof to !== "string") {
|
||||
throw new Error(this.stringService.errorForValidEmailAddress);
|
||||
throw this.errorService.createBadRequestError(this.stringService.errorForValidEmailAddress);
|
||||
}
|
||||
|
||||
const messageId = await this.monitorService.sendTestEmail({ to });
|
||||
@@ -344,8 +345,8 @@ class MonitorController {
|
||||
"sendTestEmail"
|
||||
);
|
||||
|
||||
getMonitorsByTeamId = asyncHandler(
|
||||
async (req, res, next) => {
|
||||
getMonitorsByTeamId = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await getMonitorsByTeamIdParamValidation.validateAsync(req.params);
|
||||
await getMonitorsByTeamIdQueryValidation.validateAsync(req.query);
|
||||
|
||||
@@ -363,7 +364,7 @@ class MonitorController {
|
||||
"getMonitorsByTeamId"
|
||||
);
|
||||
|
||||
getMonitorsAndSummaryByTeamId = asyncHandler(
|
||||
getMonitorsAndSummaryByTeamId = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await getMonitorsByTeamIdParamValidation.validateAsync(req.params);
|
||||
await getMonitorsByTeamIdQueryValidation.validateAsync(req.query);
|
||||
@@ -372,7 +373,7 @@ class MonitorController {
|
||||
const type = req?.query?.type;
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const result = await this.monitorService.getMonitorsAndSummaryByTeamId({ teamId, type, explain });
|
||||
@@ -386,7 +387,7 @@ class MonitorController {
|
||||
"getMonitorsAndSummaryByTeamId"
|
||||
);
|
||||
|
||||
getMonitorsWithChecksByTeamId = asyncHandler(
|
||||
getMonitorsWithChecksByTeamId = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
await getMonitorsByTeamIdParamValidation.validateAsync(req.params);
|
||||
await getMonitorsByTeamIdQueryValidation.validateAsync(req.query);
|
||||
@@ -395,7 +396,7 @@ class MonitorController {
|
||||
let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const monitors = await this.monitorService.getMonitorsWithChecksByTeamId({
|
||||
@@ -419,11 +420,11 @@ class MonitorController {
|
||||
"getMonitorsWithChecksByTeamId"
|
||||
);
|
||||
|
||||
exportMonitorsToCSV = asyncHandler(
|
||||
exportMonitorsToCSV = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
const teamId = req?.user?.teamId;
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required");
|
||||
throw this.errorService.createBadRequestError("Team ID is required");
|
||||
}
|
||||
|
||||
const csv = await this.monitorService.exportMonitorsToCSV({ teamId });
|
||||
|
||||
Reference in new issue
Block a user