Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle setting initial skill of a player #840

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/players/services/player-skill.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DiscordService } from '@/discord/services/discord.service';
import { Environment } from '@/environment/environment';
import { Events } from '@/events/events';
import { Tf2ClassName } from '@/shared/models/tf2-class-name';
import { skillChanged } from '@/discord/notifications';

jest.mock('./players.service');
jest.mock('./future-player-skill.service');
Expand Down Expand Up @@ -193,6 +194,32 @@ describe('PlayerSkillService', () => {
});
});

describe('when initializing player skill', () => {
let newPlayer: DocumentType<Player>;

beforeEach(async () => {
// @ts-expect-error
newPlayer = await playersService._createOne();
});

it('should notify admins on discord', async () => {
const spy = jest.spyOn(discordService.getAdminsChannel(), 'send');
await service.setPlayerSkill(newPlayer.id, new Map([[Tf2ClassName.soldier, 2]]));
expect(spy).toHaveBeenCalledWith({
embed: {
...skillChanged({
playerName: 'fake_player_2',
oldSkill: new Map(),
newSkill: new Map(([[Tf2ClassName.soldier, 2]])),
playerProfileUrl: `FAKE_CLIENT_URL/player/${newPlayer.id}`,
adminResponsible: undefined,
}),
timestamp: expect.any(Date),
},
});
});
});

describe('when there is no such player', () => {
it('should fail', async () => {
await expect(service.setPlayerSkill(new ObjectId().toString(), new Map([[Tf2ClassName.scout, 1]]))).rejects.toThrowError('no such player');
Expand Down
2 changes: 1 addition & 1 deletion src/players/services/player-skill.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class PlayerSkillService implements OnModuleInit {
throw new Error('no such player');
}

const oldSkill = await this.playerSkillModel.findOne({ player: playerId });
const oldSkill = await this.playerSkillModel.findOne({ player: playerId }) || { skill: new Map() };
const ret = await this.playerSkillModel.findOneAndUpdate({ player: playerId }, { skill }, { new: true, upsert: true });

for (const [key, value] of ret.skill) {
Expand Down