remove final js
This commit is contained in:
1 parent
ce322a8e28
commit
b511cde849
12 files changed
+53
-250
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { requireTeamId } from "@/controllers/controllerUtils.js";
|
||||
import { IChecksRepository, IMonitorsRepository } from "@/repositories/index.js";
|
||||
import type { MonitorType, MonitorStatusResponse, CheckErrorInfo, Check } from "@/types/index.js";
|
||||
import type { HardwareStatusPayload, PageSpeedStatusPayload } from "@/types/network.js";
|
||||
@@ -9,22 +10,18 @@ const SERVICE_NAME = "checkService";
|
||||
class CheckService {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
|
||||
private errorService: any;
|
||||
private monitorsRepository: IMonitorsRepository;
|
||||
private checksRepository: IChecksRepository;
|
||||
private logger: any;
|
||||
constructor({
|
||||
errorService,
|
||||
monitorsRepository,
|
||||
logger,
|
||||
checksRepository,
|
||||
}: {
|
||||
errorService: any;
|
||||
monitorsRepository: IMonitorsRepository;
|
||||
logger: any;
|
||||
checksRepository: IChecksRepository;
|
||||
}) {
|
||||
this.errorService = errorService;
|
||||
this.monitorsRepository = monitorsRepository;
|
||||
this.logger = logger;
|
||||
this.checksRepository = checksRepository;
|
||||
@@ -112,11 +109,10 @@ class CheckService {
|
||||
|
||||
getChecksByMonitor = async ({ monitorId, query, teamId }: { monitorId: string; query: any; teamId: string }) => {
|
||||
if (!monitorId) {
|
||||
throw this.errorService.createBadRequestError("No monitor ID in request");
|
||||
throw new AppError({ message: "No monitor ID in request", service: SERVICE_NAME, method: "getChecksByMonitor", status: 400 });
|
||||
}
|
||||
|
||||
if (!teamId) {
|
||||
throw this.errorService.createBadRequestError("No team ID in request");
|
||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getChecksByMonitor", status: 400 });
|
||||
}
|
||||
|
||||
// For verificaiton, throws an error if monitor doesn't belong to team
|
||||
@@ -136,7 +132,7 @@ class CheckService {
|
||||
let { sortOrder, dateRange, filter, page, rowsPerPage } = query;
|
||||
|
||||
if (!teamId) {
|
||||
throw this.errorService.createBadRequestError("No team ID in request");
|
||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getChecksByTeam", status: 400 });
|
||||
}
|
||||
|
||||
const parsedPage = page ? parseInt(page) : page;
|
||||
@@ -148,7 +144,7 @@ class CheckService {
|
||||
|
||||
getChecksSummaryByTeamId = async ({ teamId }: { teamId: string }) => {
|
||||
if (!teamId) {
|
||||
throw this.errorService.createBadRequestError("No team ID in request");
|
||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getChecksSummaryByTeamId", status: 400 });
|
||||
}
|
||||
const summary = await this.checksRepository.findSummaryByTeamId(teamId);
|
||||
return summary;
|
||||
@@ -156,11 +152,11 @@ class CheckService {
|
||||
|
||||
deleteChecks = async ({ monitorId, teamId }: { monitorId: string; teamId: string }) => {
|
||||
if (!monitorId) {
|
||||
throw this.errorService.createBadRequestError("No monitor ID in request");
|
||||
throw new AppError({ message: "No monitor ID in request", service: SERVICE_NAME, method: "deleteChecks", status: 400 });
|
||||
}
|
||||
|
||||
if (!teamId) {
|
||||
throw this.errorService.createBadRequestError("No team ID in request");
|
||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "deleteChecks", status: 400 });
|
||||
}
|
||||
|
||||
// For verificaiton, throws an error if monitor doesn't belong to team
|
||||
@@ -171,7 +167,7 @@ class CheckService {
|
||||
};
|
||||
deleteChecksByTeamId = async ({ teamId }: { teamId: string }) => {
|
||||
if (!teamId) {
|
||||
throw this.errorService.createBadRequestError("No team ID in request");
|
||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "deleteChecksByTeamId", status: 400 });
|
||||
}
|
||||
|
||||
const deletedCount = await this.checksRepository.deleteByTeamId(teamId);
|
||||
|
||||
@@ -18,26 +18,22 @@ class IncidentService {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
|
||||
private logger: any;
|
||||
private errorService: any;
|
||||
private incidentsRepository: IIncidentsRepository;
|
||||
private monitorsRepository: IMonitorsRepository;
|
||||
private usersRepository: IUsersRepository;
|
||||
|
||||
constructor({
|
||||
logger,
|
||||
errorService,
|
||||
incidentsRepository,
|
||||
monitorsRepository,
|
||||
usersRepository,
|
||||
}: {
|
||||
logger: any;
|
||||
errorService: any;
|
||||
incidentsRepository: IIncidentsRepository;
|
||||
monitorsRepository: IMonitorsRepository;
|
||||
usersRepository: IUsersRepository;
|
||||
}) {
|
||||
this.logger = logger;
|
||||
this.errorService = errorService;
|
||||
this.incidentsRepository = incidentsRepository;
|
||||
this.monitorsRepository = monitorsRepository;
|
||||
this.usersRepository = usersRepository;
|
||||
@@ -139,7 +135,7 @@ class IncidentService {
|
||||
) => {
|
||||
try {
|
||||
if (!teamId) {
|
||||
throw this.errorService.createBadRequestError("No team ID in request");
|
||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getIncidentsByTeam", status: 400 });
|
||||
}
|
||||
|
||||
const startDate = dateRangeLookup[dateRange];
|
||||
@@ -177,7 +173,7 @@ class IncidentService {
|
||||
getIncidentSummary = async (teamId: string, limit?: string) => {
|
||||
try {
|
||||
if (!teamId) {
|
||||
throw this.errorService.createBadRequestError(" team ID in request");
|
||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getIncidentSummary", status: 400 });
|
||||
}
|
||||
|
||||
const parsedLimit = limit && Number.isFinite(parseInt(limit, 10)) ? parseInt(limit, 10) : 10;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Invite } from "@/types/index.js";
|
||||
import type { IInvitesRepository } from "@/repositories/index.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
|
||||
const SERVICE_NAME = "inviteService";
|
||||
|
||||
@@ -8,24 +9,20 @@ class InviteService {
|
||||
|
||||
private settingsService: any;
|
||||
private emailService: any;
|
||||
private errorService: any;
|
||||
private invitesRepository: IInvitesRepository;
|
||||
|
||||
constructor({
|
||||
invitesRepository,
|
||||
settingsService,
|
||||
emailService,
|
||||
errorService,
|
||||
}: {
|
||||
invitesRepository: IInvitesRepository;
|
||||
settingsService: any;
|
||||
emailService: any;
|
||||
errorService: any;
|
||||
}) {
|
||||
this.invitesRepository = invitesRepository;
|
||||
this.settingsService = settingsService;
|
||||
this.emailService = emailService;
|
||||
this.errorService = errorService;
|
||||
}
|
||||
|
||||
get serviceName() {
|
||||
@@ -48,7 +45,12 @@ class InviteService {
|
||||
});
|
||||
const result = await this.emailService.sendEmail(invite.email, "Welcome to Uptime Monitor", html);
|
||||
if (!result) {
|
||||
throw this.errorService.createServerError("Failed to send invite e-mail... Please verify your settings.");
|
||||
throw new AppError({
|
||||
message: "Failed to send invite e-mail... Please verify your settings.",
|
||||
service: SERVICE_NAME,
|
||||
method: "sendInviteEmail",
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,24 +1,21 @@
|
||||
import { IMaintenanceWindowsRepository, IMonitorsRepository } from "@/repositories/index.js";
|
||||
import { ParseBoolean } from "@/utils/utils.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
|
||||
const SERVICE_NAME = "maintenanceWindowService";
|
||||
|
||||
class MaintenanceWindowService {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
private errorService: any;
|
||||
private monitorsRepository: IMonitorsRepository;
|
||||
private maintenanceWindowsRepository: IMaintenanceWindowsRepository;
|
||||
|
||||
constructor({
|
||||
errorService,
|
||||
monitorsRepository,
|
||||
maintenanceWindowsRepository,
|
||||
}: {
|
||||
errorService: any;
|
||||
monitorsRepository: IMonitorsRepository;
|
||||
maintenanceWindowsRepository: IMaintenanceWindowsRepository;
|
||||
}) {
|
||||
this.errorService = errorService;
|
||||
this.monitorsRepository = monitorsRepository;
|
||||
this.maintenanceWindowsRepository = maintenanceWindowsRepository;
|
||||
}
|
||||
@@ -34,7 +31,12 @@ class MaintenanceWindowService {
|
||||
const unauthorizedMonitors = monitors.filter((monitor) => monitor.teamId !== teamId);
|
||||
|
||||
if (unauthorizedMonitors.length > 0) {
|
||||
throw this.errorService.createAuthorizationError();
|
||||
throw new AppError({
|
||||
message: "Unauthorized to create maintenance window for one or more monitors",
|
||||
service: SERVICE_NAME,
|
||||
method: "createMaintenanceWindow",
|
||||
status: 403,
|
||||
});
|
||||
}
|
||||
|
||||
const dbTransactions = monitorIds.map((monitorId: string) => {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { createMonitorsBodyValidation } from "@/validation/joi.js";
|
||||
import { NormalizeData, NormalizeDataUptimeDetails } from "@/utils/dataUtils.js";
|
||||
import { type Monitor } from "@/types/index.js";
|
||||
import type {
|
||||
@@ -12,8 +11,7 @@ import type { IChecksRepository, IMonitorsRepository, IMonitorStatsRepository, I
|
||||
import fs from "fs";
|
||||
import { fileURLToPath } from "url";
|
||||
import path from "path";
|
||||
|
||||
import { AppError } from "../infrastructure/errorService.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
import { ISuperSimpleQueue } from "../infrastructure/SuperSimpleQueue/SuperSimpleQueue.js";
|
||||
|
||||
const SERVICE_NAME = "MonitorService";
|
||||
@@ -75,7 +73,6 @@ export class MonitorService implements IMonitorService {
|
||||
private jobQueue: ISuperSimpleQueue;
|
||||
private emailService: any;
|
||||
private logger: any;
|
||||
private errorService: any;
|
||||
private games: any;
|
||||
private monitorsRepository: IMonitorsRepository;
|
||||
private checksRepository: IChecksRepository;
|
||||
@@ -86,7 +83,6 @@ export class MonitorService implements IMonitorService {
|
||||
jobQueue,
|
||||
emailService,
|
||||
logger,
|
||||
errorService,
|
||||
games,
|
||||
monitorsRepository,
|
||||
checksRepository,
|
||||
@@ -96,7 +92,6 @@ export class MonitorService implements IMonitorService {
|
||||
jobQueue: ISuperSimpleQueue;
|
||||
emailService: any;
|
||||
logger: any;
|
||||
errorService: any;
|
||||
games: any;
|
||||
monitorsRepository: IMonitorsRepository;
|
||||
checksRepository: IChecksRepository;
|
||||
@@ -106,7 +101,6 @@ export class MonitorService implements IMonitorService {
|
||||
this.jobQueue = jobQueue;
|
||||
this.emailService = emailService;
|
||||
this.logger = logger;
|
||||
this.errorService = errorService;
|
||||
this.games = games;
|
||||
this.monitorsRepository = monitorsRepository;
|
||||
this.checksRepository = checksRepository;
|
||||
@@ -146,7 +140,7 @@ export class MonitorService implements IMonitorService {
|
||||
createMonitor = async (teamId: string, userId: string, body: Monitor): Promise<void> => {
|
||||
const monitor = await this.monitorsRepository.create(body, teamId, userId);
|
||||
if (!monitor) {
|
||||
throw new AppError("Failed to create monitor", 500);
|
||||
throw new AppError({ message: "Failed to create monitor", status: 500, service: SERVICE_NAME, method: "createMonitor" });
|
||||
}
|
||||
|
||||
this.jobQueue.addJob(monitor.id, monitor);
|
||||
@@ -155,7 +149,7 @@ export class MonitorService implements IMonitorService {
|
||||
createBulkMonitors = async (monitors: Array<Monitor>, userId: string, teamId: string): Promise<Monitor[] | null> => {
|
||||
const createdMonitors = await this.monitorsRepository.createBulkMonitors(monitors);
|
||||
if (!monitors || monitors.length === 0) {
|
||||
throw new AppError("Failed to create monitors", 500);
|
||||
throw new AppError({ message: "Failed to create monitors", status: 500, service: SERVICE_NAME, method: "createBulkMonitors" });
|
||||
}
|
||||
|
||||
await Promise.all(createdMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor)));
|
||||
@@ -445,7 +439,7 @@ export class MonitorService implements IMonitorService {
|
||||
const messageId = await this.emailService.sendEmail(to, subject, html);
|
||||
|
||||
if (!messageId) {
|
||||
throw this.errorService.createServerError("Failed to send test email.");
|
||||
throw new AppError({ message: "Failed to send test email.", service: SERVICE_NAME, method: "sendTestEmail", status: 500 });
|
||||
}
|
||||
|
||||
return messageId;
|
||||
@@ -455,7 +449,7 @@ export class MonitorService implements IMonitorService {
|
||||
const monitors = await this.monitorsRepository.findByTeamId(teamId, {});
|
||||
|
||||
if (!monitors || monitors.length === 0) {
|
||||
throw this.errorService.createNotFoundError("No monitors to export");
|
||||
throw new AppError({ message: "No monitors found to export.", service: SERVICE_NAME, method: "exportMonitorsToJSON", status: 400 });
|
||||
}
|
||||
|
||||
return monitors;
|
||||
|
||||
@@ -48,7 +48,6 @@ class UserService {
|
||||
settingsService: any;
|
||||
logger: any;
|
||||
jwt: any;
|
||||
errorService: any;
|
||||
jobQueue: ISuperSimpleQueue;
|
||||
monitorsRepository: IMonitorsRepository;
|
||||
usersRepository: IUsersRepository;
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
export class AppError extends Error {
|
||||
constructor(message, status = 500, service = null, method = null, details = null) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.service = service;
|
||||
this.method = method;
|
||||
this.details = details;
|
||||
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
}
|
||||
}
|
||||
|
||||
class ValidationError extends AppError {
|
||||
constructor(message, details = null, service = null, method = null) {
|
||||
super(message, 422, service, method, details);
|
||||
}
|
||||
}
|
||||
|
||||
class AuthenticationError extends AppError {
|
||||
constructor(message, details = null, service = null, method = null) {
|
||||
super(message, 401, service, method, details);
|
||||
}
|
||||
}
|
||||
|
||||
class AuthorizationError extends AppError {
|
||||
constructor(message, details = null, service = null, method = null) {
|
||||
super(message, 403, service, method, details);
|
||||
}
|
||||
}
|
||||
|
||||
class NotFoundError extends AppError {
|
||||
constructor(message, details = null, service = null, method = null) {
|
||||
super(message, 404, service, method, details);
|
||||
}
|
||||
}
|
||||
|
||||
class ConflictError extends AppError {
|
||||
constructor(message, details = null, service = null, method = null) {
|
||||
super(message, 409, service, method, details);
|
||||
}
|
||||
}
|
||||
|
||||
class DatabaseError extends AppError {
|
||||
constructor(message, details = null, service = null, method = null) {
|
||||
super(message, 500, service, method, details);
|
||||
}
|
||||
}
|
||||
|
||||
class BadRequestError extends AppError {
|
||||
constructor(message, details = null, service = null, method = null) {
|
||||
super(message, 400, service, method, details);
|
||||
}
|
||||
}
|
||||
|
||||
const SERVICE_NAME = "ErrorService";
|
||||
class ErrorService {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
constructor() {}
|
||||
|
||||
get serviceName() {
|
||||
return ErrorService.SERVICE_NAME;
|
||||
}
|
||||
|
||||
createError = (message, status = 500, service = null, method = null, details = null) => {
|
||||
return new AppError(message, status, service, method, details);
|
||||
};
|
||||
|
||||
createValidationError = (message, details = null, service = null, method = null) => {
|
||||
return new ValidationError(message, details, service, method);
|
||||
};
|
||||
|
||||
createAuthenticationError = (message = "Unauthorized", details = null, service = null, method = null) => {
|
||||
return new AuthenticationError(message, details, service, method);
|
||||
};
|
||||
|
||||
createAuthorizationError = (message, details = null, service = null, method = null) => {
|
||||
return new AuthorizationError(message, details, service, method);
|
||||
};
|
||||
|
||||
createNotFoundError = (message, details = null, service = null, method = null) => {
|
||||
return new NotFoundError(message, details, service, method);
|
||||
};
|
||||
|
||||
createConflictError = (message, details = null, service = null, method = null) => {
|
||||
return new ConflictError(message, details, service, method);
|
||||
};
|
||||
|
||||
createDatabaseError = (message, details = null, service = null, method = null) => {
|
||||
return new DatabaseError(message, details, service, method);
|
||||
};
|
||||
|
||||
createServerError = (message, details = null, service = null, method = null) => {
|
||||
return this.createError(message, 500, service, method, details);
|
||||
};
|
||||
|
||||
createBadRequestError = (message = "BadRequest", details = null, service = null, method = null) => {
|
||||
return new BadRequestError(message, details, service, method);
|
||||
};
|
||||
}
|
||||
|
||||
export default ErrorService;
|
||||
Reference in new issue
Block a user