2026-08-20 11:00:05 -07:00
|
|
|
import { Proxy } from "@/domain/proxies/proxy.type.js";
|
|
|
|
|
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 {
|
|
|
|
|
createProxy(proxy: Partial<Proxy>, teamId: string): Promise<Proxy>;
|
|
|
|
|
getProxy(proxyId: string, teamId: string): Promise<Proxy>;
|
|
|
|
|
getProxiesByTeamId(teamId: string): Promise<Proxy[]>;
|
|
|
|
|
updateProxy(proxyId: string, teamId: string, patch: Partial<Proxy>): Promise<Proxy>;
|
|
|
|
|
deleteProxy(proxyId: string, teamId: string): Promise<Proxy>;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
createProxy = async (proxyData: Partial<Proxy>, teamId: string): Promise<Proxy> => {
|
|
|
|
|
proxyData.teamId = teamId;
|
|
|
|
|
return await this.proxiesRepository.create(proxyData);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getProxy = async (proxyId: string, teamId: string): Promise<Proxy> => {
|
|
|
|
|
return await this.proxiesRepository.findById(proxyId, teamId);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getProxiesByTeamId = async (teamId: string): Promise<Proxy[]> => {
|
|
|
|
|
return await this.proxiesRepository.findByTeamId(teamId);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateProxy = async (proxyId: string, teamId: string, patch: Partial<Proxy>): Promise<Proxy> => {
|
|
|
|
|
return await this.proxiesRepository.updateById(proxyId, teamId, patch);
|
|
|
|
|
};
|
|
|
|
|
deleteProxy = async (proxyId: string, teamId: string): Promise<Proxy> => {
|
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 11:00:05 -07:00
|
|
|
return await this.proxiesRepository.deleteById(proxyId, teamId);
|
|
|
|
|
};
|
|
|
|
|
}
|