Skip to content

Commit 5896780

Browse files
authored
Merge pull request #348 from TaloDev/groups-dev-data-count
Groups should account for dev data
2 parents 97de94a + 11014de commit 5896780

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

src/entities/player-group.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Game from './game'
55
import Player from './player'
66
import PlayerGroupRule, { PlayerGroupRuleCastType } from './player-group-rule'
77
import { ruleModeValidation, rulesValidation } from '../lib/groups/rulesValidation'
8+
import { devDataPlayerFilter } from '../middlewares/dev-data-middleware'
89

910
export enum RuleMode {
1011
AND = '$and',
@@ -112,10 +113,12 @@ export default class PlayerGroup {
112113
}
113114
}
114115

115-
async toJSONWithCount() {
116+
async toJSONWithCount(em: EntityManager, includeDevData: boolean) {
116117
return {
117118
...this.toJSON(),
118-
count: await this.members.loadCount()
119+
count: await this.members.loadCount({
120+
where: includeDevData ? {} : devDataPlayerFilter(em)
121+
})
119122
}
120123
}
121124
}

src/services/api/player-group-api.service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@ import PlayerGroupAPIPolicy from '../../policies/api/player-group-api.policy'
44
import PlayerGroup from '../../entities/player-group'
55
import Player from '../../entities/player'
66
import PlayerGroupAPIDocs from '../../docs/player-group-api.docs'
7+
import { EntityManager } from '@mikro-orm/mysql'
8+
import { devDataPlayerFilter } from '../../middlewares/dev-data-middleware'
79

810
type PlayerGroupWithCountAndMembers = Pick<PlayerGroup, 'id' | 'name' | 'description' | 'rules' | 'ruleMode' | 'updatedAt'> & { count: number, members?: Player[] }
911

1012
export default class PlayerGroupAPIService extends APIService {
1113
@HasPermission(PlayerGroupAPIPolicy, 'get')
1214
@Docs(PlayerGroupAPIDocs.get)
1315
async get(req: Request): Promise<Response> {
16+
const em: EntityManager = req.ctx.em
1417
const group: PlayerGroup = req.ctx.state.group
1518

16-
const groupWithCountAndMembers: PlayerGroupWithCountAndMembers = await group.toJSONWithCount()
19+
const groupWithCountAndMembers: PlayerGroupWithCountAndMembers = await group.toJSONWithCount(em, req.ctx.state.includeDevData)
1720
if (group.membersVisible) {
18-
groupWithCountAndMembers.members = await group.members.loadItems()
21+
groupWithCountAndMembers.members = await group.members.loadItems({
22+
where: req.ctx.state.includeDevData ? {} : devDataPlayerFilter(em)
23+
})
1924
}
2025

2126
return {

src/services/player-group.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class PlayerGroupService extends Service {
5252
return {
5353
status: 200,
5454
body: {
55-
groups: await Promise.all(groups.map((group) => group.toJSONWithCount()))
55+
groups: await Promise.all(groups.map((group) => group.toJSONWithCount(em, req.ctx.state.includeDevData)))
5656
}
5757
}
5858
}
@@ -95,7 +95,7 @@ export default class PlayerGroupService extends Service {
9595
return {
9696
status: 200,
9797
body: {
98-
group: await group.toJSONWithCount()
98+
group: await group.toJSONWithCount(em, req.ctx.state.includeDevData)
9999
}
100100
}
101101
}
@@ -128,7 +128,7 @@ export default class PlayerGroupService extends Service {
128128
return {
129129
status: 200,
130130
body: {
131-
group: await group.toJSONWithCount()
131+
group: await group.toJSONWithCount(em, req.ctx.state.includeDevData)
132132
}
133133
}
134134
}
@@ -253,7 +253,7 @@ export default class PlayerGroupService extends Service {
253253
orderBy: { createdAt: 'desc' }
254254
})
255255

256-
const groups = await Promise.all(pinnedGroups.map(({ group }) => group.toJSONWithCount()))
256+
const groups = await Promise.all(pinnedGroups.map(({ group }) => group.toJSONWithCount(em, req.ctx.state.includeDevData)))
257257

258258
return {
259259
status: 200,

tests/services/_api/player-group-api/index.test.ts renamed to tests/services/_api/player-group-api/get.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,49 @@ describe('Player group API service - get', () => {
7171
expect(res.body.group.count).toBe(1)
7272
expect(res.body.group.members).toBeUndefined()
7373
})
74+
75+
it('should not return dev build players in the group members without the dev data header', async () => {
76+
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYER_GROUPS])
77+
await (<EntityManager>global.em).populate(apiKey, ['game'])
78+
79+
const player = await new PlayerFactory([apiKey.game]).state(() => ({ lastSeenAt: new Date(2024, 1, 2) })).devBuild().one()
80+
const dateRule = new PlayerGroupRule(PlayerGroupRuleName.GTE, 'lastSeenAt')
81+
dateRule.castType = PlayerGroupRuleCastType.DATETIME
82+
dateRule.operands = ['2024-01-01']
83+
84+
const group = await new PlayerGroupFactory().construct(apiKey.game).state(() => ({ rules: [dateRule], membersVisible: true })).one()
85+
await (<EntityManager>global.em).persistAndFlush([player, group])
86+
87+
const res = await request(global.app)
88+
.get(`/v1/player-groups/${group.id}`)
89+
.auth(token, { type: 'bearer' })
90+
.expect(200)
91+
92+
expect(res.body.group.id).toBe(group.id)
93+
expect(res.body.group.count).toBe(0)
94+
expect(res.body.group.members).toHaveLength(0)
95+
})
96+
97+
it('should return dev build players in the group members with the dev data header', async () => {
98+
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYER_GROUPS])
99+
await (<EntityManager>global.em).populate(apiKey, ['game'])
100+
101+
const player = await new PlayerFactory([apiKey.game]).state(() => ({ lastSeenAt: new Date(2024, 1, 2) })).devBuild().one()
102+
const dateRule = new PlayerGroupRule(PlayerGroupRuleName.GTE, 'lastSeenAt')
103+
dateRule.castType = PlayerGroupRuleCastType.DATETIME
104+
dateRule.operands = ['2024-01-01']
105+
106+
const group = await new PlayerGroupFactory().construct(apiKey.game).state(() => ({ rules: [dateRule], membersVisible: true })).one()
107+
await (<EntityManager>global.em).persistAndFlush([player, group])
108+
109+
const res = await request(global.app)
110+
.get(`/v1/player-groups/${group.id}`)
111+
.auth(token, { type: 'bearer' })
112+
.set('x-talo-include-dev-data', '1')
113+
.expect(200)
114+
115+
expect(res.body.group.id).toBe(group.id)
116+
expect(res.body.group.count).toBe(1)
117+
expect(res.body.group.members).toHaveLength(1)
118+
})
74119
})

0 commit comments

Comments
 (0)