Files
Checkmate/server/src/service/business/checkService.js
T

149 lines
3.8 KiB
JavaScript
Raw Normal View History

2025-07-23 13:41:10 -07:00
const SERVICE_NAME = "checkService";
class CheckService {
static SERVICE_NAME = SERVICE_NAME;
2025-07-25 10:16:29 -07:00
constructor({ db, settingsService, stringService, errorService }) {
2025-07-23 13:41:10 -07:00
this.db = db;
this.settingsService = settingsService;
this.stringService = stringService;
2025-07-25 10:16:29 -07:00
this.errorService = errorService;
2025-07-23 13:41:10 -07:00
}
2025-07-25 15:15:14 -07:00
get serviceName() {
return CheckService.SERVICE_NAME;
}
2025-07-23 13:41:10 -07:00
getChecksByMonitor = async ({ monitorId, query, teamId }) => {
if (!monitorId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No monitor ID in request");
2025-07-23 13:41:10 -07:00
}
if (!teamId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No team ID in request");
2025-07-23 13:41:10 -07:00
}
2025-07-30 12:25:12 -07:00
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
2025-07-23 13:41:10 -07:00
if (!monitor) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createNotFoundError("Monitor not found");
2025-07-23 13:41:10 -07:00
}
if (!monitor.teamId.equals(teamId)) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createAuthorizationError();
2025-07-23 13:41:10 -07:00
}
2025-08-18 10:55:12 -07:00
let { sortOrder, dateRange, filter, ack, page, rowsPerPage, status } = query;
const result = await this.db.checkModule.getChecksByMonitor({
2025-07-23 13:41:10 -07:00
monitorId,
sortOrder,
dateRange,
filter,
ack,
page,
rowsPerPage,
status,
});
return result;
};
getChecksByTeam = async ({ teamId, query }) => {
let { sortOrder, dateRange, filter, ack, page, rowsPerPage } = query;
if (!teamId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No team ID in request");
2025-07-23 13:41:10 -07:00
}
const checkData = await this.db.checkModule.getChecksByTeam({
2025-07-23 13:41:10 -07:00
sortOrder,
dateRange,
filter,
ack,
page,
rowsPerPage,
teamId,
});
return checkData;
};
getChecksSummaryByTeamId = async ({ teamId }) => {
if (!teamId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No team ID in request");
2025-07-23 13:41:10 -07:00
}
const summary = await this.db.checkModule.getChecksSummaryByTeamId({ teamId });
2025-07-23 13:41:10 -07:00
return summary;
};
ackCheck = async ({ checkId, teamId, ack }) => {
if (!checkId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No check ID in request");
2025-07-23 13:41:10 -07:00
}
if (!teamId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No team ID in request");
2025-07-23 13:41:10 -07:00
}
const updatedCheck = await this.db.checkModule.ackCheck(checkId, teamId, ack);
2025-07-23 13:41:10 -07:00
return updatedCheck;
};
ackAllChecks = async ({ monitorId, path, teamId, ack }) => {
if (path === "monitor") {
if (!monitorId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No monitor ID in request");
2025-07-23 13:41:10 -07:00
}
2025-07-30 12:25:12 -07:00
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
2025-07-23 13:41:10 -07:00
if (!monitor) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createNotFoundError("Monitor not found");
2025-07-23 13:41:10 -07:00
}
if (!monitor.teamId.equals(teamId)) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createAuthorizationError();
2025-07-23 13:41:10 -07:00
}
}
const updatedChecks = await this.db.checkModule.ackAllChecks(monitorId, teamId, ack, path);
2025-07-23 13:41:10 -07:00
return updatedChecks;
};
deleteChecks = async ({ monitorId, teamId }) => {
if (!monitorId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No monitor ID in request");
2025-07-23 13:41:10 -07:00
}
if (!teamId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No team ID in request");
2025-07-23 13:41:10 -07:00
}
2025-07-30 12:25:12 -07:00
const monitor = await this.db.monitorModule.getMonitorById(monitorId);
2025-07-23 13:41:10 -07:00
if (!monitor) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createNotFoundError("Monitor not found");
2025-07-23 13:41:10 -07:00
}
if (!monitor.teamId.equals(teamId)) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createAuthorizationError();
2025-07-23 13:41:10 -07:00
}
const deletedCount = await this.db.checkModule.deleteChecks(monitorId);
2025-07-23 13:41:10 -07:00
return deletedCount;
};
deleteChecksByTeamId = async ({ teamId }) => {
if (!teamId) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createBadRequestError("No team ID in request");
2025-07-23 13:41:10 -07:00
}
const deletedCount = await this.db.checkModule.deleteChecksByTeamId(teamId);
2025-07-23 13:41:10 -07:00
return deletedCount;
};
updateChecksTTL = async ({ teamId, ttl }) => {
const SECONDS_PER_DAY = 86400;
const newTTL = parseInt(ttl, 10) * SECONDS_PER_DAY;
await this.db.checkModule.updateChecksTTL(teamId, newTTL);
2025-07-23 13:41:10 -07:00
};
}
export default CheckService;