Files
Checkmate/server/src/domain/proxies/proxy.service.ts
T

111 lines
4.0 KiB
TypeScript
Raw Normal View History

import { Proxy, ProxyResponse, ProxySummary } from "@/domain/proxies/proxy.type.js";
2026-08-20 11:00:05 -07:00
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
import { IProxiesRepository } from "@/domain/proxies/proxy.repository.interface.js";
2026-08-20 11:31:51 -07:00
import { AppError } from "@/utils/AppError.js";
import { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
2026-08-20 11:00:05 -07:00
export interface IProxiesService {
2026-08-20 13:52:14 -07:00
createProxy(proxy: Partial<Proxy>, teamId: string): Promise<ProxyResponse>;
getProxy(proxyId: string, teamId: string): Promise<ProxyResponse>;
2026-08-25 11:02:37 -07:00
getProxies(): Promise<ProxyResponse[]>;
getProxySummary(proxyId: string): Promise<ProxySummary | null>;
2026-08-20 13:52:14 -07:00
getProxiesByTeamId(teamId: string): Promise<ProxyResponse[]>;
2026-08-20 13:57:24 -07:00
updateProxy(proxyId: string, teamId: string, patch: Partial<Proxy> & { clearPassword?: boolean; clearUsername?: boolean }): Promise<ProxyResponse>;
2026-08-20 13:52:14 -07:00
deleteProxy(proxyId: string, teamId: string): Promise<ProxyResponse>;
2026-08-20 11:00:05 -07:00
}
2026-08-20 11:31:51 -07:00
const SERVICE_NAME = "ProxiesService";
2026-08-20 11:00:05 -07:00
export class ProxiesService implements IProxiesService {
constructor(
private proxiesRepository: IProxiesRepository,
2026-08-20 11:31:51 -07:00
private monitorsRepository: IMonitorsRepository,
private settingsService: ISettingsService
2026-08-20 11:00:05 -07:00
) {}
2026-08-20 13:52:14 -07:00
private toResponse = ({ password, ...rest }: Proxy): ProxyResponse => ({ ...rest, hasPassword: Boolean(password) });
createProxy = async (proxyData: Partial<Proxy>, teamId: string): Promise<ProxyResponse> => {
2026-08-20 11:00:05 -07:00
proxyData.teamId = teamId;
2026-08-20 13:52:14 -07:00
if (!proxyData.password) {
delete proxyData.password;
}
if (!proxyData.username) {
delete proxyData.username;
}
const raw = await this.proxiesRepository.create(proxyData);
return this.toResponse(raw);
2026-08-20 11:00:05 -07:00
};
2026-08-20 13:52:14 -07:00
getProxy = async (proxyId: string, teamId: string): Promise<ProxyResponse> => {
const raw = await this.proxiesRepository.findById(proxyId, teamId);
return this.toResponse(raw);
2026-08-20 11:00:05 -07:00
};
2026-08-25 11:02:37 -07:00
getProxies = async (): Promise<ProxyResponse[]> => {
const raw = await this.proxiesRepository.findAll();
const clean = raw.map((r) => {
return this.toResponse(r);
});
return clean;
};
getProxySummary = async (proxyId: string): Promise<ProxySummary | null> => {
const proxy = await this.proxiesRepository.findByIdOrNull(proxyId);
if (!proxy) {
return null;
}
const { id, name, host, port } = proxy;
return { id, name, host, port };
};
2026-08-20 13:52:14 -07:00
getProxiesByTeamId = async (teamId: string): Promise<ProxyResponse[]> => {
const raw = await this.proxiesRepository.findByTeamId(teamId);
const clean = raw.map((r) => {
return this.toResponse(r);
});
return clean;
2026-08-20 11:00:05 -07:00
};
2026-08-20 13:57:24 -07:00
updateProxy = async (
proxyId: string,
teamId: string,
patch: Partial<Proxy> & { clearPassword?: boolean; clearUsername?: boolean }
): Promise<ProxyResponse> => {
// Empty/absent password and username don't delete, clearPassword / clearUsername does
const { clearPassword, clearUsername, ...proxyPatch } = patch;
2026-08-20 13:52:14 -07:00
if (!proxyPatch.password) {
delete proxyPatch.password;
}
if (!proxyPatch.username) {
delete proxyPatch.username;
}
2026-08-20 13:57:24 -07:00
const raw = await this.proxiesRepository.updateById(proxyId, teamId, proxyPatch, {
unsetPassword: clearPassword === true,
unsetUsername: clearUsername === true,
});
2026-08-20 13:52:14 -07:00
return this.toResponse(raw);
2026-08-20 11:00:05 -07:00
};
2026-08-20 13:52:14 -07:00
deleteProxy = async (proxyId: string, teamId: string): Promise<ProxyResponse> => {
2026-08-20 11:31:51 -07:00
// Make sure proxy is not in use
const monitorsUsingProxy = await this.monitorsRepository.findMonitorCountByProxyId(proxyId);
if (monitorsUsingProxy > 0) {
throw new AppError({
message: `Proxy still in use by ${monitorsUsingProxy} monitor${monitorsUsingProxy > 1 ? "s" : ""}`,
service: SERVICE_NAME,
status: 409,
});
}
// Make sure this is not set as a global proxy
const settings = await this.settingsService.getDBSettings();
if (settings.globalProxyId === proxyId) {
throw new AppError({
message: "Proxy is set as the global proxy in settings",
service: SERVICE_NAME,
status: 409,
});
}
2026-08-20 13:52:14 -07:00
const raw = await this.proxiesRepository.deleteById(proxyId, teamId);
return this.toResponse(raw);
2026-08-20 11:00:05 -07:00
};
}