-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add currency field + base api changes (#709)
* feat: add currency field + base api changes * fix: remove a debug msg
- Loading branch information
1 parent
a339c70
commit c6518f9
Showing
10 changed files
with
723 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/app-api/src/controllers/PlayerOnGameserverController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { IsNumber, IsOptional, IsString, IsUUID, ValidateNested } from 'class-validator'; | ||
import { ITakaroQuery } from '@takaro/db'; | ||
import { APIOutput, apiResponse } from '@takaro/http'; | ||
import { AuthenticatedRequest, AuthService } from '../service/AuthService.js'; | ||
import { Body, Get, Post, JsonController, UseBefore, Req, Params, Res } from 'routing-controllers'; | ||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; | ||
import { Type } from 'class-transformer'; | ||
import { ParamId } from '../lib/validators.js'; | ||
import { PERMISSIONS } from '@takaro/auth'; | ||
import { Response } from 'express'; | ||
import { PlayerOnGameServerService, PlayerOnGameserverOutputDTO } from '../service/PlayerOnGameserverService.js'; | ||
|
||
export class PlayerOnGameserverOutputDTOAPI extends APIOutput<PlayerOnGameserverOutputDTO> { | ||
@Type(() => PlayerOnGameserverOutputDTO) | ||
@ValidateNested() | ||
declare data: PlayerOnGameserverOutputDTO; | ||
} | ||
|
||
export class PlayerOnGameserverOutputArrayDTOAPI extends APIOutput<PlayerOnGameserverOutputDTO[]> { | ||
@Type(() => PlayerOnGameserverOutputDTO) | ||
@ValidateNested({ each: true }) | ||
declare data: PlayerOnGameserverOutputDTO[]; | ||
} | ||
|
||
class PlayerOnGameServerSearchInputAllowedFilters { | ||
@IsOptional() | ||
@IsUUID(4, { each: true }) | ||
id!: string[]; | ||
|
||
@IsOptional() | ||
@IsString({ each: true }) | ||
gameId!: string; | ||
|
||
@IsUUID(4, { each: true }) | ||
@IsOptional() | ||
gameServerId!: string; | ||
|
||
@IsUUID(4, { each: true }) | ||
@IsOptional() | ||
playerId!: string; | ||
} | ||
|
||
class PlayerOnGameServerSearchInputDTO extends ITakaroQuery<PlayerOnGameServerSearchInputAllowedFilters> { | ||
@ValidateNested() | ||
@Type(() => PlayerOnGameServerSearchInputAllowedFilters) | ||
declare filters: PlayerOnGameServerSearchInputAllowedFilters; | ||
|
||
@ValidateNested() | ||
@Type(() => PlayerOnGameServerSearchInputAllowedFilters) | ||
declare search: PlayerOnGameServerSearchInputAllowedFilters; | ||
} | ||
|
||
class PlayerOnGameServerSetCurrencyInputDTO { | ||
@IsNumber() | ||
currency!: number; | ||
} | ||
|
||
@OpenAPI({ | ||
security: [{ domainAuth: [] }], | ||
}) | ||
@JsonController() | ||
export class PlayerOnGameServerController { | ||
@UseBefore(AuthService.getAuthMiddleware([PERMISSIONS.READ_PLAYERS])) | ||
@ResponseSchema(PlayerOnGameserverOutputArrayDTOAPI) | ||
@Post('/gameserver/player/search') | ||
async search( | ||
@Req() req: AuthenticatedRequest, | ||
@Res() res: Response, | ||
@Body() query: PlayerOnGameServerSearchInputDTO | ||
) { | ||
const service = new PlayerOnGameServerService(req.domainId); | ||
const result = await service.find({ | ||
...query, | ||
page: res.locals.page, | ||
limit: res.locals.limit, | ||
}); | ||
return apiResponse(result.results, { | ||
meta: { total: result.total }, | ||
req, | ||
res, | ||
}); | ||
} | ||
|
||
@UseBefore(AuthService.getAuthMiddleware([PERMISSIONS.READ_PLAYERS])) | ||
@ResponseSchema(PlayerOnGameserverOutputDTOAPI) | ||
@Get('/gameserver/player/:id') | ||
async getOne(@Req() req: AuthenticatedRequest, @Params() params: ParamId) { | ||
const service = new PlayerOnGameServerService(req.domainId); | ||
return apiResponse(await service.findOne(params.id)); | ||
} | ||
|
||
@UseBefore(AuthService.getAuthMiddleware([PERMISSIONS.MANAGE_PLAYERS])) | ||
@ResponseSchema(PlayerOnGameserverOutputDTOAPI) | ||
@Post('/gameserver/player/:id/currency') | ||
async setCurrency( | ||
@Req() req: AuthenticatedRequest, | ||
@Params() params: ParamId, | ||
@Body() body: PlayerOnGameServerSetCurrencyInputDTO | ||
) { | ||
const service = new PlayerOnGameServerService(req.domainId); | ||
return apiResponse(await service.setCurrency(params.id, body.currency)); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/app-api/src/controllers/__tests__/PlayerOnGameserverController.integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { IntegrationTest, SetupGameServerPlayers, expect } from '@takaro/test'; | ||
|
||
const group = 'PlayerOnGameserverController'; | ||
|
||
const tests = [ | ||
new IntegrationTest<SetupGameServerPlayers.ISetupData>({ | ||
group, | ||
snapshot: false, | ||
name: 'Get list of players', | ||
setup: SetupGameServerPlayers.setup, | ||
test: async function () { | ||
const res = await this.client.playerOnGameserver.playerOnGameServerControllerSearch({ | ||
sortBy: 'gameId', | ||
filters: { | ||
gameServerId: [this.setupData.gameServer1.id], | ||
}, | ||
}); | ||
expect(res.data.data.length).to.be.eq(this.setupData.players.length); | ||
}, | ||
}), | ||
new IntegrationTest<SetupGameServerPlayers.ISetupData>({ | ||
group, | ||
snapshot: false, | ||
name: 'Get one player', | ||
setup: SetupGameServerPlayers.setup, | ||
test: async function () { | ||
const res = await this.client.playerOnGameserver.playerOnGameServerControllerSearch(); | ||
|
||
const player = res.data.data[0]; | ||
|
||
const playerRes = await this.client.playerOnGameserver.playerOnGameServerControllerGetOne(player.id); | ||
expect(playerRes.data.data.id).to.be.eq(player.id); | ||
}, | ||
}), | ||
new IntegrationTest<SetupGameServerPlayers.ISetupData>({ | ||
group, | ||
snapshot: false, | ||
name: 'Can set currency', | ||
setup: SetupGameServerPlayers.setup, | ||
test: async function () { | ||
const res = await this.client.playerOnGameserver.playerOnGameServerControllerSearch(); | ||
|
||
const player = res.data.data[0]; | ||
|
||
await this.client.playerOnGameserver.playerOnGameServerControllerSetCurrency(player.id, { | ||
currency: 100, | ||
}); | ||
const playerRes = await this.client.playerOnGameserver.playerOnGameServerControllerGetOne(player.id); | ||
|
||
expect(playerRes.data.data.currency).to.be.eq(100); | ||
}, | ||
}), | ||
new IntegrationTest<SetupGameServerPlayers.ISetupData>({ | ||
group, | ||
snapshot: true, | ||
name: 'Rejects negative currency', | ||
setup: SetupGameServerPlayers.setup, | ||
expectedStatus: 400, | ||
test: async function () { | ||
const res = await this.client.playerOnGameserver.playerOnGameServerControllerSearch(); | ||
|
||
const player = res.data.data[0]; | ||
|
||
const rejectedRes = await this.client.playerOnGameserver.playerOnGameServerControllerSetCurrency(player.id, { | ||
currency: -100, | ||
}); | ||
|
||
expect(rejectedRes.data.meta.error.message).to.be.eq('Currency must be positive'); | ||
|
||
return rejectedRes; | ||
}, | ||
}), | ||
]; | ||
|
||
describe(group, function () { | ||
tests.forEach((test) => { | ||
test.run(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.