Fix incorrect fetch of userVars when users are part of multi-workspaces

This commit is contained in:
Charles Bochet
2024-07-28 11:58:10 +02:00
parent dcb8c7c03a
commit 936279f895

View File

@ -19,12 +19,16 @@ export class UserVarsService<
workspaceId?: string; workspaceId?: string;
key: Extract<K, string>; key: Extract<K, string>;
}): Promise<KeyValueTypesMap[K]> { }): Promise<KeyValueTypesMap[K]> {
const userVarWorkspaceLevel = await this.keyValuePairService.get({ let userVarWorkspaceLevel: any[] = [];
type: KeyValuePairType.USER_VAR,
userId: null, if (workspaceId) {
workspaceId, userVarWorkspaceLevel = await this.keyValuePairService.get({
key, type: KeyValuePairType.USER_VAR,
}); userId: null,
workspaceId,
key,
});
}
if (userVarWorkspaceLevel.length > 1) { if (userVarWorkspaceLevel.length > 1) {
throw new Error( throw new Error(
@ -38,6 +42,7 @@ export class UserVarsService<
userVarUserLevel = await this.keyValuePairService.get({ userVarUserLevel = await this.keyValuePairService.get({
type: KeyValuePairType.USER_VAR, type: KeyValuePairType.USER_VAR,
userId, userId,
workspaceId: null,
key, key,
}); });
} }
@ -46,9 +51,28 @@ export class UserVarsService<
throw new Error(`Multiple values found for key ${key} at user level`); throw new Error(`Multiple values found for key ${key} at user level`);
} }
return mergeUserVars([...userVarUserLevel, ...userVarWorkspaceLevel]).get( let userVarWorkspaceAndUserLevel: any[] = [];
key,
) as KeyValueTypesMap[K]; if (userId && workspaceId) {
userVarWorkspaceAndUserLevel = await this.keyValuePairService.get({
type: KeyValuePairType.USER_VAR,
userId,
workspaceId,
key,
});
}
if (userVarWorkspaceAndUserLevel.length > 1) {
throw new Error(
`Multiple values found for key ${key} at workspace and user level`,
);
}
return mergeUserVars([
...userVarUserLevel,
...userVarWorkspaceLevel,
...userVarWorkspaceAndUserLevel,
]).get(key) as KeyValueTypesMap[K];
} }
public async getAll({ public async getAll({