Skip to content

Commit

Permalink
feat(account): Add routes parameters class validator
Browse files Browse the repository at this point in the history
  • Loading branch information
rateau-violent committed Oct 5, 2023
1 parent dcc9716 commit ab69131
Show file tree
Hide file tree
Showing 13 changed files with 792 additions and 389 deletions.
43 changes: 11 additions & 32 deletions src/account/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { AuthService } from "@auth/auth.service";
import { EmailConfirmationService } from "@email/email-confirmation.service";
import { ForgottenPasswordService } from "@email/forgotten-password.service";
import { PasswordUpdateService } from "@email/password-update.service";
import { PasswordParameterDTO, EmailParameterDTO, UsernameParameterDTO } from "@account/account.dto";

dotenv.config();

Expand Down Expand Up @@ -73,8 +74,8 @@ export class AccountController {
*/
@Post("send-password-reset-link")
@HttpCode(HttpStatus.OK)
async sendPasswordResetLink(@Body("email") email: string): Promise<void> {
if (!this.isFieldValid(email) || await this.profileService.findOneByEmail(email) === null) {
async sendPasswordResetLink(@Body() { email }: EmailParameterDTO): Promise<void> {
if (await this.profileService.findOneByEmail(email) === null) {
throw new BadRequestException("Invalid email address");
}
await this.forgottenPasswordService.sendPasswordResetLink(email);
Expand All @@ -90,12 +91,10 @@ export class AccountController {
*/
@Put("reset-password")
@HttpCode(HttpStatus.OK)
async resetPassword(@Query("token") token: string, @Body("password") password: string): Promise<void> {
async resetPassword(@Query("token") token: string,
@Body() { password }: PasswordParameterDTO): Promise<void> {
const username: string = await this.forgottenPasswordService.decodeResetToken(token);

if (!this.isFieldValid(password)) {
throw new BadRequestException("Invalid password field");
}
await this.forgottenPasswordService.resetPassword(username, password);
}

Expand Down Expand Up @@ -126,13 +125,10 @@ export class AccountController {
@UseGuards(JwtAuthGuard)
@Put("password")
@HttpCode(HttpStatus.OK)
async updatePassword(@Request() req, @Body("password") password: string): Promise<void> {
async updatePassword(@Request() req, @Body() { password }: PasswordParameterDTO): Promise<void> {
const username = req.user.username;
const profile: Profile = await this.profileService.findOneByUsername(username);

if (!this.isFieldValid(password)) {
throw new BadRequestException();
}
await this.profileService.updatePassword(username, password);
await this.passwordUpdateService.sendPasswordUpdateEmail(profile.email);
}
Expand All @@ -147,13 +143,10 @@ export class AccountController {
@UseGuards(JwtAuthGuard)
@Put("email")
@HttpCode(HttpStatus.OK)
async updateEmail(@Request() req, @Body("email") email: string): Promise<void> {
async updateEmail(@Request() req, @Body() { email }: EmailParameterDTO): Promise<void> {
const username = req.user.username;
const _profile: Profile = await this.profileService.findOneByUsername(username);

if (!this.isFieldValid(email)) {
throw new BadRequestException();
}
await this.profileService.updateEmail(username, email);
await this.emailConfirmationService.sendVerificationLink(email);
}
Expand All @@ -168,15 +161,12 @@ export class AccountController {
@UseGuards(JwtAuthGuard)
@Put("username")
@HttpCode(HttpStatus.OK)
async updateUsername(@Request() req, @Body("username") newUsername: string): Promise<{username: string}> {
const username = req.user.username;
async updateUsername(@Request() req, @Body() { username }: UsernameParameterDTO): Promise<UsernameParameterDTO> {
const u = req.user.username;

if (!this.isFieldValid(newUsername)) {
throw new BadRequestException();
}
await this.profileService.updateUsername(username, newUsername);
await this.profileService.updateUsername(u, username);
return {
username: newUsername
username
};
}

Expand All @@ -195,15 +185,4 @@ export class AccountController {
email: profile.email
};
}

/**
* @brief Checks if a string parameter is valid
* Checks if it is not undefined, null or empty
* @param str The parameter to check
* @return True if the parameter is valid, false otherwise
* @private
*/
private isFieldValid(str: string): boolean {
return (str !== undefined && str !== null && str !== "");
}
}
23 changes: 23 additions & 0 deletions src/account/account.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IsDefined, IsEmail, IsNotEmpty, IsString } from "class-validator";

export class EmailParameterDTO {
@IsDefined()
@IsString()
@IsNotEmpty()
@IsEmail()
public email: string;
}

export class PasswordParameterDTO {
@IsDefined()
@IsString()
@IsNotEmpty()
public password: string;
}

export class UsernameParameterDTO {
@IsDefined()
@IsString()
@IsNotEmpty()
public username: string;
}
1 change: 0 additions & 1 deletion src/prophecy/prophecy.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { ProphecyArmyWithIdDTO } from "@prophecy/army/prophecy-army.dto";
import { ArmyList } from "@army-list/army-list.entity";
import { ProphecyUnitMathsResponseDTO } from "@prophecy/maths/prophecy-unit-maths.dto";
import { ProphecyArmyRequestDTO, ProphecyUnitRequestDTO } from "@prophecy/prophecy.dto";
import { IsDefined } from "class-validator";

@Controller("prophecies")
export class ProphecyController {
Expand Down
1 change: 0 additions & 1 deletion src/prophecy/prophecy.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
import { ProphecyUnitAttackingPosition } from "@prophecy/unit/prophecy-unit.entity";
import { IsDefined, IsEnum, IsNotEmpty, IsString, ValidateNested } from "class-validator";
import { Type } from "class-transformer";
import { Body } from "@nestjs/common";

export class ProphecyUnitRequestDTO {
@IsDefined()
Expand Down
Loading

0 comments on commit ab69131

Please sign in to comment.