Skip to content

Commit 936279f

Browse files
committed
Fix incorrect fetch of userVars when users are part of multi-workspaces
1 parent dcb8c7c commit 936279f

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

packages/twenty-server/src/engine/core-modules/user/user-vars/services/user-vars.service.ts

+33-9
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ export class UserVarsService<
1919
workspaceId?: string;
2020
key: Extract<K, string>;
2121
}): Promise<KeyValueTypesMap[K]> {
22-
const userVarWorkspaceLevel = await this.keyValuePairService.get({
23-
type: KeyValuePairType.USER_VAR,
24-
userId: null,
25-
workspaceId,
26-
key,
27-
});
22+
let userVarWorkspaceLevel: any[] = [];
23+
24+
if (workspaceId) {
25+
userVarWorkspaceLevel = await this.keyValuePairService.get({
26+
type: KeyValuePairType.USER_VAR,
27+
userId: null,
28+
workspaceId,
29+
key,
30+
});
31+
}
2832

2933
if (userVarWorkspaceLevel.length > 1) {
3034
throw new Error(
@@ -38,6 +42,7 @@ export class UserVarsService<
3842
userVarUserLevel = await this.keyValuePairService.get({
3943
type: KeyValuePairType.USER_VAR,
4044
userId,
45+
workspaceId: null,
4146
key,
4247
});
4348
}
@@ -46,9 +51,28 @@ export class UserVarsService<
4651
throw new Error(`Multiple values found for key ${key} at user level`);
4752
}
4853

49-
return mergeUserVars([...userVarUserLevel, ...userVarWorkspaceLevel]).get(
50-
key,
51-
) as KeyValueTypesMap[K];
54+
let userVarWorkspaceAndUserLevel: any[] = [];
55+
56+
if (userId && workspaceId) {
57+
userVarWorkspaceAndUserLevel = await this.keyValuePairService.get({
58+
type: KeyValuePairType.USER_VAR,
59+
userId,
60+
workspaceId,
61+
key,
62+
});
63+
}
64+
65+
if (userVarWorkspaceAndUserLevel.length > 1) {
66+
throw new Error(
67+
`Multiple values found for key ${key} at workspace and user level`,
68+
);
69+
}
70+
71+
return mergeUserVars([
72+
...userVarUserLevel,
73+
...userVarWorkspaceLevel,
74+
...userVarWorkspaceAndUserLevel,
75+
]).get(key) as KeyValueTypesMap[K];
5276
}
5377

5478
public async getAll({

0 commit comments

Comments
 (0)