diff --git a/src/configuration/controllers/configuration.controller.spec.ts b/src/configuration/controllers/configuration.controller.spec.ts index e69f79060..ec7ce4c59 100644 --- a/src/configuration/controllers/configuration.controller.spec.ts +++ b/src/configuration/controllers/configuration.controller.spec.ts @@ -1,5 +1,7 @@ import { Tf2ClassName } from '@/shared/models/tf2-class-name'; import { Test, TestingModule } from '@nestjs/testing'; +import { DefaultPlayerSkill } from '../dto/default-player-skill'; +import { WhitelistId } from '../dto/whitelist-id'; import { ConfigurationService } from '../services/configuration.service'; import { ConfigurationController } from './configuration.controller'; @@ -33,7 +35,7 @@ describe('ConfigurationController', () => { }); it('should return default player skill', async () => { - expect(await controller.getDefaultPlayerSkill()).toEqual(defaultPlayerSkill); + expect(await controller.getDefaultPlayerSkill()).toEqual(new DefaultPlayerSkill(defaultPlayerSkill)); }); }); @@ -44,9 +46,9 @@ describe('ConfigurationController', () => { configurationService.setDefaultPlayerSkill.mockResolvedValue(defaultPlayerSkill); }); - it('should set default player skll', async () => { - const ret = await controller.setDefaultPlayerSkill({ [Tf2ClassName.medic]: 5 }); - expect(ret).toEqual(defaultPlayerSkill); + it('should set default player skill', async () => { + const ret = await controller.setDefaultPlayerSkill(new DefaultPlayerSkill(new Map([[Tf2ClassName.medic, 5]]))); + expect(ret).toEqual(new DefaultPlayerSkill(defaultPlayerSkill)); expect(configurationService.setDefaultPlayerSkill).toHaveBeenCalledWith(defaultPlayerSkill); }); }); @@ -57,7 +59,7 @@ describe('ConfigurationController', () => { }); it('should return the whitelist id', async () => { - expect(await controller.getWhitelistId()).toEqual('etf2l_6v6'); + expect(await controller.getWhitelistId()).toEqual(new WhitelistId('etf2l_6v6')); }); }); @@ -67,8 +69,8 @@ describe('ConfigurationController', () => { }); it('should set the whitelist id', async () => { - const ret = await controller.setWhitelistId('etf2l_6v6'); - expect(ret).toEqual('etf2l_6v6'); + const ret = await controller.setWhitelistId(new WhitelistId('etf2l_6v6')); + expect(ret).toEqual(new WhitelistId('etf2l_6v6')); expect(configurationService.setWhitelistId).toHaveBeenCalledWith('etf2l_6v6'); }); }); diff --git a/src/configuration/controllers/configuration.controller.ts b/src/configuration/controllers/configuration.controller.ts index 9e5119ff0..139b4c9ee 100644 --- a/src/configuration/controllers/configuration.controller.ts +++ b/src/configuration/controllers/configuration.controller.ts @@ -1,6 +1,7 @@ import { Auth } from '@/auth/decorators/auth.decorator'; -import { Tf2ClassName } from '@/shared/models/tf2-class-name'; -import { Body, ClassSerializerInterceptor, Controller, Get, Put, UseInterceptors } from '@nestjs/common'; +import { Body, ClassSerializerInterceptor, Controller, Get, Put, UseInterceptors, ValidationPipe } from '@nestjs/common'; +import { DefaultPlayerSkill } from '../dto/default-player-skill'; +import { WhitelistId } from '../dto/whitelist-id'; import { ConfigurationService } from '../services/configuration.service'; @Controller('configuration') @@ -13,27 +14,29 @@ export class ConfigurationController { @Get('default-player-skill') @UseInterceptors(ClassSerializerInterceptor) async getDefaultPlayerSkill() { - return this.configurationService.getDefaultPlayerSkill(); + return new DefaultPlayerSkill(await this.configurationService.getDefaultPlayerSkill()); } @Put('default-player-skill') @Auth('admin', 'super-user') + @UseInterceptors(ClassSerializerInterceptor) async setDefaultPlayerSkill( - @Body() value: { [className in Tf2ClassName]?: number }, + @Body(new ValidationPipe({ transform: true })) { value }: DefaultPlayerSkill, ) { - const valueAsMap = new Map(Object.entries(value)) as Map; - return this.configurationService.setDefaultPlayerSkill(valueAsMap); + return new DefaultPlayerSkill(await this.configurationService.setDefaultPlayerSkill(value)); } @Get('whitelist-id') + @UseInterceptors(ClassSerializerInterceptor) async getWhitelistId() { - return this.configurationService.getWhitelistId(); + return new WhitelistId(await this.configurationService.getWhitelistId()); } @Put('whitelist-id') @Auth('admin', 'super-user') - async setWhitelistId(@Body() whitelistId: string) { - return this.configurationService.setWhitelistId(whitelistId); + @UseInterceptors(ClassSerializerInterceptor) + async setWhitelistId(@Body(new ValidationPipe()) { value }: WhitelistId) { + return new WhitelistId(await this.configurationService.setWhitelistId(value)); } } diff --git a/src/configuration/dto/default-player-skill.ts b/src/configuration/dto/default-player-skill.ts new file mode 100644 index 000000000..1fb64f2fb --- /dev/null +++ b/src/configuration/dto/default-player-skill.ts @@ -0,0 +1,20 @@ +import { Tf2ClassName } from '@/shared/models/tf2-class-name'; +import { Type } from 'class-transformer'; +import { Equals, IsNumber } from 'class-validator'; +import { ConfigurationEntryKey } from '../models/configuration-entry-key'; + +export class DefaultPlayerSkill { + + constructor(value: Map) { + this.key = ConfigurationEntryKey.defaultPlayerSkill; + this.value = value; + } + + @Equals(ConfigurationEntryKey.defaultPlayerSkill) + key: string; + + @IsNumber({ }, { each: true }) + @Type(() => Number) + value: Map; + +} diff --git a/src/configuration/dto/whitelist-id.ts b/src/configuration/dto/whitelist-id.ts new file mode 100644 index 000000000..311f0f822 --- /dev/null +++ b/src/configuration/dto/whitelist-id.ts @@ -0,0 +1,17 @@ +import { Equals, IsString } from 'class-validator'; +import { ConfigurationEntryKey } from '../models/configuration-entry-key'; + +export class WhitelistId { + + constructor(value: string) { + this.key = ConfigurationEntryKey.whitelistId; + this.value = value; + } + + @Equals(ConfigurationEntryKey.whitelistId) + key: string; + + @IsString() + value: string; + +}