wire repo and service
This commit is contained in:
1 parent
edd9335e96
commit
6e3252aec8
5 files changed
+41
-5
No files matched your search
@@ -53,6 +53,7 @@ export interface IMonitorsRepository {
|
||||
|
||||
// counts
|
||||
findMonitorCountByTeamIdAndType(teamId: string, config: TeamQueryConfig): Promise<number>;
|
||||
findMonitorCountByProxyId(proxyId: string): Promise<number>;
|
||||
|
||||
// other
|
||||
findMonitorsSummaryByTeamId(teamId: string, config?: SummaryConfig): Promise<MonitorsSummary>;
|
||||
|
||||
@@ -172,6 +172,11 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
||||
return count;
|
||||
};
|
||||
|
||||
findMonitorCountByProxyId = async (proxyId: string): Promise<number> => {
|
||||
const count = await MonitorModel.countDocuments({ proxyId });
|
||||
return count;
|
||||
};
|
||||
|
||||
updateById = async (monitorId: string, teamId: string, patch: Partial<Monitor>) => {
|
||||
const updatedMonitor = await MonitorModel.findOneAndUpdate(
|
||||
{ _id: monitorId, teamId },
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
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";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
import { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
|
||||
|
||||
export interface IProxiesService {
|
||||
createProxy(proxy: Partial<Proxy>, teamId: string): Promise<Proxy>;
|
||||
@@ -10,10 +12,12 @@ export interface IProxiesService {
|
||||
deleteProxy(proxyId: string, teamId: string): Promise<Proxy>;
|
||||
}
|
||||
|
||||
const SERVICE_NAME = "ProxiesService";
|
||||
export class ProxiesService implements IProxiesService {
|
||||
constructor(
|
||||
private proxiesRepository: IProxiesRepository,
|
||||
private monitorsRepository: IMonitorsRepository
|
||||
private monitorsRepository: IMonitorsRepository,
|
||||
private settingsService: ISettingsService
|
||||
) {}
|
||||
|
||||
createProxy = async (proxyData: Partial<Proxy>, teamId: string): Promise<Proxy> => {
|
||||
@@ -33,7 +37,25 @@ export class ProxiesService implements IProxiesService {
|
||||
return await this.proxiesRepository.updateById(proxyId, teamId, patch);
|
||||
};
|
||||
deleteProxy = async (proxyId: string, teamId: string): Promise<Proxy> => {
|
||||
// TODO guard
|
||||
// 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,
|
||||
});
|
||||
}
|
||||
return await this.proxiesRepository.deleteById(proxyId, teamId);
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user