Skip to content

Commit

Permalink
fix(players): fix force create player (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Mar 17, 2021
1 parent 490ba60 commit a0f1588
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
7 changes: 1 addition & 6 deletions src/players/controllers/players.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }));
});
});
Expand Down
10 changes: 3 additions & 7 deletions src/players/controllers/players.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down
11 changes: 11 additions & 0 deletions src/players/dto/force-create-player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IsString, Matches } from 'class-validator';

export class ForceCreatePlayer {

@IsString()
name: string;

@Matches(/^\d{17}$/)
steamId: string;

}
3 changes: 0 additions & 3 deletions src/players/models/player.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/players/services/players.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit a0f1588

Please sign in to comment.