diff --git a/packages/nocodb/src/models/BaseUser.ts b/packages/nocodb/src/models/BaseUser.ts index 4fcf770c93..c4c5ca2465 100644 --- a/packages/nocodb/src/models/BaseUser.ts +++ b/packages/nocodb/src/models/BaseUser.ts @@ -25,6 +25,9 @@ export default class BaseUser { fk_user_id: string; roles?: string; invited_by?: string; + starred?: boolean; + order?: number; + hidden?: boolean; constructor(data: BaseUser) { Object.assign(this, data); @@ -94,6 +97,9 @@ export default class BaseUser { 'base_id', 'roles', 'invited_by', + 'starred', + 'order', + 'hidden', ]); const { base_id, fk_user_id } = await ncMeta.metaInsert2( @@ -175,6 +181,31 @@ export default class BaseUser { return this.castType(baseUser); } + /** + * Check if user entry exists in BaseUser table + * @param context + * @param baseId + * @param userId + * @param ncMeta + */ + public static async getBaseUserFromDb( + context: NcContext, + baseId: string, + userId: string, + ncMeta = Noco.ncMeta, + ): Promise { + const baseUser = await ncMeta.metaGet2( + context.workspace_id, + context.base_id, + MetaTable.PROJECT_USERS, + { + base_id: baseId, + fk_user_id: userId, + }, + ) + return baseUser && this.castType(baseUser); + } + public static async getUsersList( context: NcContext, { diff --git a/packages/nocodb/src/services/base-users/base-users.service.ts b/packages/nocodb/src/services/base-users/base-users.service.ts index 35e7c8bece..93c6dd94f4 100644 --- a/packages/nocodb/src/services/base-users/base-users.service.ts +++ b/packages/nocodb/src/services/base-users/base-users.service.ts @@ -105,7 +105,11 @@ export class BaseUsersService { if (user) { // check if this user has been added to this base - const baseUser = await BaseUser.get(context, param.baseId, user.id); + const baseUser = await BaseUser.getBaseUserFromDb( + context, + param.baseId, + user.id, + ); const base = await Base.get(context, param.baseId); @@ -113,18 +117,28 @@ export class BaseUsersService { return NcError.baseNotFound(param.baseId); } - if (baseUser && baseUser.roles) { + // if already exists and has a role then throw error + if (baseUser?.roles) { NcError.badRequest( `${user.email} with role ${baseUser.roles} already exists in this base`, ); } - - await BaseUser.insert(context, { - base_id: param.baseId, - fk_user_id: user.id, - roles: param.baseUser.roles || 'editor', - invited_by: param.req?.user?.id, - }); + // if user exist and role is not assigned then assign role by updating base user + else if (baseUser) { + await BaseUser.updateRoles( + context, + param.baseId, + user.id, + param.baseUser.roles, + ); + } else { + await BaseUser.insert(context, { + base_id: param.baseId, + fk_user_id: user.id, + roles: param.baseUser.roles || 'editor', + invited_by: param.req?.user?.id, + }); + } this.appHooksService.emit(AppEvents.PROJECT_INVITE, { base, @@ -461,7 +475,13 @@ export class BaseUsersService { if (Object.keys(baseUserData).length) { // create new base user if it doesn't exist - if (!(await BaseUser.get(context, param.baseId, param.user?.id))) { + if ( + !(await BaseUser.getBaseUserFromDb( + context, + param.baseId, + param.user?.id, + )) + ) { await BaseUser.insert(context, { ...baseUserData, base_id: param.baseId,