fix: fix append group member roles will occur error when operate multi users

pull/105/merge
moonrailgun 2 years ago
parent 967e671521
commit 52647a98b5

@ -320,29 +320,24 @@ export class Group extends TimeStamps implements Base {
}
/**
*
*
*
*
*/
static async updateGroupMemberField<
static async checkGroupFieldPermission<
K extends keyof Pick<GroupMember, 'roles' | 'muteUntil'>
>(
this: ReturnModelType<typeof Group>,
ctx: TcContext,
groupId: string,
memberId: string,
fieldName: K,
fieldValue: GroupMember[K] | ((member: GroupMember) => void),
operatorUserId: string
): Promise<Group> {
const group = await this.findById(groupId);
fieldName: K
) {
const userId = ctx.meta.userId;
const t = ctx.meta.t;
if (fieldName === 'roles') {
// 检查操作用户是否有管理角色的权限
const [hasRolePermission] = await call(ctx).checkUserPermissions(
groupId,
operatorUserId,
userId,
[PERMISSION.core.manageRoles]
);
if (!hasRolePermission) {
@ -352,13 +347,34 @@ export class Group extends TimeStamps implements Base {
// 检查操作用户是否有管理用户权限
const [hasUserPermission] = await call(ctx).checkUserPermissions(
groupId,
operatorUserId,
userId,
[PERMISSION.core.manageUser]
);
if (!hasUserPermission) {
throw new NoPermissionError(t('没有操作用户权限'));
}
}
}
/**
*
*
*
*/
static async updateGroupMemberField<
K extends keyof Pick<GroupMember, 'roles' | 'muteUntil'>
>(
this: ReturnModelType<typeof Group>,
ctx: TcContext,
groupId: string,
memberId: string,
fieldName: K,
fieldValue: GroupMember[K] | ((member: GroupMember) => void)
): Promise<Group> {
const group = await this.findById(groupId);
const t = ctx.meta.t;
await this.checkGroupFieldPermission(ctx, groupId, fieldName);
const member = group.members.find((m) => String(m.userId) === memberId);
if (!member) {

@ -22,6 +22,7 @@ import {
PanelFeature,
config,
SYSTEM_USERID,
db,
} from 'tailchat-server-sdk';
import moment from 'moment';
@ -655,20 +656,25 @@ class GroupService extends TcService {
}>
) {
const { groupId, memberIds, roles } = ctx.params;
const { userId } = ctx.meta;
await Promise.all(
memberIds.map((memberId) =>
this.adapter.model.updateGroupMemberField(
ctx,
groupId,
memberId,
'roles',
(member) =>
(member['roles'] = _.uniq([...member['roles'], ...roles])),
userId
)
)
await this.adapter.model.checkGroupFieldPermission(ctx, groupId, 'roles');
// 更新内容
await this.adapter.model.updateMany(
{
_id: new db.Types.ObjectId(groupId),
'members.userId': {
$in: [...memberIds],
},
},
{
$addToSet: {
'members.$[elem].roles': {
$each: roles,
},
},
},
{ arrayFilters: [{ 'elem.userId': { $in: [...memberIds] } }] }
);
const group = await this.adapter.model.findById(groupId);
@ -692,19 +698,25 @@ class GroupService extends TcService {
}>
) {
const { groupId, memberIds, roles } = ctx.params;
const { userId } = ctx.meta;
await Promise.all(
memberIds.map((memberId) =>
this.adapter.model.updateGroupMemberField(
ctx,
groupId,
memberId,
'roles',
(member) => (member['roles'] = _.without(member['roles'], ...roles)),
userId
)
)
await this.adapter.model.checkGroupFieldPermission(ctx, groupId, 'roles');
// 更新内容
await this.adapter.model.updateMany(
{
_id: new db.Types.ObjectId(groupId),
'members.userId': {
$in: [...memberIds],
},
},
{
$pull: {
'members.$[elem].roles': {
$in: roles,
},
},
},
{ arrayFilters: [{ 'elem.userId': { $in: [...memberIds] } }] }
);
const group = await this.adapter.model.findById(groupId);
@ -1112,7 +1124,6 @@ class GroupService extends TcService {
}>
) {
const { groupId, memberId, muteMs } = ctx.params;
const userId = ctx.meta.userId;
const language = ctx.meta.language;
const isUnmute = muteMs < 0;
@ -1121,8 +1132,7 @@ class GroupService extends TcService {
groupId,
memberId,
'muteUntil',
isUnmute ? undefined : new Date(new Date().valueOf() + muteMs),
userId
isUnmute ? undefined : new Date(new Date().valueOf() + muteMs)
);
this.notifyGroupInfoUpdate(ctx, group);

Loading…
Cancel
Save