Merge pull request #2714 from bluewave-labs/feat/statuspage-module
feat: refactor status page module into class
This commit is contained in:
5 files changed
+45
-50
No files matched your search
@@ -33,15 +33,19 @@ import jwt from "jsonwebtoken";
|
|||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
|
||||||
// DB Modules
|
// DB Modules
|
||||||
|
import { NormalizeData } from "../utils/dataUtils.js";
|
||||||
|
|
||||||
import Check from "../db/models/Check.js";
|
import Check from "../db/models/Check.js";
|
||||||
import HardwareCheck from "../db/models/HardwareCheck.js";
|
import HardwareCheck from "../db/models/HardwareCheck.js";
|
||||||
import PageSpeedCheck from "../db/models/PageSpeedCheck.js";
|
import PageSpeedCheck from "../db/models/PageSpeedCheck.js";
|
||||||
import Monitor from "../db/models/Monitor.js";
|
import Monitor from "../db/models/Monitor.js";
|
||||||
import User from "../db/models/User.js";
|
import User from "../db/models/User.js";
|
||||||
import InviteToken from "../db/models/InviteToken.js";
|
import InviteToken from "../db/models/InviteToken.js";
|
||||||
|
import StatusPage from "../db/models/StatusPage.js";
|
||||||
|
|
||||||
import InviteModule from "../db/mongo/modules/inviteModule.js";
|
import InviteModule from "../db/mongo/modules/inviteModule.js";
|
||||||
import CheckModule from "../db/mongo/modules/checkModule.js";
|
import CheckModule from "../db/mongo/modules/checkModule.js";
|
||||||
|
import StatusPageModule from "../db/mongo/modules/statusPageModule.js";
|
||||||
|
|
||||||
export const initializeServices = async ({ logger, envSettings, settingsService }) => {
|
export const initializeServices = async ({ logger, envSettings, settingsService }) => {
|
||||||
const serviceRegistry = new ServiceRegistry({ logger });
|
const serviceRegistry = new ServiceRegistry({ logger });
|
||||||
@@ -55,7 +59,8 @@ export const initializeServices = async ({ logger, envSettings, settingsService
|
|||||||
// Create DB
|
// Create DB
|
||||||
const checkModule = new CheckModule({ logger, Check, HardwareCheck, PageSpeedCheck, Monitor, User });
|
const checkModule = new CheckModule({ logger, Check, HardwareCheck, PageSpeedCheck, Monitor, User });
|
||||||
const inviteModule = new InviteModule({ InviteToken, crypto, stringService });
|
const inviteModule = new InviteModule({ InviteToken, crypto, stringService });
|
||||||
const db = new MongoDB({ logger, envSettings, checkModule, inviteModule });
|
const statusPageModule = new StatusPageModule({ StatusPage, NormalizeData, stringService });
|
||||||
|
const db = new MongoDB({ logger, envSettings, checkModule, inviteModule, statusPageModule });
|
||||||
await db.connect();
|
await db.connect();
|
||||||
|
|
||||||
const networkService = new NetworkService(axios, ping, logger, http, Docker, net, stringService, settingsService);
|
const networkService = new NetworkService(axios, ping, logger, http, Docker, net, stringService, settingsService);
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class StatusPageController extends BaseController {
|
|||||||
await imageValidation.validateAsync(req.file);
|
await imageValidation.validateAsync(req.file);
|
||||||
|
|
||||||
const { _id, teamId } = req.user;
|
const { _id, teamId } = req.user;
|
||||||
const statusPage = await this.db.createStatusPage({
|
const statusPage = await this.db.statusPageModule.createStatusPage({
|
||||||
statusPageData: req.body,
|
statusPageData: req.body,
|
||||||
image: req.file,
|
image: req.file,
|
||||||
userId: _id,
|
userId: _id,
|
||||||
@@ -39,7 +39,7 @@ class StatusPageController extends BaseController {
|
|||||||
await createStatusPageBodyValidation.validateAsync(req.body);
|
await createStatusPageBodyValidation.validateAsync(req.body);
|
||||||
await imageValidation.validateAsync(req.file);
|
await imageValidation.validateAsync(req.file);
|
||||||
|
|
||||||
const statusPage = await this.db.updateStatusPage(req.body, req.file);
|
const statusPage = await this.db.statusPageModule.updateStatusPage(req.body, req.file);
|
||||||
if (statusPage === null) {
|
if (statusPage === null) {
|
||||||
throw this.errorService.createNotFoundError(this.stringService.statusPageNotFound);
|
throw this.errorService.createNotFoundError(this.stringService.statusPageNotFound);
|
||||||
}
|
}
|
||||||
@@ -54,7 +54,7 @@ class StatusPageController extends BaseController {
|
|||||||
|
|
||||||
getStatusPage = this.asyncHandler(
|
getStatusPage = this.asyncHandler(
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
const statusPage = await this.db.getStatusPage();
|
const statusPage = await this.db.statusPageModule.getStatusPage();
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.statusPageByUrl,
|
msg: this.stringService.statusPageByUrl,
|
||||||
data: statusPage,
|
data: statusPage,
|
||||||
@@ -69,7 +69,7 @@ class StatusPageController extends BaseController {
|
|||||||
await getStatusPageParamValidation.validateAsync(req.params);
|
await getStatusPageParamValidation.validateAsync(req.params);
|
||||||
await getStatusPageQueryValidation.validateAsync(req.query);
|
await getStatusPageQueryValidation.validateAsync(req.query);
|
||||||
|
|
||||||
const statusPage = await this.db.getStatusPageByUrl(req.params.url, req.query.type);
|
const statusPage = await this.db.statusPageModule.getStatusPageByUrl(req.params.url);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.statusPageByUrl,
|
msg: this.stringService.statusPageByUrl,
|
||||||
data: statusPage,
|
data: statusPage,
|
||||||
@@ -82,7 +82,7 @@ class StatusPageController extends BaseController {
|
|||||||
getStatusPagesByTeamId = this.asyncHandler(
|
getStatusPagesByTeamId = this.asyncHandler(
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
const teamId = req.user.teamId;
|
const teamId = req.user.teamId;
|
||||||
const statusPages = await this.db.getStatusPagesByTeamId(teamId);
|
const statusPages = await this.db.statusPageModule.getStatusPagesByTeamId(teamId);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.statusPageByTeamId,
|
msg: this.stringService.statusPageByTeamId,
|
||||||
|
|||||||
@@ -44,11 +44,6 @@ import * as notificationModule from "./modules/notificationModule.js";
|
|||||||
//****************************************
|
//****************************************
|
||||||
import * as settingsModule from "./modules/settingsModule.js";
|
import * as settingsModule from "./modules/settingsModule.js";
|
||||||
|
|
||||||
//****************************************
|
|
||||||
// Status Page
|
|
||||||
//****************************************
|
|
||||||
import * as statusPageModule from "./modules/statusPageModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Diagnostic
|
// Diagnostic
|
||||||
//****************************************
|
//****************************************
|
||||||
@@ -57,7 +52,7 @@ import * as diagnosticModule from "./modules/diagnosticModule.js";
|
|||||||
class MongoDB {
|
class MongoDB {
|
||||||
static SERVICE_NAME = "MongoDB";
|
static SERVICE_NAME = "MongoDB";
|
||||||
|
|
||||||
constructor({ logger, envSettings, checkModule, inviteModule }) {
|
constructor({ logger, envSettings, checkModule, inviteModule, statusPageModule }) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.envSettings = envSettings;
|
this.envSettings = envSettings;
|
||||||
Object.assign(this, userModule);
|
Object.assign(this, userModule);
|
||||||
@@ -70,7 +65,7 @@ class MongoDB {
|
|||||||
Object.assign(this, maintenanceWindowModule);
|
Object.assign(this, maintenanceWindowModule);
|
||||||
Object.assign(this, notificationModule);
|
Object.assign(this, notificationModule);
|
||||||
Object.assign(this, settingsModule);
|
Object.assign(this, settingsModule);
|
||||||
Object.assign(this, statusPageModule);
|
this.statusPageModule = statusPageModule;
|
||||||
Object.assign(this, diagnosticModule);
|
Object.assign(this, diagnosticModule);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,20 @@
|
|||||||
import StatusPage from "../../models/StatusPage.js";
|
// import StatusPage from "../../models/StatusPage.js";
|
||||||
import { NormalizeData } from "../../../utils/dataUtils.js";
|
// import { NormalizeData } from "../../../utils/dataUtils.js";
|
||||||
import ServiceRegistry from "../../../service/system/serviceRegistry.js";
|
// import ServiceRegistry from "../../../service/system/serviceRegistry.js";
|
||||||
import StringService from "../../../service/system/stringService.js";
|
// import StringService from "../../../service/system/stringService.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "statusPageModule";
|
const SERVICE_NAME = "statusPageModule";
|
||||||
|
|
||||||
const createStatusPage = async ({ statusPageData, image, userId, teamId }) => {
|
class StatusPageModule {
|
||||||
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
constructor({ StatusPage, NormalizeData, stringService }) {
|
||||||
|
this.StatusPage = StatusPage;
|
||||||
|
this.NormalizeData = NormalizeData;
|
||||||
|
this.stringService = stringService;
|
||||||
|
}
|
||||||
|
|
||||||
|
createStatusPage = async ({ statusPageData, image, userId, teamId }) => {
|
||||||
try {
|
try {
|
||||||
const statusPage = new StatusPage({
|
const statusPage = new this.StatusPage({
|
||||||
...statusPageData,
|
...statusPageData,
|
||||||
userId,
|
userId,
|
||||||
teamId,
|
teamId,
|
||||||
@@ -26,7 +31,7 @@ const createStatusPage = async ({ statusPageData, image, userId, teamId }) => {
|
|||||||
if (error?.code === 11000) {
|
if (error?.code === 11000) {
|
||||||
// Handle duplicate URL errors
|
// Handle duplicate URL errors
|
||||||
error.status = 400;
|
error.status = 400;
|
||||||
error.message = stringService.statusPageUrlNotUnique;
|
error.message = this.stringService.statusPageUrlNotUnique;
|
||||||
}
|
}
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "createStatusPage";
|
error.method = "createStatusPage";
|
||||||
@@ -34,7 +39,7 @@ const createStatusPage = async ({ statusPageData, image, userId, teamId }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateStatusPage = async (statusPageData, image) => {
|
updateStatusPage = async (statusPageData, image) => {
|
||||||
try {
|
try {
|
||||||
if (image) {
|
if (image) {
|
||||||
statusPageData.logo = {
|
statusPageData.logo = {
|
||||||
@@ -48,7 +53,7 @@ const updateStatusPage = async (statusPageData, image) => {
|
|||||||
if (statusPageData.deleteSubmonitors === "true") {
|
if (statusPageData.deleteSubmonitors === "true") {
|
||||||
statusPageData.subMonitors = [];
|
statusPageData.subMonitors = [];
|
||||||
}
|
}
|
||||||
const statusPage = await StatusPage.findOneAndUpdate({ url: statusPageData.url }, statusPageData, {
|
const statusPage = await this.StatusPage.findOneAndUpdate({ url: statusPageData.url }, statusPageData, {
|
||||||
new: true,
|
new: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -60,10 +65,10 @@ const updateStatusPage = async (statusPageData, image) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStatusPageByUrl = async (url, type) => {
|
getStatusPageByUrl = async (url) => {
|
||||||
// TODO This is deprecated, can remove and have controller call getStatusPage
|
// TODO This is deprecated, can remove and have controller call getStatusPage
|
||||||
try {
|
try {
|
||||||
return getStatusPage(url);
|
return this.getStatusPage(url);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "getStatusPageByUrl";
|
error.method = "getStatusPageByUrl";
|
||||||
@@ -71,9 +76,9 @@ const getStatusPageByUrl = async (url, type) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStatusPagesByTeamId = async (teamId) => {
|
getStatusPagesByTeamId = async (teamId) => {
|
||||||
try {
|
try {
|
||||||
const statusPages = await StatusPage.find({ teamId });
|
const statusPages = await this.StatusPage.find({ teamId });
|
||||||
return statusPages;
|
return statusPages;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
@@ -82,13 +87,11 @@ const getStatusPagesByTeamId = async (teamId) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStatusPage = async (url) => {
|
getStatusPage = async (url) => {
|
||||||
const stringService = ServiceRegistry.get(StringService.SERVICE_NAME);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const preliminaryStatusPage = await StatusPage.findOne({ url });
|
const preliminaryStatusPage = await this.StatusPage.findOne({ url });
|
||||||
if (!preliminaryStatusPage) {
|
if (!preliminaryStatusPage) {
|
||||||
const error = new Error(stringService.statusPageNotFound);
|
const error = new Error(this.stringService.statusPageNotFound);
|
||||||
error.status = 404;
|
error.status = 404;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@@ -114,7 +117,7 @@ const getStatusPage = async (url) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const statusPageQuery = await StatusPage.aggregate([
|
const statusPageQuery = await this.StatusPage.aggregate([
|
||||||
{ $match: { url: url } },
|
{ $match: { url: url } },
|
||||||
{
|
{
|
||||||
$set: {
|
$set: {
|
||||||
@@ -239,7 +242,7 @@ const getStatusPage = async (url) => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
if (!statusPageQuery.length) {
|
if (!statusPageQuery.length) {
|
||||||
const error = new Error(stringService.statusPageNotFound);
|
const error = new Error(this.stringService.statusPageNotFound);
|
||||||
error.status = 404;
|
error.status = 404;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@@ -249,7 +252,7 @@ const getStatusPage = async (url) => {
|
|||||||
const normalizedMonitors = monitors.map((monitor) => {
|
const normalizedMonitors = monitors.map((monitor) => {
|
||||||
return {
|
return {
|
||||||
...monitor,
|
...monitor,
|
||||||
checks: NormalizeData(monitor.checks, 10, 100),
|
checks: this.NormalizeData(monitor.checks, 10, 100),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -261,32 +264,24 @@ const getStatusPage = async (url) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteStatusPage = async (url) => {
|
deleteStatusPage = async (url) => {
|
||||||
try {
|
try {
|
||||||
await StatusPage.deleteOne({ url });
|
await this.StatusPage.deleteOne({ url });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "deleteStatusPage";
|
error.method = "deleteStatusPage";
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
deleteStatusPagesByMonitorId = async (monitorId) => {
|
||||||
const deleteStatusPagesByMonitorId = async (monitorId) => {
|
|
||||||
try {
|
try {
|
||||||
await StatusPage.deleteMany({ monitors: { $in: [monitorId] } });
|
await this.StatusPage.deleteMany({ monitors: { $in: [monitorId] } });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "deleteStatusPageByMonitorId";
|
error.method = "deleteStatusPageByMonitorId";
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export default StatusPageModule;
|
||||||
createStatusPage,
|
|
||||||
updateStatusPage,
|
|
||||||
getStatusPagesByTeamId,
|
|
||||||
getStatusPage,
|
|
||||||
getStatusPageByUrl,
|
|
||||||
deleteStatusPage,
|
|
||||||
deleteStatusPagesByMonitorId,
|
|
||||||
};
|
|
||||||
@@ -143,7 +143,7 @@ class MonitorService {
|
|||||||
await this.verifyTeamAccess({ teamId, monitorId });
|
await this.verifyTeamAccess({ teamId, monitorId });
|
||||||
const monitor = await this.db.deleteMonitor({ teamId, monitorId });
|
const monitor = await this.db.deleteMonitor({ teamId, monitorId });
|
||||||
await this.jobQueue.deleteJob(monitor);
|
await this.jobQueue.deleteJob(monitor);
|
||||||
await this.db.deleteStatusPagesByMonitorId(monitor._id);
|
await this.db.statusPageModule.deleteStatusPagesByMonitorId(monitor._id);
|
||||||
return monitor;
|
return monitor;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user