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

150 lines
4.1 KiB
TypeScript
Raw Normal View History

2026-01-15 20:55:01 +00:00
import { IMonitorsRepository } from "@/repositories/index.js";
2025-07-23 13:41:10 -07:00
const SERVICE_NAME = "checkService";
class CheckService {
static SERVICE_NAME = SERVICE_NAME;
2026-01-15 20:55:01 +00:00
private db: any;
private settingsService: any;
private stringService: any;
private errorService: any;
private monitorsRepository: IMonitorsRepository;
constructor({
db,
settingsService,
stringService,
errorService,
monitorsRepository,
}: {
db: any;
settingsService: any;
stringService: any;
errorService: any;
monitorsRepository: IMonitorsRepository;
}) {
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;
2026-01-15 20:55:01 +00:00
this.monitorsRepository = monitorsRepository;
2025-07-23 13:41:10 -07:00
}
2025-07-25 15:15:14 -07:00
get serviceName() {
return CheckService.SERVICE_NAME;
}
2026-01-15 20:55:01 +00:00
getChecksByMonitor = async ({ monitorId, query, teamId }: { monitorId: string; query: any; teamId: string }) => {
2025-07-23 13:41:10 -07:00
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
}
2026-01-15 20:55:01 +00:00
// For verificaiton, throws an error if monitor doesn't belong to team
await this.monitorsRepository.findById(monitorId, teamId);
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;
};
2026-01-15 20:55:01 +00:00
getChecksByTeam = async ({ teamId, query }: { teamId: string; query: any }) => {
2025-07-23 13:41:10 -07:00
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;
};
2026-01-15 20:55:01 +00:00
getChecksSummaryByTeamId = async ({ teamId }: { teamId: string }) => {
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 summary = await this.db.checkModule.getChecksSummaryByTeamId({ teamId });
2025-07-23 13:41:10 -07:00
return summary;
};
2026-01-15 20:55:01 +00:00
ackCheck = async ({ checkId, teamId, ack }: { checkId: string; teamId: string; ack: any }) => {
2025-07-23 13:41:10 -07:00
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;
};
2026-01-15 20:55:01 +00:00
ackAllChecks = async ({ monitorId, path, teamId, ack }: { monitorId: string; path: string; teamId: string; ack: any }) => {
2025-07-23 13:41:10 -07:00
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
}
2026-01-15 20:55:01 +00:00
// For verificaiton, throws an error if monitor doesn't belong to team
await this.monitorsRepository.findById(monitorId, teamId);
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;
};
2026-01-15 20:55:01 +00:00
deleteChecks = async ({ monitorId, teamId }: { monitorId: string; teamId: string }) => {
2025-07-23 13:41:10 -07:00
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
}
2026-01-15 20:55:01 +00:00
// For verificaiton, throws an error if monitor doesn't belong to team
await this.monitorsRepository.findById(monitorId, teamId);
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;
};
2026-01-15 20:55:01 +00:00
deleteChecksByTeamId = async ({ teamId }: { teamId: string }) => {
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 deletedCount = await this.db.checkModule.deleteChecksByTeamId(teamId);
2025-07-23 13:41:10 -07:00
return deletedCount;
};
2026-01-15 20:55:01 +00:00
updateChecksTTL = async ({ teamId, ttl }: { teamId: string; ttl: string }) => {
2025-07-23 13:41:10 -07:00
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;