Skip to content

Commit

Permalink
fix(players): cleanup PlayersController (#2757)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Jan 9, 2024
1 parent 89c5393 commit 7ccda21
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 46 deletions.
34 changes: 17 additions & 17 deletions src/players/controllers/players.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { PlayerBansService } from '../services/player-bans.service';
import { User } from '@/auth/decorators/user.decorator';
import { Tf2ClassName } from '@/shared/models/tf2-class-name';
import { PlayerStatsDto } from '../dto/player-stats.dto';
import { ForceCreatePlayer } from '../dto/force-create-player';
import { PlayerRole } from '../models/player-role';
import { LinkedProfilesService } from '../services/linked-profiles.service';
import { LinkedProfilesDto } from '../dto/linked-profiles.dto';
Expand All @@ -47,10 +46,12 @@ import { PlayerSkillRecordMalformedError } from '../errors/player-skill-record-m
import { ValidateSkillPipe } from '../pipes/validate-skill.pipe';
import { isUndefined } from 'lodash';
import { PlayerBanId } from '../types/player-ban-id';
import { AddPlayerBanDto } from '../dto/add-player-ban.dto';
import { Types } from 'mongoose';
import { PlayerId } from '../types/player-id';
import { CacheInterceptor } from '@nestjs/cache-manager';
import { ZodPipe } from '@/shared/pipes/zod.pipe';
import { forceCreatePlayerSchema } from '../dto/force-create-player.schema';
import { z } from 'zod';
import { updatePlayerSchema } from '../dto/update-player.schema';
import { addPlayerBanSchema } from '../dto/add-player-ban.schema';

@Controller('players')
export class PlayersController {
Expand Down Expand Up @@ -78,16 +79,18 @@ export class PlayersController {
@Auth(PlayerRole.admin)
@UsePipes(ValidationPipe)
async forceCreatePlayer(
@Body() player: ForceCreatePlayer,
@Body(new ZodPipe(forceCreatePlayerSchema))
playerData: z.infer<typeof forceCreatePlayerSchema>,
): Promise<Serializable<PlayerDto>> {
return await this.playersService.forceCreatePlayer(player);
return await this.playersService.forceCreatePlayer(playerData);
}

@Patch(':id')
@Auth(PlayerRole.admin)
async updatePlayer(
@Param('id', PlayerByIdPipe) player: Player,
@Body() update: Partial<Player>,
@Body(new ZodPipe(updatePlayerSchema))
update: z.infer<typeof updatePlayerSchema>,
@User() admin: Player,
): Promise<Serializable<PlayerDto>> {
return await this.playersService.updatePlayer(
Expand All @@ -98,7 +101,6 @@ export class PlayersController {
}

@UseInterceptors(CacheInterceptor)
// @CacheTTL(30 * 60 * 1000) // 30 minutes
@Get(':id/stats')
async getPlayerStats(
@Param('id', PlayerByIdPipe) player: Player,
Expand All @@ -111,6 +113,7 @@ export class PlayersController {
};
}

// TODO v12: move to a separate controller
@Get('/all/skill')
@Auth(PlayerRole.admin)
async getAllPlayersWithSkills(): Promise<Serializable<PlayerSkillDto>[]> {
Expand Down Expand Up @@ -175,23 +178,20 @@ export class PlayersController {
@Auth(PlayerRole.admin)
@UsePipes(ValidationPipe)
async addPlayerBan(
@Body() playerBan: AddPlayerBanDto,
@Body(new ZodPipe(addPlayerBanSchema))
playerBan: z.infer<typeof addPlayerBanSchema>,
@User() user: Player,
): Promise<Serializable<PlayerBanDto>> {
if (playerBan.admin !== user.id) {
if (!playerBan.admin.equals(user._id)) {
throw new BadRequestException(
"the admin field must be the same as authorized user's id",
);
}
return await this.playerBansService.addPlayerBan({
player: new Types.ObjectId(playerBan.player) as PlayerId,
admin: new Types.ObjectId(playerBan.admin) as PlayerId,
start: new Date(playerBan.start),
end: new Date(playerBan.end),
reason: playerBan.reason,
});

return await this.playerBansService.addPlayerBan(playerBan);
}

// TODO v12: make it PUT /players/:playerId/bans/:banId/revoke
@Post(':playerId/bans/:banId')
@Auth(PlayerRole.admin)
@HttpCode(200)
Expand Down
19 changes: 0 additions & 19 deletions src/players/dto/add-player-ban.dto.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/players/dto/add-player-ban.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Types } from 'mongoose';
import { z } from 'zod';
import { PlayerId } from '../types/player-id';

export const addPlayerBanSchema = z.object({
player: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
message: 'player has to be a valid player id',
})
.transform((val) => new Types.ObjectId(val) as PlayerId),
admin: z
.string()
.refine((val) => Types.ObjectId.isValid(val), {
message: 'admin has to be a valid player id',
})
.transform((val) => new Types.ObjectId(val) as PlayerId),
start: z
.string()
.datetime()
.transform((val) => new Date(val)),
end: z
.string()
.datetime()
.transform((val) => new Date(val)),
reason: z.string().optional(),
});
6 changes: 6 additions & 0 deletions src/players/dto/force-create-player.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from 'zod';

export const forceCreatePlayerSchema = z.object({
name: z.string(),
steamId: z.string().regex(/^\d{17}$/),
});
9 changes: 0 additions & 9 deletions src/players/dto/force-create-player.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/players/dto/player.dto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export interface PlayerDto {
id: string;
name: string;
steamId?: string;
steamId: string;
joinedAt: string;
avatar: {
small?: string;
medium?: string;
large?: string;
};
roles: ('super user' | 'admin' | 'bot')[];
// TODO v12: remove
etf2lProfileId?: number;
_links: {
href: string;
Expand Down
14 changes: 14 additions & 0 deletions src/players/dto/update-player.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { z } from 'zod';
import { PlayerRole } from '../models/player-role';

export const updatePlayerSchema = z.object({
name: z.string().optional(),
avatar: z
.object({
small: z.string(),
medium: z.string(),
large: z.string(),
})
.optional(),
roles: z.array(z.nativeEnum(PlayerRole)).optional(),
});

0 comments on commit 7ccda21

Please sign in to comment.