Skip to content

Commit 36aad1d

Browse files
committed
return count with recently created group
1 parent bf3d0fc commit 36aad1d

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/services/player-group.service.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { ruleModeValidation, rulesValidation } from '../lib/groups/rulesValidati
77
import createGameActivity from '../lib/logging/createGameActivity'
88
import PlayerGroupPolicy from '../policies/player-group.policy'
99

10+
type PlayerGroupWithCount = Pick<PlayerGroup, 'id' | 'name' | 'description' | 'rules' | 'ruleMode' | 'updatedAt'> & { count: number }
11+
1012
@Routes([
1113
{
1214
method: 'GET'
@@ -32,16 +34,22 @@ import PlayerGroupPolicy from '../policies/player-group.policy'
3234
}
3335
])
3436
export default class PlayerGroupService extends Service {
37+
private async groupWithCount(group: PlayerGroup): Promise<PlayerGroupWithCount> {
38+
return {
39+
...group.toJSON(),
40+
count: await group.members.loadCount()
41+
}
42+
}
43+
3544
@HasPermission(PlayerGroupPolicy, 'index')
3645
async index(req: Request): Promise<Response> {
3746
const em: EntityManager = req.ctx.em
3847
const groups = await em.getRepository(PlayerGroup).find({ game: req.ctx.state.game })
39-
const counts = await Promise.all(groups.map(async (group) => await group.members.loadCount()))
4048

4149
return {
4250
status: 200,
4351
body: {
44-
groups: groups.map((group, idx) => ({ ...group.toJSON(), count: counts[idx] }))
52+
groups: await Promise.all(groups.map(this.groupWithCount))
4553
}
4654
}
4755
}
@@ -83,7 +91,7 @@ export default class PlayerGroupService extends Service {
8391
return {
8492
status: 200,
8593
body: {
86-
group: { ...group.toJSON(), count: await group.members.loadCount() }
94+
group: await this.groupWithCount(group)
8795
}
8896
}
8997
}

tests/services/player-group/post.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import GameActivity, { GameActivityType } from '../../../src/entities/game-activ
88
import PlayerGroupRule, { PlayerGroupRuleCastType, PlayerGroupRuleName } from '../../../src/entities/player-group-rule'
99
import PlayerProp from '../../../src/entities/player-prop'
1010
import PlayerFactory from '../../fixtures/PlayerFactory'
11-
import PlayerGroup from '../../../src/entities/player-group'
1211

1312
describe('Player group service - post', () => {
1413
it.each(userPermissionProvider([
@@ -48,6 +47,7 @@ describe('Player group service - post', () => {
4847
expect(res.body.group.name).toBe('Winners')
4948
expect(res.body.group.description).toBe('People who have completed the game')
5049
expect(res.body.group.rules).toStrictEqual(rules)
50+
expect(res.body.group.count).toBe(0)
5151

5252
expect(activity.extra.groupName).toBe(res.body.group.name)
5353
} else {
@@ -90,10 +90,7 @@ describe('Player group service - post', () => {
9090
.auth(token, { type: 'bearer' })
9191
.expect(200)
9292

93-
const newGroupId = res.body.group.id
94-
const group = await (<EntityManager>global.em).getRepository(PlayerGroup).findOne(newGroupId)
95-
const count = await group.members.loadCount()
96-
expect(count).toBe(1)
93+
expect(res.body.group.count).toBe(1)
9794
})
9895

9996
it('should require a valid ruleMode', async () => {

0 commit comments

Comments
 (0)