Skip to content

Commit

Permalink
Merge pull request #26 from ToniDarodda/implementation-of-plainToInst…
Browse files Browse the repository at this point in the history
…ance/0014

[Backend] feat: update toDto function to implement class-transformer
  • Loading branch information
ToniDarodda authored Sep 10, 2024
2 parents 006dfbd + 1986c24 commit 1127bab
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .env.development
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
API_PORT=3000
API_PORT=3005

ORIGIN=*
FRONTEND_PATH=http://localhost:3001
FRONTEND_PATH=http://localhost:3000

TYPE=postgres
HOST=localhost
Expand Down
4 changes: 2 additions & 2 deletions .env.production
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
API_PORT=3000
API_PORT=3005

ORIGIN=*
FRONTEND_PATH=http://localhost:3001
FRONTEND_PATH=http://localhost:3000

TYPE=postgres
HOST=localhost
Expand Down
2 changes: 2 additions & 0 deletions src/entities/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Country } from 'types/country';
import { ApiProperty } from '@nestjs/swagger';
import { Role } from 'types/role';
import { CommonAccount } from './commonAccount';
import { Exclude } from 'class-transformer';

@Entity('account')
export class Account extends CommonAccount {
Expand All @@ -25,6 +26,7 @@ export class Account extends CommonAccount {

@ApiProperty({ example: 'po9@cQesP!!D(' })
@Column('varchar')
@Exclude({ toPlainOnly: true })
password: string;

@ApiProperty({ example: Country.FRANCE })
Expand Down
9 changes: 2 additions & 7 deletions src/modules/account/controller/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Get,
HttpCode,
HttpStatus,
NotFoundException,
Param,
Patch,
Post,
Expand Down Expand Up @@ -182,11 +181,7 @@ export class AccountController {
})
@Roles(Role.USER)
@UseGuards(JwtAuthGuard, RolesGuard)
async delete(@AuthToken() { sub }: DecodedUserToken) {
try {
return await this.accountService.delete(sub);
} catch (err) {
throw new NotFoundException('User not found');
}
delete(@AuthToken() { sub }: DecodedUserToken) {
return this.accountService.delete(sub);
}
}
7 changes: 7 additions & 0 deletions src/modules/account/dto/request/signUp.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { Expose } from 'class-transformer';
import {
IsNotEmpty,
IsString,
Expand All @@ -12,29 +13,35 @@ export class SignUpAccount {
@ApiProperty({ example: 'Toni' })
@IsNotEmpty()
@IsString()
@Expose()
firstName: string;

@ApiProperty({ example: 'Da Rodda' })
@IsNotEmpty()
@IsString()
@Expose()
lastName: string;

@ApiProperty({ example: 'toni.da.rodda.pro@gmail.com' })
@IsEmail()
@Expose()
email: string;

@ApiProperty({ example: '+330678400302' })
@IsNotEmpty()
@IsString()
@Expose()
phoneNumber: string;

@ApiProperty({ example: 'po9@cQesP!!D(' })
@IsNotEmpty()
@IsString()
@Expose()
password: string;

@ApiProperty({ example: [Country.CANADA] })
@IsArray()
@IsEnum(Country, { each: true })
@Expose()
country: Country;
}
22 changes: 12 additions & 10 deletions src/modules/account/service/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { SignUpAccount } from '../dto/request/signUp.dto';
import { PatchAccountDto } from '../dto/request/patch.dto';
import { MailService } from 'modules/mail/service/mail/mail.service';
import { AccountDto } from '../dto/response/account.dto';
import { plainToInstance } from 'class-transformer';

@Injectable()
export class AccountService {
Expand All @@ -28,13 +29,6 @@ export class AccountService {
private readonly emailService: MailService,
) {}

private static toDto(account: Account): AccountDto {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password, salt, ...data } = account;

return { ...data };
}

async isMailAccountAlreadyUsed(email: string): Promise<boolean> {
try {
const account = await this.accountRepository.findOneBy({ email });
Expand Down Expand Up @@ -133,12 +127,20 @@ export class AccountService {
};
}

getByMail(email: string): Promise<AccountDto> {
return this.accountRepository.findOneBy({ email });
async getByMail(email: string): Promise<AccountDto> {
const account = await this.accountRepository.findOneBy({ email });

return plainToInstance(AccountDto, account, {
excludeExtraneousValues: true,
});
}

async get(id: Account['id']): Promise<AccountDto> {
return AccountService.toDto(await this.accountRepository.findOneBy({ id }));
const account = await this.accountRepository.findOneBy({ id });

return plainToInstance(AccountDto, account, {
excludeExtraneousValues: true,
});
}

update(id: Account['id'], data: PatchAccountDto): Promise<UpdateResult> {
Expand Down

0 comments on commit 1127bab

Please sign in to comment.