Skip to content

Commit

Permalink
groups should account for dev data
Browse files Browse the repository at this point in the history
  • Loading branch information
tudddorrr committed Oct 22, 2024
1 parent 97de94a commit 11014de
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/entities/player-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Game from './game'
import Player from './player'
import PlayerGroupRule, { PlayerGroupRuleCastType } from './player-group-rule'
import { ruleModeValidation, rulesValidation } from '../lib/groups/rulesValidation'
import { devDataPlayerFilter } from '../middlewares/dev-data-middleware'

export enum RuleMode {
AND = '$and',
Expand Down Expand Up @@ -112,10 +113,12 @@ export default class PlayerGroup {
}
}

async toJSONWithCount() {
async toJSONWithCount(em: EntityManager, includeDevData: boolean) {
return {
...this.toJSON(),
count: await this.members.loadCount()
count: await this.members.loadCount({
where: includeDevData ? {} : devDataPlayerFilter(em)
})
}
}
}
9 changes: 7 additions & 2 deletions src/services/api/player-group-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import PlayerGroupAPIPolicy from '../../policies/api/player-group-api.policy'
import PlayerGroup from '../../entities/player-group'
import Player from '../../entities/player'
import PlayerGroupAPIDocs from '../../docs/player-group-api.docs'
import { EntityManager } from '@mikro-orm/mysql'
import { devDataPlayerFilter } from '../../middlewares/dev-data-middleware'

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

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

const groupWithCountAndMembers: PlayerGroupWithCountAndMembers = await group.toJSONWithCount()
const groupWithCountAndMembers: PlayerGroupWithCountAndMembers = await group.toJSONWithCount(em, req.ctx.state.includeDevData)
if (group.membersVisible) {
groupWithCountAndMembers.members = await group.members.loadItems()
groupWithCountAndMembers.members = await group.members.loadItems({
where: req.ctx.state.includeDevData ? {} : devDataPlayerFilter(em)
})
}

return {
Expand Down
8 changes: 4 additions & 4 deletions src/services/player-group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class PlayerGroupService extends Service {
return {
status: 200,
body: {
groups: await Promise.all(groups.map((group) => group.toJSONWithCount()))
groups: await Promise.all(groups.map((group) => group.toJSONWithCount(em, req.ctx.state.includeDevData)))
}
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ export default class PlayerGroupService extends Service {
return {
status: 200,
body: {
group: await group.toJSONWithCount()
group: await group.toJSONWithCount(em, req.ctx.state.includeDevData)
}
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ export default class PlayerGroupService extends Service {
return {
status: 200,
body: {
group: await group.toJSONWithCount()
group: await group.toJSONWithCount(em, req.ctx.state.includeDevData)
}
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ export default class PlayerGroupService extends Service {
orderBy: { createdAt: 'desc' }
})

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

return {
status: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,49 @@ describe('Player group API service - get', () => {
expect(res.body.group.count).toBe(1)
expect(res.body.group.members).toBeUndefined()
})

it('should not return dev build players in the group members without the dev data header', async () => {
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYER_GROUPS])
await (<EntityManager>global.em).populate(apiKey, ['game'])

const player = await new PlayerFactory([apiKey.game]).state(() => ({ lastSeenAt: new Date(2024, 1, 2) })).devBuild().one()
const dateRule = new PlayerGroupRule(PlayerGroupRuleName.GTE, 'lastSeenAt')
dateRule.castType = PlayerGroupRuleCastType.DATETIME
dateRule.operands = ['2024-01-01']

const group = await new PlayerGroupFactory().construct(apiKey.game).state(() => ({ rules: [dateRule], membersVisible: true })).one()
await (<EntityManager>global.em).persistAndFlush([player, group])

const res = await request(global.app)
.get(`/v1/player-groups/${group.id}`)
.auth(token, { type: 'bearer' })
.expect(200)

expect(res.body.group.id).toBe(group.id)
expect(res.body.group.count).toBe(0)
expect(res.body.group.members).toHaveLength(0)
})

it('should return dev build players in the group members with the dev data header', async () => {
const [apiKey, token] = await createAPIKeyAndToken([APIKeyScope.READ_PLAYER_GROUPS])
await (<EntityManager>global.em).populate(apiKey, ['game'])

const player = await new PlayerFactory([apiKey.game]).state(() => ({ lastSeenAt: new Date(2024, 1, 2) })).devBuild().one()
const dateRule = new PlayerGroupRule(PlayerGroupRuleName.GTE, 'lastSeenAt')
dateRule.castType = PlayerGroupRuleCastType.DATETIME
dateRule.operands = ['2024-01-01']

const group = await new PlayerGroupFactory().construct(apiKey.game).state(() => ({ rules: [dateRule], membersVisible: true })).one()
await (<EntityManager>global.em).persistAndFlush([player, group])

const res = await request(global.app)
.get(`/v1/player-groups/${group.id}`)
.auth(token, { type: 'bearer' })
.set('x-talo-include-dev-data', '1')
.expect(200)

expect(res.body.group.id).toBe(group.id)
expect(res.body.group.count).toBe(1)
expect(res.body.group.members).toHaveLength(1)
})
})

0 comments on commit 11014de

Please sign in to comment.