diff --git a/src/players/controllers/players.controller.spec.ts b/src/players/controllers/players.controller.spec.ts index 77457afaa..feff1a328 100644 --- a/src/players/controllers/players.controller.spec.ts +++ b/src/players/controllers/players.controller.spec.ts @@ -152,17 +152,12 @@ describe('Players Controller', () => { expect(spy).toHaveBeenCalledWith('FAKE_ID'); expect(ret).toEqual(playersService.player as any); }); - - it('should return 404', async () => { - jest.spyOn(playersService, 'getById').mockResolvedValue(null); - await expect(controller.getPlayer('FAKE_ID')).rejects.toThrow(NotFoundException); - }); }); describe('#forceCreatePlayer()', () => { it('should call the service', async () => { const spy = jest.spyOn(playersService, 'forceCreatePlayer'); - await controller.forceCreatePlayer({ id: 'FAKE_PLAYER_ID', name: 'FAKE_PLAYER_NAME', steamId: 'FAKE_PLAYER_STEAM_ID' } as Player); + await controller.forceCreatePlayer({ name: 'FAKE_PLAYER_NAME', steamId: 'FAKE_PLAYER_STEAM_ID' }); expect(spy).toHaveBeenCalledWith(expect.objectContaining({ name: 'FAKE_PLAYER_NAME', steamId: 'FAKE_PLAYER_STEAM_ID' })); }); }); diff --git a/src/players/controllers/players.controller.ts b/src/players/controllers/players.controller.ts index 90d2389cb..b9e2659c8 100644 --- a/src/players/controllers/players.controller.ts +++ b/src/players/controllers/players.controller.ts @@ -12,6 +12,7 @@ import { User } from '@/auth/decorators/user.decorator'; import { Tf2ClassName } from '@/shared/models/tf2-class-name'; import { DocumentNotFoundFilter } from '@/shared/filters/document-not-found.filter'; import { PlayerStats } from '../dto/player-stats'; +import { ForceCreatePlayer } from '../dto/force-create-player'; @Controller('players') @UseInterceptors(CacheInterceptor) @@ -34,19 +35,14 @@ export class PlayersController { @UseInterceptors(ClassSerializerInterceptor) @UseFilters(DocumentNotFoundFilter) async getPlayer(@Param('id', ObjectIdValidationPipe) playerId: string) { - const player = await this.playersService.getById(playerId); - if (player) { - return player; - } else { - throw new NotFoundException(); - } + return this.playersService.getById(playerId); } @Post() @Auth('admin', 'super-user') @UsePipes(ValidationPipe) @UseInterceptors(ClassSerializerInterceptor) - async forceCreatePlayer(@Body() player: Player) { + async forceCreatePlayer(@Body() player: ForceCreatePlayer) { return await this.playersService.forceCreatePlayer(player); } diff --git a/src/players/dto/force-create-player.ts b/src/players/dto/force-create-player.ts new file mode 100644 index 000000000..d0b53cbd0 --- /dev/null +++ b/src/players/dto/force-create-player.ts @@ -0,0 +1,11 @@ +import { IsString, Matches } from 'class-validator'; + +export class ForceCreatePlayer { + + @IsString() + name: string; + + @Matches(/^\d{17}$/) + steamId: string; + +} diff --git a/src/players/models/player.ts b/src/players/models/player.ts index b88ed4bd7..87e739a62 100644 --- a/src/players/models/player.ts +++ b/src/players/models/player.ts @@ -1,7 +1,6 @@ import { MongooseDocument } from '@/utils/mongoose-document'; import { prop, index } from '@typegoose/typegoose'; import { Exclude, Expose, Transform, Type } from 'class-transformer'; -import { IsString, Matches } from 'class-validator'; import { PlayerAvatar } from './player-avatar'; import { PlayerRole } from './player-role'; import { TwitchTvUser } from './twitch-tv-user'; @@ -13,11 +12,9 @@ export class Player extends MongooseDocument { @Transform(({ value, obj }) => value ?? obj._id.toString()) id?: string; - @IsString() @prop({ required: true, unique: true, trim: true }) name!: string; - @Matches(/^\d{17}$/) @prop({ unique: true }) steamId?: string; // SteamID64 only diff --git a/src/players/services/players.service.ts b/src/players/services/players.service.ts index 8a8fc12a1..62cd92d9d 100644 --- a/src/players/services/players.service.ts +++ b/src/players/services/players.service.ts @@ -125,10 +125,10 @@ export class PlayersService implements OnModuleInit { // eslint-disable-next-line no-empty } catch (error) { } - const id = (await this.playerModel.create({ + const { id } = await this.playerModel.create({ etf2lProfileId: etf2lProfile?.id, ...playerData, - }))._id; + }); const player = await this.getById(id); this.logger.verbose(`created new player (name: ${player.name})`); this.events.playerRegisters.next({ player });