Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit b156ddb

Browse files
committed
feat: 增加group.createGroupRole与group.deleteGroupRole
1 parent 41b0cd1 commit b156ddb

2 files changed

Lines changed: 155 additions & 4 deletions

File tree

services/core/group/group.service.ts

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ class GroupService extends TcService {
104104
},
105105
}
106106
);
107+
this.registerAction('createGroupRole', this.createGroupRole, {
108+
params: {
109+
groupId: 'string',
110+
roleName: 'string',
111+
},
112+
});
113+
this.registerAction('deleteGroupRole', this.deleteGroupRole, {
114+
params: {
115+
groupId: 'string',
116+
roleName: 'string',
117+
},
118+
});
107119
}
108120

109121
/**
@@ -215,8 +227,9 @@ class GroupService extends TcService {
215227
) {
216228
const { groupId, fieldName, fieldValue } = ctx.params;
217229
const userId = ctx.meta.userId;
230+
const t = ctx.meta.t;
218231
if (!['name', 'avatar', 'panels', 'roles'].includes(fieldName)) {
219-
throw new EntityError('该数据不允许修改');
232+
throw new EntityError(t('该数据不允许修改'));
220233
}
221234

222235
const group = await this.adapter.model.findById(groupId).exec();
@@ -238,9 +251,10 @@ class GroupService extends TcService {
238251
groupId: string;
239252
}>
240253
): Promise<boolean> {
254+
const t = ctx.meta.t;
241255
const group = await this.adapter.model.findById(ctx.params.groupId);
242256
if (!group) {
243-
throw new DataNotFoundError('没有找到群组');
257+
throw new DataNotFoundError(t('没有找到群组'));
244258
}
245259

246260
return String(group.owner) === ctx.meta.userId;
@@ -533,10 +547,11 @@ class GroupService extends TcService {
533547
*/
534548
async getGroupLobbyConverseId(ctx: TcContext<{ groupId: string }>) {
535549
const groupId = ctx.params.groupId;
550+
const t = ctx.meta.t;
536551

537552
const group = await this.adapter.model.findById(groupId);
538553
if (!group) {
539-
throw new DataNotFoundError('群组未找到');
554+
throw new DataNotFoundError(t('群组未找到'));
540555
}
541556

542557
const firstTextPanel = group.panels.find(
@@ -549,6 +564,72 @@ class GroupService extends TcService {
549564

550565
return firstTextPanel.id;
551566
}
567+
568+
/**
569+
* 创建群组角色
570+
*/
571+
async createGroupRole(ctx: TcContext<{ groupId: string; roleName: string }>) {
572+
const { groupId, roleName } = ctx.params;
573+
const userId = ctx.meta.userId;
574+
575+
const group = await this.adapter.model
576+
.findOneAndUpdate(
577+
{
578+
_id: new Types.ObjectId(groupId),
579+
owner: new Types.ObjectId(userId),
580+
},
581+
{
582+
$push: {
583+
roles: {
584+
name: roleName,
585+
permissions: [],
586+
},
587+
},
588+
},
589+
{
590+
new: true,
591+
}
592+
)
593+
.exec();
594+
595+
const json = await this.transformDocuments(ctx, {}, group);
596+
597+
this.roomcastNotify(ctx, groupId, 'updateInfo', json);
598+
599+
return json;
600+
}
601+
/**
602+
* 删除群组角色
603+
*/
604+
async deleteGroupRole(ctx: TcContext<{ groupId: string; roleName: string }>) {
605+
const { groupId, roleName } = ctx.params;
606+
const userId = ctx.meta.userId;
607+
608+
const group = await this.adapter.model
609+
.findOneAndUpdate(
610+
{
611+
_id: new Types.ObjectId(groupId),
612+
owner: new Types.ObjectId(userId),
613+
},
614+
{
615+
$pull: {
616+
roles: {
617+
name: roleName,
618+
},
619+
},
620+
},
621+
{
622+
new: true,
623+
}
624+
)
625+
.exec();
626+
627+
const json = await this.transformDocuments(ctx, {}, group);
628+
629+
this.roomcastNotify(ctx, groupId, 'updateInfo', json);
630+
631+
return json;
632+
}
552633
}
553634

554635
export default GroupService;

test/integration/group/group.spec.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function createTestGroup(
1414
owner: userId,
1515
members: [
1616
{
17-
role: ['manager'],
17+
roles: [],
1818
userId: userId,
1919
},
2020
],
@@ -23,6 +23,19 @@ function createTestGroup(
2323
};
2424
}
2525

26+
function createTestRole(
27+
name: string = generateRandomStr(),
28+
permissions: string[] = []
29+
) {
30+
const roleId = new Types.ObjectId();
31+
return {
32+
_id: roleId,
33+
id: String(roleId),
34+
name,
35+
permissions,
36+
};
37+
}
38+
2639
describe('Test "group" service', () => {
2740
const { broker, service, insertTestData } =
2841
createTestServiceBroker<GroupService>(GroupService);
@@ -274,4 +287,61 @@ describe('Test "group" service', () => {
274287
expect(res.panels.length).toBe(1);
275288
});
276289
});
290+
291+
describe('Group Roles Controllers', () => {
292+
test('Test "group.createGroupRole"', async () => {
293+
const userId = new Types.ObjectId();
294+
const testGroup = await insertTestData(createTestGroup(userId));
295+
296+
const res: Group = await broker.call(
297+
'group.createGroupRole',
298+
{
299+
groupId: String(testGroup.id),
300+
roleName: 'testRole',
301+
},
302+
{
303+
meta: {
304+
userId,
305+
},
306+
}
307+
);
308+
309+
expect(res.roles.length).toBe(1);
310+
expect(res.roles).toMatchObject([
311+
{
312+
name: 'testRole',
313+
permissions: [],
314+
},
315+
]);
316+
});
317+
318+
test('Test "group.deleteGroupRole"', async () => {
319+
const userId = new Types.ObjectId();
320+
const role = createTestRole('管理员', ['any']);
321+
const testGroup = await insertTestData(
322+
createTestGroup(userId, {
323+
roles: [role],
324+
})
325+
);
326+
327+
expect(testGroup.roles.length).toBe(1);
328+
expect(testGroup.roles).toMatchObject([role]);
329+
330+
const res: Group = await broker.call(
331+
'group.deleteGroupRole',
332+
{
333+
groupId: String(testGroup.id),
334+
roleName: '管理员',
335+
},
336+
{
337+
meta: {
338+
userId,
339+
},
340+
}
341+
);
342+
343+
expect(res.roles.length).toBe(0);
344+
expect(res.roles).toEqual([]);
345+
});
346+
});
277347
});

0 commit comments

Comments
 (0)