From 40e21a0c05b87eeffc2a0defcf8a2d146ce85f3c Mon Sep 17 00:00:00 2001 From: rajdip-b Date: Mon, 24 Jun 2024 20:24:20 +0530 Subject: [PATCH] Fixed breaking dto --- src/auth/auth.e2e.spec.ts | 10 ++++++---- src/auth/service/auth.service.ts | 27 +++++++++++---------------- src/user/dto/user-response.dto.ts | 20 +++++++++++++++++++- src/user/service/user.service.ts | 2 +- src/user/user.e2e.spec.ts | 11 +++++++++-- 5 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/auth/auth.e2e.spec.ts b/src/auth/auth.e2e.spec.ts index 435d451..eb9fade 100644 --- a/src/auth/auth.e2e.spec.ts +++ b/src/auth/auth.e2e.spec.ts @@ -41,20 +41,22 @@ describe('Auth Controller Tests', () => { it('should be able to sign up using email', async () => { const response = await app.inject({ method: 'POST', - url: '/auth/send-verification-email', + url: '/auth/email', payload: { email: 'jane@example.com', }, }); expect(response.statusCode).toEqual(201); - expect(response.json().email).toEqual('jane@example.com'); + expect(response.json()).toEqual({ + status: 'success', + }); }); it('should send verification code to email on sign up', async () => { await app.inject({ method: 'POST', - url: '/auth/send-verification-email', + url: '/auth/email', payload: { email: 'jane@example.com', }, @@ -101,7 +103,7 @@ describe('Auth Controller Tests', () => { // Sign up await app.inject({ method: 'POST', - url: '/auth/send-verification-email', + url: '/auth/email', payload: { email: 'jane@example.com', }, diff --git a/src/auth/service/auth.service.ts b/src/auth/service/auth.service.ts index f4cd8b9..0149465 100644 --- a/src/auth/service/auth.service.ts +++ b/src/auth/service/auth.service.ts @@ -220,22 +220,17 @@ export class AuthService { throw new NotFoundException('User not found'); } - const updatedUser = await this.prisma.user.update({ - where: { - email, - }, - data: { - isEmailVerified: true, - }, - select: { - id: true, - email: true, - name: true, - profilePictureUrl: true, - authType: true, - isEmailVerified: true, - }, - }); + const updatedUser = plainToClass( + UserResponseDto, + await this.prisma.user.update({ + where: { + email, + }, + data: { + isEmailVerified: true, + }, + }), + ); await this.prisma.verificationCode.delete({ where: { diff --git a/src/user/dto/user-response.dto.ts b/src/user/dto/user-response.dto.ts index fe71586..3b98c59 100644 --- a/src/user/dto/user-response.dto.ts +++ b/src/user/dto/user-response.dto.ts @@ -1,14 +1,32 @@ import { AuthType } from '@prisma/client'; -import { Exclude } from 'class-transformer'; +import { Exclude, Expose } from 'class-transformer'; @Exclude() export default class UserResponseDto { + @Expose() + id: string; + + @Expose() email: string; + + @Expose() name: string; + + @Expose() location: string; + + @Expose() onboarded: boolean; + + @Expose() profilePictureUrl: string; + + @Expose() authType: AuthType; + + @Expose() isEmailVerified: boolean; + + @Expose() headline: string; } diff --git a/src/user/service/user.service.ts b/src/user/service/user.service.ts index 9fdeb29..84f858d 100644 --- a/src/user/service/user.service.ts +++ b/src/user/service/user.service.ts @@ -30,7 +30,7 @@ export class UserService { ) {} async getSelf(user: User) { - return user; + return plainToClass(UserResponseDto, user); } async updateSelf(user: User, dto: UpdateUserDto) { diff --git a/src/user/user.e2e.spec.ts b/src/user/user.e2e.spec.ts index aa8ae29..b294fc4 100644 --- a/src/user/user.e2e.spec.ts +++ b/src/user/user.e2e.spec.ts @@ -54,7 +54,7 @@ describe('User Controller Tests', () => { expect(prisma).toBeDefined(); }); - it('should not be able to update itself if onboarding is not finished', async () => { + it('should be able to update itself if onboarding is not finished', async () => { await prisma.user.update({ where: { email: 'johndoe@example.com', @@ -75,7 +75,14 @@ describe('User Controller Tests', () => { }, }); - expect(response.statusCode).toBe(401); + const updatedUser = await prisma.user.findUnique({ + where: { + email: 'johndoe@example.com', + }, + }); + expect(updatedUser.name).toBe('Jane Doe'); + + expect(response.statusCode).toBe(200); }); it('should be able to get the current user', async () => {