Skip to content

Commit

Permalink
fix(players): player bans & skills queries (#1130)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Aug 3, 2021
1 parent 81b7e76 commit 652ad14
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/players/services/player-bans.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 11 additions & 5 deletions src/players/services/player-bans.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -65,7 +65,7 @@ export class PlayerBansService implements OnModuleInit {
): Promise<PlayerBan[]> {
const plain = await this.playerBanModel
.find({
player: playerId,
player: new Types.ObjectId(playerId),
end: {
$gte: new Date(),
},
Expand All @@ -77,8 +77,12 @@ export class PlayerBansService implements OnModuleInit {

async addPlayerBan(props: PlayerBan): Promise<PlayerBan> {
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})`,
);
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/players/services/player-skill.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
14 changes: 11 additions & 3 deletions src/players/services/player-skill.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -28,11 +29,18 @@ export class PlayerSkillService implements OnModuleInit {
}

async getAll(): Promise<PlayerSkill[]> {
return await this.playerSkillModel.find({});
return plainToClass(
PlayerSkill,
await this.playerSkillModel.find({}).lean().exec(),
);
}

async getPlayerSkill(playerId: string): Promise<PlayerSkillType> {
return (await this.playerSkillModel.findOne({ player: playerId }))?.skill;
return (
await this.playerSkillModel.findOne({
player: new Types.ObjectId(playerId),
})
)?.skill;
}

async setPlayerSkill(
Expand All @@ -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 },
)
Expand Down

0 comments on commit 652ad14

Please sign in to comment.