From 936279f895d5dbddd648902f9fc4c3e2fab62881 Mon Sep 17 00:00:00 2001 From: Charles Bochet Date: Sun, 28 Jul 2024 11:58:10 +0200 Subject: [PATCH] Fix incorrect fetch of userVars when users are part of multi-workspaces --- .../user-vars/services/user-vars.service.ts | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/twenty-server/src/engine/core-modules/user/user-vars/services/user-vars.service.ts b/packages/twenty-server/src/engine/core-modules/user/user-vars/services/user-vars.service.ts index 6ac037c7b..e4e345b3d 100644 --- a/packages/twenty-server/src/engine/core-modules/user/user-vars/services/user-vars.service.ts +++ b/packages/twenty-server/src/engine/core-modules/user/user-vars/services/user-vars.service.ts @@ -19,12 +19,16 @@ export class UserVarsService< workspaceId?: string; key: Extract; }): Promise { - 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( @@ -38,6 +42,7 @@ export class UserVarsService< userVarUserLevel = await this.keyValuePairService.get({ type: KeyValuePairType.USER_VAR, userId, + workspaceId: null, key, }); } @@ -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({