add clear username flag

This commit is contained in:
Alex Holliday committed 2026-08-20 13:57:24 -07:00
1 parent 4443a53a23
commit ede93add64
4 files changed
+25 -9

No files matched your search

+1 -1
View File
@@ -14,9 +14,9 @@ export const createProxyBodyValidation = z.object({
password: z.string().optional(),
});
// Empty/absent password keeps doesn't delete, only clearPassword: true does.
export const editProxyBodyValidation = createProxyBodyValidation.extend({
clearPassword: z.boolean().optional(),
clearUsername: z.boolean().optional(),
});
export const getProxyByIdParamValidation = z.object({
@@ -7,7 +7,7 @@ export interface IProxiesRepository {
findById(proxyId: string, teamId: string): Promise<Proxy>;
findByTeamId(teamId: string): Promise<Proxy[]>;
// update
updateById(proxyId: string, teamId: string, patch: Partial<Proxy>, options?: { unsetPassword?: boolean }): Promise<Proxy>;
updateById(proxyId: string, teamId: string, patch: Partial<Proxy>, options?: { unsetPassword?: boolean; unsetUsername?: boolean }): Promise<Proxy>;
// delete
deleteById(proxyId: string, teamId: string): Promise<Proxy>;
}
@@ -50,7 +50,12 @@ class MongoProxiesRepository implements IProxiesRepository {
return proxies.map(this.toEntity);
}
async updateById(proxyId: string, teamId: string, patch: Partial<Proxy>, options?: { unsetPassword?: boolean }): Promise<Proxy> {
async updateById(
proxyId: string,
teamId: string,
patch: Partial<Proxy>,
options?: { unsetPassword?: boolean; unsetUsername?: boolean }
): Promise<Proxy> {
try {
const update: UpdateQuery<ProxyDocument> = {
$set: {
@@ -59,7 +64,11 @@ class MongoProxiesRepository implements IProxiesRepository {
};
if (options?.unsetPassword) {
delete update.$set?.password;
update.$unset = { password: 1 };
update.$unset = { ...update.$unset, password: 1 };
}
if (options?.unsetUsername) {
delete update.$set?.username;
update.$unset = { ...update.$unset, username: 1 };
}
const updatedProxy = await ProxyModel.findOneAndUpdate({ _id: proxyId, teamId }, update, { new: true, runValidators: true });
if (!updatedProxy) {
+12 -5
View File
@@ -8,7 +8,7 @@ export interface IProxiesService {
createProxy(proxy: Partial<Proxy>, teamId: string): Promise<ProxyResponse>;
getProxy(proxyId: string, teamId: string): Promise<ProxyResponse>;
getProxiesByTeamId(teamId: string): Promise<ProxyResponse[]>;
updateProxy(proxyId: string, teamId: string, patch: Partial<Proxy> & { clearPassword?: boolean }): Promise<ProxyResponse>;
updateProxy(proxyId: string, teamId: string, patch: Partial<Proxy> & { clearPassword?: boolean; clearUsername?: boolean }): Promise<ProxyResponse>;
deleteProxy(proxyId: string, teamId: string): Promise<ProxyResponse>;
}
@@ -47,16 +47,23 @@ export class ProxiesService implements IProxiesService {
return clean;
};
updateProxy = async (proxyId: string, teamId: string, patch: Partial<Proxy> & { clearPassword?: boolean }): Promise<ProxyResponse> => {
// Empty/absent password keeps the stored one; clearPassword removes it
const { clearPassword, ...proxyPatch } = patch;
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;
if (!proxyPatch.password) {
delete proxyPatch.password;
}
if (!proxyPatch.username) {
delete proxyPatch.username;
}
const raw = await this.proxiesRepository.updateById(proxyId, teamId, proxyPatch, { unsetPassword: clearPassword === true });
const raw = await this.proxiesRepository.updateById(proxyId, teamId, proxyPatch, {
unsetPassword: clearPassword === true,
unsetUsername: clearUsername === true,
});
return this.toResponse(raw);
};
deleteProxy = async (proxyId: string, teamId: string): Promise<ProxyResponse> => {