add clear username flag
This commit is contained in:
1 parent
4443a53a23
commit
ede93add64
4 files changed
+25
-9
No files matched your search
@@ -14,9 +14,9 @@ export const createProxyBodyValidation = z.object({
|
|||||||
password: z.string().optional(),
|
password: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Empty/absent password keeps doesn't delete, only clearPassword: true does.
|
|
||||||
export const editProxyBodyValidation = createProxyBodyValidation.extend({
|
export const editProxyBodyValidation = createProxyBodyValidation.extend({
|
||||||
clearPassword: z.boolean().optional(),
|
clearPassword: z.boolean().optional(),
|
||||||
|
clearUsername: z.boolean().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getProxyByIdParamValidation = z.object({
|
export const getProxyByIdParamValidation = z.object({
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ export interface IProxiesRepository {
|
|||||||
findById(proxyId: string, teamId: string): Promise<Proxy>;
|
findById(proxyId: string, teamId: string): Promise<Proxy>;
|
||||||
findByTeamId(teamId: string): Promise<Proxy[]>;
|
findByTeamId(teamId: string): Promise<Proxy[]>;
|
||||||
// update
|
// 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
|
// delete
|
||||||
deleteById(proxyId: string, teamId: string): Promise<Proxy>;
|
deleteById(proxyId: string, teamId: string): Promise<Proxy>;
|
||||||
}
|
}
|
||||||
@@ -50,7 +50,12 @@ class MongoProxiesRepository implements IProxiesRepository {
|
|||||||
return proxies.map(this.toEntity);
|
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 {
|
try {
|
||||||
const update: UpdateQuery<ProxyDocument> = {
|
const update: UpdateQuery<ProxyDocument> = {
|
||||||
$set: {
|
$set: {
|
||||||
@@ -59,7 +64,11 @@ class MongoProxiesRepository implements IProxiesRepository {
|
|||||||
};
|
};
|
||||||
if (options?.unsetPassword) {
|
if (options?.unsetPassword) {
|
||||||
delete update.$set?.password;
|
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 });
|
const updatedProxy = await ProxyModel.findOneAndUpdate({ _id: proxyId, teamId }, update, { new: true, runValidators: true });
|
||||||
if (!updatedProxy) {
|
if (!updatedProxy) {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ export interface IProxiesService {
|
|||||||
createProxy(proxy: Partial<Proxy>, teamId: string): Promise<ProxyResponse>;
|
createProxy(proxy: Partial<Proxy>, teamId: string): Promise<ProxyResponse>;
|
||||||
getProxy(proxyId: string, teamId: string): Promise<ProxyResponse>;
|
getProxy(proxyId: string, teamId: string): Promise<ProxyResponse>;
|
||||||
getProxiesByTeamId(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>;
|
deleteProxy(proxyId: string, teamId: string): Promise<ProxyResponse>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,16 +47,23 @@ export class ProxiesService implements IProxiesService {
|
|||||||
return clean;
|
return clean;
|
||||||
};
|
};
|
||||||
|
|
||||||
updateProxy = async (proxyId: string, teamId: string, patch: Partial<Proxy> & { clearPassword?: boolean }): Promise<ProxyResponse> => {
|
updateProxy = async (
|
||||||
// Empty/absent password keeps the stored one; clearPassword removes it
|
proxyId: string,
|
||||||
const { clearPassword, ...proxyPatch } = patch;
|
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) {
|
if (!proxyPatch.password) {
|
||||||
delete proxyPatch.password;
|
delete proxyPatch.password;
|
||||||
}
|
}
|
||||||
if (!proxyPatch.username) {
|
if (!proxyPatch.username) {
|
||||||
delete 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);
|
return this.toResponse(raw);
|
||||||
};
|
};
|
||||||
deleteProxy = async (proxyId: string, teamId: string): Promise<ProxyResponse> => {
|
deleteProxy = async (proxyId: string, teamId: string): Promise<ProxyResponse> => {
|
||||||
|
|||||||
Reference in new issue
Block a user