Browse Source

Merge pull request #9592 from nocodb/nc-fix/add-to-fav-bug

Nc fix/add to fav bug
pull/9604/head
Pranav C 2 months ago committed by GitHub
parent
commit
8e2e1b7274
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 31
      packages/nocodb/src/models/BaseUser.ts
  2. 40
      packages/nocodb/src/services/base-users/base-users.service.ts

31
packages/nocodb/src/models/BaseUser.ts

@ -25,6 +25,9 @@ export default class BaseUser {
fk_user_id: string; fk_user_id: string;
roles?: string; roles?: string;
invited_by?: string; invited_by?: string;
starred?: boolean;
order?: number;
hidden?: boolean;
constructor(data: BaseUser) { constructor(data: BaseUser) {
Object.assign(this, data); Object.assign(this, data);
@ -94,6 +97,9 @@ export default class BaseUser {
'base_id', 'base_id',
'roles', 'roles',
'invited_by', 'invited_by',
'starred',
'order',
'hidden',
]); ]);
const { base_id, fk_user_id } = await ncMeta.metaInsert2( const { base_id, fk_user_id } = await ncMeta.metaInsert2(
@ -175,6 +181,31 @@ export default class BaseUser {
return this.castType(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<BaseUser> {
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( public static async getUsersList(
context: NcContext, context: NcContext,
{ {

40
packages/nocodb/src/services/base-users/base-users.service.ts

@ -105,7 +105,11 @@ export class BaseUsersService {
if (user) { if (user) {
// check if this user has been added to this base // 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); const base = await Base.get(context, param.baseId);
@ -113,18 +117,28 @@ export class BaseUsersService {
return NcError.baseNotFound(param.baseId); return NcError.baseNotFound(param.baseId);
} }
if (baseUser && baseUser.roles) { // if already exists and has a role then throw error
if (baseUser?.roles) {
NcError.badRequest( NcError.badRequest(
`${user.email} with role ${baseUser.roles} already exists in this base`, `${user.email} with role ${baseUser.roles} already exists in this base`,
); );
} }
// if user exist and role is not assigned then assign role by updating base user
await BaseUser.insert(context, { else if (baseUser) {
base_id: param.baseId, await BaseUser.updateRoles(
fk_user_id: user.id, context,
roles: param.baseUser.roles || 'editor', param.baseId,
invited_by: param.req?.user?.id, 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, { this.appHooksService.emit(AppEvents.PROJECT_INVITE, {
base, base,
@ -461,7 +475,13 @@ export class BaseUsersService {
if (Object.keys(baseUserData).length) { if (Object.keys(baseUserData).length) {
// create new base user if it doesn't exist // 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, { await BaseUser.insert(context, {
...baseUserData, ...baseUserData,
base_id: param.baseId, base_id: param.baseId,

Loading…
Cancel
Save