From 493cb0e3e71f8aafa98f6e7bd61f5572cac968d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Tue, 3 Aug 2021 02:36:47 +0200 Subject: [PATCH] fix(players): player bans & skills queries --- src/players/services/player-bans.service.spec.ts | 4 ++-- src/players/services/player-bans.service.ts | 16 +++++++++++----- .../services/player-skill.service.spec.ts | 2 +- src/players/services/player-skill.service.ts | 14 +++++++++++--- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/players/services/player-bans.service.spec.ts b/src/players/services/player-bans.service.spec.ts index 2764459b2..5d6f95b59 100644 --- a/src/players/services/player-bans.service.spec.ts +++ b/src/players/services/player-bans.service.spec.ts @@ -78,7 +78,7 @@ describe('PlayerBansService', () => { end.setHours(end.getHours() + 1); mockPlayerBan = await playerBanModel.create({ - player: player.id, + player: player._id, admin: admin.id, start: new Date(), end, @@ -135,7 +135,7 @@ describe('PlayerBansService', () => { end.setHours(end.getHours() + 1); newBan = { - player: player.id, + player: player.id.toString(), admin: admin.id, start: new Date(), end, diff --git a/src/players/services/player-bans.service.ts b/src/players/services/player-bans.service.ts index eb126b438..7dedd923d 100644 --- a/src/players/services/player-bans.service.ts +++ b/src/players/services/player-bans.service.ts @@ -53,7 +53,7 @@ export class PlayerBansService implements OnModuleInit { return plainToClass( PlayerBan, await this.playerBanModel - .find({ player: playerId }) + .find({ player: new Types.ObjectId(playerId) }) .sort({ start: -1 }) .lean() .exec(), @@ -65,7 +65,7 @@ export class PlayerBansService implements OnModuleInit { ): Promise { const plain = await this.playerBanModel .find({ - player: playerId, + player: new Types.ObjectId(playerId), end: { $gte: new Date(), }, @@ -77,8 +77,12 @@ export class PlayerBansService implements OnModuleInit { async addPlayerBan(props: PlayerBan): Promise { const player = await this.playersService.getById(props.player.toString()); - const { id } = await this.playerBanModel.create(props); - const addedBan = await this.getById(id); + const { id } = await this.playerBanModel.create({ + ...props, + player: player._id, + admin: new Types.ObjectId(props.admin), + }); + const addedBan = plainToClass(PlayerBan, await this.getById(id)); this.logger.verbose( `ban added for player ${player.id} (reason: ${addedBan.reason})`, ); @@ -106,7 +110,9 @@ export class PlayerBansService implements OnModuleInit { return plainToClass( PlayerBan, await this.playerBanModel - .findOneAndUpdate({ _id: banId }, update, { new: true }) + .findOneAndUpdate({ _id: new Types.ObjectId(banId) }, update, { + new: true, + }) .orFail() .lean() .exec(), diff --git a/src/players/services/player-skill.service.spec.ts b/src/players/services/player-skill.service.spec.ts index ada2ae1c9..d014d3341 100644 --- a/src/players/services/player-skill.service.spec.ts +++ b/src/players/services/player-skill.service.spec.ts @@ -155,7 +155,7 @@ describe('PlayerSkillService', () => { describe('#getPlayerSkill()', () => { it('should retrieve player skill', async () => { - const ret = await service.getPlayerSkill(mockPlayer._id); + const ret = await service.getPlayerSkill(mockPlayer.id); expect(ret.size).toEqual(1); expect(ret.get(Tf2ClassName.soldier)).toEqual(4); }); diff --git a/src/players/services/player-skill.service.ts b/src/players/services/player-skill.service.ts index 52892b3a3..cea39b408 100644 --- a/src/players/services/player-skill.service.ts +++ b/src/players/services/player-skill.service.ts @@ -4,6 +4,7 @@ import { FuturePlayerSkillService } from './future-player-skill.service'; import { Events } from '@/events/events'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; +import { plainToClass } from 'class-transformer'; export type PlayerSkillType = PlayerSkill['skill']; @@ -28,11 +29,18 @@ export class PlayerSkillService implements OnModuleInit { } async getAll(): Promise { - return await this.playerSkillModel.find({}); + return plainToClass( + PlayerSkill, + await this.playerSkillModel.find({}).lean().exec(), + ); } async getPlayerSkill(playerId: string): Promise { - return (await this.playerSkillModel.findOne({ player: playerId }))?.skill; + return ( + await this.playerSkillModel.findOne({ + player: new Types.ObjectId(playerId), + }) + )?.skill; } async setPlayerSkill( @@ -48,7 +56,7 @@ export class PlayerSkillService implements OnModuleInit { )?.skill || new Map(); const newSkill = ( await this.playerSkillModel.findOneAndUpdate( - { player: playerId }, + { player: new Types.ObjectId(playerId) }, { skill }, { new: true, upsert: true }, )