From 5435167e73d79ecf046fe713c21e873818c19855 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Thu, 20 Aug 2026 10:49:12 -0700 Subject: [PATCH] add proxy repository and interface --- .../proxies/proxy.repository.interface.ts | 13 +++ .../domain/proxies/proxy.repository.mongo.ts | 84 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 server/src/domain/proxies/proxy.repository.interface.ts create mode 100644 server/src/domain/proxies/proxy.repository.mongo.ts diff --git a/server/src/domain/proxies/proxy.repository.interface.ts b/server/src/domain/proxies/proxy.repository.interface.ts new file mode 100644 index 000000000..bfc2e3ab1 --- /dev/null +++ b/server/src/domain/proxies/proxy.repository.interface.ts @@ -0,0 +1,13 @@ +import { Proxy } from "@/domain/proxies/proxy.type.js"; + +export interface IProxiesRepository { + // create + create(proxyData: Partial): Promise; + // read + findById(proxyId: string, teamId: string): Promise; + findByTeamId(teamId: string): Promise; + // update + updateById(proxyId: string, teamId: string, patch: Partial): Promise; + // delete + deleteById(proxyId: string, teamId: string): Promise; +} diff --git a/server/src/domain/proxies/proxy.repository.mongo.ts b/server/src/domain/proxies/proxy.repository.mongo.ts new file mode 100644 index 000000000..8890fe66f --- /dev/null +++ b/server/src/domain/proxies/proxy.repository.mongo.ts @@ -0,0 +1,84 @@ +import { IProxiesRepository } from "@/domain/proxies/proxy.repository.interface.js"; +import { Proxy } from "@/domain/proxies/proxy.type.js"; +import { ProxyDocument, ProxyModel } from "@/domain/proxies/proxy.model.js"; +import { AppError } from "@/utils/AppError.js"; +import { toStringId, toDateString } from "@/utils/mongoMappers.js"; + +const SERVICE_NAME = "ProxiesRepository"; + +class MongoProxiesRepository implements IProxiesRepository { + static SERVICE_NAME = SERVICE_NAME; + + private toEntity = (doc: ProxyDocument): Proxy => { + return { + id: toStringId(doc._id), + teamId: toStringId(doc.teamId), + name: doc.name, + protocol: doc.protocol, + host: doc.host, + port: doc.port, + username: doc.username, + password: doc.password, + createdAt: toDateString(doc.createdAt), + updatedAt: toDateString(doc.updatedAt), + }; + }; + + async create(proxyData: Partial): Promise { + try { + const proxy = await ProxyModel.create({ ...proxyData }); + return this.toEntity(proxy); + } catch (error) { + if (error && typeof error === "object" && (error as { code?: number }).code === 11000) { + throw new AppError({ message: `A proxy named "${proxyData.name}" already exists`, service: SERVICE_NAME, status: 409 }); + } + throw error; + } + } + + async findById(proxyId: string, teamId: string): Promise { + const proxy = await ProxyModel.findOne({ _id: proxyId, teamId }); + if (!proxy) { + throw new AppError({ message: "Proxy not found", service: SERVICE_NAME, status: 404 }); + } + return this.toEntity(proxy); + } + + async findByTeamId(teamId: string): Promise { + const proxies = await ProxyModel.find({ teamId }); + return proxies.map(this.toEntity); + } + + async updateById(proxyId: string, teamId: string, patch: Partial): Promise { + try { + const updatedProxy = await ProxyModel.findOneAndUpdate( + { _id: proxyId, teamId }, + { + $set: { + ...patch, + }, + }, + { new: true, runValidators: true } + ); + if (!updatedProxy) { + throw new AppError({ message: "Proxy not found or could not be updated", service: SERVICE_NAME, status: 404 }); + } + return this.toEntity(updatedProxy); + } catch (error) { + if (error && typeof error === "object" && (error as { code?: number }).code === 11000) { + throw new AppError({ message: `A proxy named "${patch.name}" already exists`, service: SERVICE_NAME, status: 409 }); + } + throw error; + } + } + + async deleteById(proxyId: string, teamId: string): Promise { + const deletedProxy = await ProxyModel.findOneAndDelete({ _id: proxyId, teamId }); + if (!deletedProxy) { + throw new AppError({ message: "Proxy not found or could not be deleted", service: SERVICE_NAME, status: 404 }); + } + return this.toEntity(deletedProxy); + } +} + +export default MongoProxiesRepository;