Skip to content

Commit

Permalink
Fix incorrect fetch of userVars when users are part of multi-workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Jul 28, 2024
1 parent dcb8c7c commit 936279f
Showing 1 changed file with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ export class UserVarsService<
workspaceId?: string;
key: Extract<K, string>;
}): Promise<KeyValueTypesMap[K]> {
const userVarWorkspaceLevel = await this.keyValuePairService.get({
type: KeyValuePairType.USER_VAR,
userId: null,
workspaceId,
key,
});
let userVarWorkspaceLevel: any[] = [];

if (workspaceId) {
userVarWorkspaceLevel = await this.keyValuePairService.get({
type: KeyValuePairType.USER_VAR,
userId: null,
workspaceId,
key,
});
}

if (userVarWorkspaceLevel.length > 1) {
throw new Error(
Expand All @@ -38,6 +42,7 @@ export class UserVarsService<
userVarUserLevel = await this.keyValuePairService.get({
type: KeyValuePairType.USER_VAR,
userId,
workspaceId: null,
key,
});
}
Expand All @@ -46,9 +51,28 @@ export class UserVarsService<
throw new Error(`Multiple values found for key ${key} at user level`);
}

return mergeUserVars([...userVarUserLevel, ...userVarWorkspaceLevel]).get(
key,
) as KeyValueTypesMap[K];
let userVarWorkspaceAndUserLevel: any[] = [];

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({
Expand Down

0 comments on commit 936279f

Please sign in to comment.