From e11008a1e1b6212322d28e1bafc6e233063a7929 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 30 Jan 2024 11:57:52 -0300 Subject: [PATCH 01/29] fix: adjust Credential Id to String -uuid --- .../migration.sql | 30 +++++++++++++++++++ prisma/schema.prisma | 6 ++-- 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 prisma/migrations/20240128005220_cahnge_credential_id_from_int_to_string_uuid/migration.sql diff --git a/prisma/migrations/20240128005220_cahnge_credential_id_from_int_to_string_uuid/migration.sql b/prisma/migrations/20240128005220_cahnge_credential_id_from_int_to_string_uuid/migration.sql new file mode 100644 index 0000000..bc3c947 --- /dev/null +++ b/prisma/migrations/20240128005220_cahnge_credential_id_from_int_to_string_uuid/migration.sql @@ -0,0 +1,30 @@ +/* + Warnings: + + - The primary key for the `credentials` table will be changed. If it partially fails, the table could be left without primary key constraint. + +*/ +-- DropForeignKey +ALTER TABLE "parent_profiles" DROP CONSTRAINT "parent_profiles_credential_id_fkey"; + +-- DropForeignKey +ALTER TABLE "reset_password_info" DROP CONSTRAINT "reset_password_info_credential_id_fkey"; + +-- AlterTable +ALTER TABLE "credentials" DROP CONSTRAINT "credentials_pkey", +ALTER COLUMN "id" DROP DEFAULT, +ALTER COLUMN "id" SET DATA TYPE TEXT, +ADD CONSTRAINT "credentials_pkey" PRIMARY KEY ("id"); +DROP SEQUENCE "credentials_id_seq"; + +-- AlterTable +ALTER TABLE "parent_profiles" ALTER COLUMN "credential_id" SET DATA TYPE TEXT; + +-- AlterTable +ALTER TABLE "reset_password_info" ALTER COLUMN "credential_id" SET DATA TYPE TEXT; + +-- AddForeignKey +ALTER TABLE "reset_password_info" ADD CONSTRAINT "reset_password_info_credential_id_fkey" FOREIGN KEY ("credential_id") REFERENCES "credentials"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "parent_profiles" ADD CONSTRAINT "parent_profiles_credential_id_fkey" FOREIGN KEY ("credential_id") REFERENCES "credentials"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 166f5d8..df7ce52 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -11,7 +11,7 @@ datasource db { } model Credential { - id Int @id @default(autoincrement()) + id String @id @default(uuid()) email String @unique password String policiesAcceptedAt DateTime @map("policies_accepted_at") @@ -29,7 +29,7 @@ model ResetPasswordInfo { id Int @id @default(autoincrement()) recoveryToken String @unique @map("recovery_token") expiresIn DateTime @updatedAt @map("expires_in") - credentialId Int @unique @map("credential_id") + credentialId String @unique @map("credential_id") credential Credential @relation(fields: [credentialId], references: [id], onDelete: Cascade) @@map("reset_password_info") @@ -39,7 +39,7 @@ model ParentProfile { id String @id @default(uuid()) fullname String childrens ChildrenProfile[] - credentialId Int @unique @map("credential_id") + credentialId String @unique @map("credential_id") credential Credential @relation(fields: [credentialId], references: [id], onDelete: Cascade) updatedAt DateTime @updatedAt @map("updated_at") createdAt DateTime @default(now()) @map("created_at") From 85b9ba6979414658fe781afb02b645d4faa66952 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 30 Jan 2024 11:59:09 -0300 Subject: [PATCH 02/29] LOR-214 update getCredentialByEmail to also receive an ID --- src/modules/account/account.repository.ts | 24 ++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/modules/account/account.repository.ts b/src/modules/account/account.repository.ts index 863ac58..d9a00f4 100644 --- a/src/modules/account/account.repository.ts +++ b/src/modules/account/account.repository.ts @@ -46,16 +46,25 @@ export class AccountRepository { return; } - async getCredentialIdByEmail( - hashedEmail: string, - ): Promise { + async getCredentialIdByEmailOrId(idOrEmail: { + id?: string; + hashedEmail?: string; + }): Promise { + let whereCondition = {}; + if (idOrEmail.hashedEmail) { + whereCondition = { email: idOrEmail.hashedEmail }; + } else if (idOrEmail.id) { + whereCondition = { id: idOrEmail.id[0] }; + } else { + throw new Error('Invalid input'); + } + const response = await this.prisma.credential - .findUnique({ - where: { - email: hashedEmail, - }, + .findFirst({ + where: whereCondition, select: { id: true, + email: true, password: true, parentProfile: { select: { @@ -74,6 +83,7 @@ export class AccountRepository { }, }) .then((response) => { + console.log('response', response); return response; }) .catch((error) => handleErrors(error)); From 8ba2c20dad1584209d1d1455f52e44d5fba88f61 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 30 Jan 2024 11:59:46 -0300 Subject: [PATCH 03/29] LOR-214 - add endpoint to get users --- src/modules/account/account.controller.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/modules/account/account.controller.ts b/src/modules/account/account.controller.ts index fab20d8..3894000 100644 --- a/src/modules/account/account.controller.ts +++ b/src/modules/account/account.controller.ts @@ -1,4 +1,12 @@ -import { Controller, Post, Body, HttpCode, Put } from '@nestjs/common'; +import { + Controller, + Post, + Body, + HttpCode, + Put, + Param, + Get, +} from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { ApiResponse, ApiTags } from '@nestjs/swagger'; import { MailService } from '../mail/mail.service'; @@ -105,4 +113,15 @@ export class AccountController { message: 'Senha redefinida com sucesso', }; } + @Get('/user/:id') + @ApiTags('Authentication') + @HttpCode(200) + @ApiResponse(responses.ok) + @ApiResponse(responses.badRequest) + @ApiResponse(responses.unauthorized) + @ApiResponse(responses.internalError) + async getCredential(@Param() id: string) { + const response = await this.accountService.getCredential(id); + return response; + } } From f5747d5614d703a7ab12111ceaa6b7498e69f7ae Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 30 Jan 2024 12:00:18 -0300 Subject: [PATCH 04/29] LOR-214 - fix: update function name getCredentialIdByEmailOrId --- src/modules/account/account.service.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/modules/account/account.service.ts b/src/modules/account/account.service.ts index c9696cf..fdf4202 100644 --- a/src/modules/account/account.service.ts +++ b/src/modules/account/account.service.ts @@ -162,9 +162,9 @@ export class AccountService { // ! verificar responsabilidade única const hashedEmail = await this.hashData(email); - const account = await this.accountRepository.getCredentialIdByEmail( + const account = await this.accountRepository.getCredentialIdByEmailOrId({ hashedEmail, - ); + }); if (!account) { throw new EmailNotFoundException(); @@ -223,9 +223,9 @@ export class AccountService { async login(email: string, password: string) { const hashedEmail = await this.hashData(email); - const credential = await this.accountRepository.getCredentialIdByEmail( + const credential = await this.accountRepository.getCredentialIdByEmailOrId({ hashedEmail, - ); + }); if (!credential) { throw new EmailNotFoundException(); @@ -260,4 +260,18 @@ export class AccountService { user, }; } + async getCredential(id: string) { + const credential = await this.accountRepository.getCredentialIdByEmailOrId({ + id, + }); + console.log('credential', credential); + + if (!credential) { + throw new EmailNotFoundException(); + } + + return { + credential, + }; + } } From 680a47090f5be8723074b69913b128a4daae2849 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 30 Jan 2024 15:12:51 -0300 Subject: [PATCH 05/29] LOR-214 adjust test to use id as string --- test/unit/account.service.stubs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/account.service.stubs.ts b/test/unit/account.service.stubs.ts index ddb8c54..87a059c 100644 --- a/test/unit/account.service.stubs.ts +++ b/test/unit/account.service.stubs.ts @@ -21,7 +21,7 @@ export const resetPasswordInput: ResetPasswordDto = { }; export const getCredentialOutput: GetCredentialIdByEmailOutput = { - id: faker.helpers.rangeToNumber({ min: 1, max: 3000 }), + id: faker.string.uuid(), password: faker.internet.password(), parentProfile: { id: faker.string.uuid(), From d622fbcb63cb4dd1df6d23c74c08bec5af3891be Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 30 Jan 2024 19:01:39 -0300 Subject: [PATCH 06/29] LOR-124: fix: remove console.log --- src/modules/account/account.repository.ts | 1 - src/modules/account/account.service.ts | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/modules/account/account.repository.ts b/src/modules/account/account.repository.ts index d9a00f4..4ebb67c 100644 --- a/src/modules/account/account.repository.ts +++ b/src/modules/account/account.repository.ts @@ -83,7 +83,6 @@ export class AccountRepository { }, }) .then((response) => { - console.log('response', response); return response; }) .catch((error) => handleErrors(error)); diff --git a/src/modules/account/account.service.ts b/src/modules/account/account.service.ts index fdf4202..bd882bc 100644 --- a/src/modules/account/account.service.ts +++ b/src/modules/account/account.service.ts @@ -162,6 +162,7 @@ export class AccountService { // ! verificar responsabilidade única const hashedEmail = await this.hashData(email); + const account = await this.accountRepository.getCredentialIdByEmailOrId({ hashedEmail, }); @@ -189,7 +190,6 @@ export class AccountService { }); delete account.password; - return { url, fullname: account.parentProfile.fullname, @@ -264,7 +264,6 @@ export class AccountService { const credential = await this.accountRepository.getCredentialIdByEmailOrId({ id, }); - console.log('credential', credential); if (!credential) { throw new EmailNotFoundException(); From 3122b7713564e7315b2f71333f490810e913c8c4 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 30 Jan 2024 19:02:16 -0300 Subject: [PATCH 07/29] LOR-124: fix: update name of function to getCredentialIdByEmailOrId --- test/unit/account.service.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/account.service.spec.ts b/test/unit/account.service.spec.ts index 27415bc..85724ae 100644 --- a/test/unit/account.service.spec.ts +++ b/test/unit/account.service.spec.ts @@ -13,7 +13,7 @@ describe('AccountService unit test', () => { const accountRepositoryMock = { saveCredentialParentAndChildrenProps: jest.fn(), - getCredentialIdByEmail: jest + getCredentialIdByEmailOrId: jest .fn() .mockReturnValue(stubs.getCredentialOutput), savePasswordResetInformation: jest.fn(), @@ -84,7 +84,7 @@ describe('AccountService unit test', () => { it('Unhappy path - should return void', async () => { jest - .spyOn(accountRepositoryMock, 'getCredentialIdByEmail') + .spyOn(accountRepositoryMock, 'getCredentialIdByEmailOrId') .mockReturnValueOnce(null); try { From c530e94f75151027b56516f326ac36c3fe961a28 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 30 Jan 2024 20:50:46 -0300 Subject: [PATCH 08/29] LOR-214: feat: add guard and swagger tags --- src/modules/account/account.controller.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/modules/account/account.controller.ts b/src/modules/account/account.controller.ts index 3894000..19ea86b 100644 --- a/src/modules/account/account.controller.ts +++ b/src/modules/account/account.controller.ts @@ -6,9 +6,10 @@ import { Put, Param, Get, + UseGuards, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { ApiResponse, ApiTags } from '@nestjs/swagger'; +import { ApiBearerAuth, ApiResponse, ApiTags } from '@nestjs/swagger'; import { MailService } from '../mail/mail.service'; import { AccountService } from './account.service'; import { responses } from 'src/globals/responses/docs'; @@ -19,6 +20,7 @@ import { ResetPasswordDto, SetPasswordDto, } from './account.dto'; +import { AuthorizationGuard, RequestToken } from '../../guard'; @Controller('/auth') export class AccountController { @@ -113,8 +115,11 @@ export class AccountController { message: 'Senha redefinida com sucesso', }; } + @UseGuards(AuthorizationGuard) + @RequestToken({ type: 'access', role: 'user' }) + @ApiBearerAuth('access') @Get('/user/:id') - @ApiTags('Authentication') + @ApiTags('User') @HttpCode(200) @ApiResponse(responses.ok) @ApiResponse(responses.badRequest) From 1309b885a13933f92d0ed456f456b4da79f5edbf Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 6 Feb 2024 20:42:51 -0300 Subject: [PATCH 09/29] LOR-214 feat User decorator --- src/decorators/account.decorator.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/decorators/account.decorator.ts diff --git a/src/decorators/account.decorator.ts b/src/decorators/account.decorator.ts new file mode 100644 index 0000000..22c45e4 --- /dev/null +++ b/src/decorators/account.decorator.ts @@ -0,0 +1,8 @@ +import { createParamDecorator, ExecutionContext } from '@nestjs/common'; + +export const User = createParamDecorator( + (data: unknown, ctx: ExecutionContext) => { + const request = ctx.switchToHttp().getRequest(); + return request['session.payload'].cid; + }, +); From 4c93f0b3fa11c07ff620d4282cf6c33af97bb42b Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 6 Feb 2024 20:46:34 -0300 Subject: [PATCH 10/29] LOR-214 feat: add validation to check if logged user is the same as userId --- src/modules/account/account.service.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/modules/account/account.service.ts b/src/modules/account/account.service.ts index bd882bc..a0973ab 100644 --- a/src/modules/account/account.service.ts +++ b/src/modules/account/account.service.ts @@ -260,11 +260,14 @@ export class AccountService { user, }; } - async getCredential(id: string) { + async getCredential(id: string, userId: string) { + if (id != userId) { + throw new InvalidCredentialsException(); + } + const credential = await this.accountRepository.getCredentialIdByEmailOrId({ id, }); - if (!credential) { throw new EmailNotFoundException(); } From f63d328a0fa428e2b290358af778adce721ad7f7 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 6 Feb 2024 20:46:58 -0300 Subject: [PATCH 11/29] LOR-214 feat: add User decorator to check user logged --- src/modules/account/account.controller.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/account/account.controller.ts b/src/modules/account/account.controller.ts index 19ea86b..313e24c 100644 --- a/src/modules/account/account.controller.ts +++ b/src/modules/account/account.controller.ts @@ -21,6 +21,7 @@ import { SetPasswordDto, } from './account.dto'; import { AuthorizationGuard, RequestToken } from '../../guard'; +import { User } from '../../decorators/account.decorator'; @Controller('/auth') export class AccountController { @@ -125,8 +126,8 @@ export class AccountController { @ApiResponse(responses.badRequest) @ApiResponse(responses.unauthorized) @ApiResponse(responses.internalError) - async getCredential(@Param() id: string) { - const response = await this.accountService.getCredential(id); + async getCredential(@Param('id') id: string, @User() userId: string) { + const response = await this.accountService.getCredential(id, userId); return response; } } From 02baf6e32b933cacda4e85fa1fc89a1a33e9081e Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 6 Feb 2024 20:47:43 -0300 Subject: [PATCH 12/29] LOR-214 feat: update getCredentialIdByEmailOrId --- src/modules/account/account.repository.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/account/account.repository.ts b/src/modules/account/account.repository.ts index 4ebb67c..5e71191 100644 --- a/src/modules/account/account.repository.ts +++ b/src/modules/account/account.repository.ts @@ -54,7 +54,7 @@ export class AccountRepository { if (idOrEmail.hashedEmail) { whereCondition = { email: idOrEmail.hashedEmail }; } else if (idOrEmail.id) { - whereCondition = { id: idOrEmail.id[0] }; + whereCondition = { id: idOrEmail.id }; } else { throw new Error('Invalid input'); } From 5e51479b57c5fa4290aae2f4bbd9d44ab5fb565f Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 6 Feb 2024 20:59:26 -0300 Subject: [PATCH 13/29] LOR-214 feat: add cid to login response --- src/modules/account/account.service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/account/account.service.ts b/src/modules/account/account.service.ts index a0973ab..7fd5265 100644 --- a/src/modules/account/account.service.ts +++ b/src/modules/account/account.service.ts @@ -244,6 +244,7 @@ export class AccountService { pid: credential.parentProfile.id, }; const user = { + cid: credential.id, pid: credential.parentProfile.id, parentName: credential.parentProfile.fullname, childrens: credential.parentProfile.childrens, From 14e332c480df93dfbcb2ff8f07f7232181a76097 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 6 Feb 2024 21:27:44 -0300 Subject: [PATCH 14/29] LOR-214 fix: update id --- src/modules/account/account.controller.ts | 6 +++--- src/modules/account/account.service.ts | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/modules/account/account.controller.ts b/src/modules/account/account.controller.ts index 313e24c..a4f469a 100644 --- a/src/modules/account/account.controller.ts +++ b/src/modules/account/account.controller.ts @@ -119,15 +119,15 @@ export class AccountController { @UseGuards(AuthorizationGuard) @RequestToken({ type: 'access', role: 'user' }) @ApiBearerAuth('access') - @Get('/user/:id') + @Get('/user') @ApiTags('User') @HttpCode(200) @ApiResponse(responses.ok) @ApiResponse(responses.badRequest) @ApiResponse(responses.unauthorized) @ApiResponse(responses.internalError) - async getCredential(@Param('id') id: string, @User() userId: string) { - const response = await this.accountService.getCredential(id, userId); + async getCredential(@User() id: string) { + const response = await this.accountService.getCredential(id); return response; } } diff --git a/src/modules/account/account.service.ts b/src/modules/account/account.service.ts index 7fd5265..e8b99ce 100644 --- a/src/modules/account/account.service.ts +++ b/src/modules/account/account.service.ts @@ -261,11 +261,7 @@ export class AccountService { user, }; } - async getCredential(id: string, userId: string) { - if (id != userId) { - throw new InvalidCredentialsException(); - } - + async getCredential(id: string) { const credential = await this.accountRepository.getCredentialIdByEmailOrId({ id, }); From 1333fb59c4e736afb327568dde8ae241f47e1be1 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 6 Feb 2024 21:56:41 -0300 Subject: [PATCH 15/29] LOR-214 fix: separa functions to get id by mail and by token --- src/modules/account/account.controller.ts | 5 +-- src/modules/account/account.entity.d.ts | 7 ++++ src/modules/account/account.repository.ts | 49 ++++++++++++++++------- src/modules/account/account.service.ts | 10 ++--- test/unit/account.service.spec.ts | 4 +- 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/modules/account/account.controller.ts b/src/modules/account/account.controller.ts index a4f469a..547a105 100644 --- a/src/modules/account/account.controller.ts +++ b/src/modules/account/account.controller.ts @@ -4,7 +4,6 @@ import { Body, HttpCode, Put, - Param, Get, UseGuards, } from '@nestjs/common'; @@ -119,8 +118,8 @@ export class AccountController { @UseGuards(AuthorizationGuard) @RequestToken({ type: 'access', role: 'user' }) @ApiBearerAuth('access') - @Get('/user') - @ApiTags('User') + @Get('/account') + @ApiTags('Account') @HttpCode(200) @ApiResponse(responses.ok) @ApiResponse(responses.badRequest) diff --git a/src/modules/account/account.entity.d.ts b/src/modules/account/account.entity.d.ts index 630a752..ad7ae36 100644 --- a/src/modules/account/account.entity.d.ts +++ b/src/modules/account/account.entity.d.ts @@ -33,6 +33,13 @@ export type GetCredentialIdByEmailOutput = Pick< >; }; }; +export type GetCredential = Pick & { + parentProfile: Pick & { + childrens: Array< + Pick + >; + }; +}; export type getCredentialIdByRecoveryTokenInput = { hashedToken: string; diff --git a/src/modules/account/account.repository.ts b/src/modules/account/account.repository.ts index 5e71191..1754f6c 100644 --- a/src/modules/account/account.repository.ts +++ b/src/modules/account/account.repository.ts @@ -7,6 +7,7 @@ import { getCredentialIdByRecoveryTokenInput, getCredentialIdByRecoveryTokenOutout, SavePasswordInput, + GetCredential, } from './account.entity'; import { handleErrors } from 'src/globals/errors'; @@ -46,26 +47,46 @@ export class AccountRepository { return; } - async getCredentialIdByEmailOrId(idOrEmail: { - id?: string; - hashedEmail?: string; - }): Promise { - let whereCondition = {}; - if (idOrEmail.hashedEmail) { - whereCondition = { email: idOrEmail.hashedEmail }; - } else if (idOrEmail.id) { - whereCondition = { id: idOrEmail.id }; - } else { - throw new Error('Invalid input'); - } + async getCredentialIdByEmail( + hashedemail, + ): Promise { + const response = await this.prisma.credential + .findUnique({ + where: { email: hashedemail }, + select: { + id: true, + email: true, + password: true, + parentProfile: { + select: { + id: true, + fullname: true, + childrens: { + select: { + id: true, + fullname: true, + gender: true, + birthdate: true, + }, + }, + }, + }, + }, + }) + .then((response) => { + return response; + }) + .catch((error) => handleErrors(error)); + return response; + } + async getCredentialId(id: string): Promise { const response = await this.prisma.credential .findFirst({ - where: whereCondition, + where: { id }, select: { id: true, email: true, - password: true, parentProfile: { select: { id: true, diff --git a/src/modules/account/account.service.ts b/src/modules/account/account.service.ts index e8b99ce..24d35ab 100644 --- a/src/modules/account/account.service.ts +++ b/src/modules/account/account.service.ts @@ -163,7 +163,7 @@ export class AccountService { // ! verificar responsabilidade única const hashedEmail = await this.hashData(email); - const account = await this.accountRepository.getCredentialIdByEmailOrId({ + const account = await this.accountRepository.getCredentialIdByEmail({ hashedEmail, }); @@ -223,9 +223,9 @@ export class AccountService { async login(email: string, password: string) { const hashedEmail = await this.hashData(email); - const credential = await this.accountRepository.getCredentialIdByEmailOrId({ + const credential = await this.accountRepository.getCredentialIdByEmail( hashedEmail, - }); + ); if (!credential) { throw new EmailNotFoundException(); @@ -262,9 +262,7 @@ export class AccountService { }; } async getCredential(id: string) { - const credential = await this.accountRepository.getCredentialIdByEmailOrId({ - id, - }); + const credential = await this.accountRepository.getCredentialId(id); if (!credential) { throw new EmailNotFoundException(); } diff --git a/test/unit/account.service.spec.ts b/test/unit/account.service.spec.ts index 85724ae..27415bc 100644 --- a/test/unit/account.service.spec.ts +++ b/test/unit/account.service.spec.ts @@ -13,7 +13,7 @@ describe('AccountService unit test', () => { const accountRepositoryMock = { saveCredentialParentAndChildrenProps: jest.fn(), - getCredentialIdByEmailOrId: jest + getCredentialIdByEmail: jest .fn() .mockReturnValue(stubs.getCredentialOutput), savePasswordResetInformation: jest.fn(), @@ -84,7 +84,7 @@ describe('AccountService unit test', () => { it('Unhappy path - should return void', async () => { jest - .spyOn(accountRepositoryMock, 'getCredentialIdByEmailOrId') + .spyOn(accountRepositoryMock, 'getCredentialIdByEmail') .mockReturnValueOnce(null); try { From aa2b333fb0db40ca1ad3a77aaab4049f1a3aa605 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Wed, 7 Feb 2024 09:10:12 -0300 Subject: [PATCH 16/29] LOR-214: remove pid and cid from login response --- src/modules/account/account.service.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/modules/account/account.service.ts b/src/modules/account/account.service.ts index 24d35ab..13a4ba1 100644 --- a/src/modules/account/account.service.ts +++ b/src/modules/account/account.service.ts @@ -244,8 +244,6 @@ export class AccountService { pid: credential.parentProfile.id, }; const user = { - cid: credential.id, - pid: credential.parentProfile.id, parentName: credential.parentProfile.fullname, childrens: credential.parentProfile.childrens, }; From 2ab71e0afabd67f7e6be8b19e795fb20f82c20ae Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Wed, 7 Feb 2024 10:03:01 -0300 Subject: [PATCH 17/29] LOR-214: feat: add message to get account response --- src/modules/account/account.controller.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/account/account.controller.ts b/src/modules/account/account.controller.ts index 547a105..0631016 100644 --- a/src/modules/account/account.controller.ts +++ b/src/modules/account/account.controller.ts @@ -126,7 +126,7 @@ export class AccountController { @ApiResponse(responses.unauthorized) @ApiResponse(responses.internalError) async getCredential(@User() id: string) { - const response = await this.accountService.getCredential(id); - return response; + const data = await this.accountService.getCredential(id); + return { message: 'Dados recebidos com sucesso', data }; } } From 2cea824557922bea088de1a51ea54628d22616d6 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Wed, 7 Feb 2024 10:03:39 -0300 Subject: [PATCH 18/29] LOR-214 - feat: add email to GetCredential type --- src/modules/account/account.entity.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/account/account.entity.d.ts b/src/modules/account/account.entity.d.ts index ad7ae36..c41857d 100644 --- a/src/modules/account/account.entity.d.ts +++ b/src/modules/account/account.entity.d.ts @@ -33,7 +33,7 @@ export type GetCredentialIdByEmailOutput = Pick< >; }; }; -export type GetCredential = Pick & { +export type GetCredential = Pick & { parentProfile: Pick & { childrens: Array< Pick From a9bb6f06cb616d47427c7fc1778a9fdcb72460ed Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Wed, 7 Feb 2024 10:04:16 -0300 Subject: [PATCH 19/29] LOR-214 - feat: add test to Account --- test/unit/account.service.spec.ts | 45 ++++++++++++++++++++++++++++++ test/unit/account.service.stubs.ts | 17 +++++++++++ 2 files changed, 62 insertions(+) diff --git a/test/unit/account.service.spec.ts b/test/unit/account.service.spec.ts index 27415bc..1623c09 100644 --- a/test/unit/account.service.spec.ts +++ b/test/unit/account.service.spec.ts @@ -17,6 +17,7 @@ describe('AccountService unit test', () => { .fn() .mockReturnValue(stubs.getCredentialOutput), savePasswordResetInformation: jest.fn(), + getCredentialId: jest.fn().mockReturnValue(stubs.getCredentialOutputById), }; beforeEach(async () => { @@ -94,4 +95,48 @@ describe('AccountService unit test', () => { } }); }); + describe('Account', () => { + it('Happy path - should return account details', async () => { + const actual = await service.getCredential( + stubs.getCredentialOutputById.id, + ); + + expect(actual.credential).toHaveProperty( + 'message', + 'Dados recebidos com sucesso', + ); + expect(actual).toHaveProperty('credential'); + expect(actual.credential).toHaveProperty( + 'id', + stubs.getCredentialOutputById.id, + ); + expect(actual.credential).toHaveProperty( + 'email', + stubs.getCredentialOutputById.email, + ); + expect(actual.credential).toHaveProperty('parentProfile'); + expect(actual.credential.parentProfile).toHaveProperty( + 'id', + stubs.getCredentialOutputById.parentProfile.id, + ); + expect(actual.credential.parentProfile).toHaveProperty( + 'fullname', + stubs.getCredentialOutputById.parentProfile.fullname, + ); + expect(actual.credential.parentProfile).toHaveProperty('childrens'); + expect(actual.credential.parentProfile.childrens).toHaveLength(1); + }); + }); + + it('Unhappy path - should return void', async () => { + jest + .spyOn(accountRepositoryMock, 'getCredentialId') + .mockReturnValueOnce(null); + + try { + await service.getCredential(stubs.getCredentialOutputById.id); + } catch (actual) { + expect(actual).toBeInstanceOf(EmailNotFoundException); + } + }); }); diff --git a/test/unit/account.service.stubs.ts b/test/unit/account.service.stubs.ts index 87a059c..04d7f23 100644 --- a/test/unit/account.service.stubs.ts +++ b/test/unit/account.service.stubs.ts @@ -36,3 +36,20 @@ export const getCredentialOutput: GetCredentialIdByEmailOutput = { ], }, }; +export const getCredentialOutputById = { + message: 'Dados recebidos com sucesso', + id: faker.string.uuid(), + email: faker.internet.email(), + parentProfile: { + id: faker.string.uuid(), + fullname: faker.person.fullName(), + childrens: [ + { + id: faker.number.int(), + fullname: faker.person.fullName(), + birthdate: faker.date.birthdate(), + gender: faker.helpers.enumValue(Genders), + }, + ], + }, +}; From 25708dbd892c0a99139177591c4750112f869a61 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan <101154455+Daaaiii@users.noreply.github.com> Date: Tue, 20 Feb 2024 20:07:34 -0300 Subject: [PATCH 20/29] fix identation Co-authored-by: Cosmo <48590313+viniciuscosmome@users.noreply.github.com> --- prisma/schema.prisma | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index df7ce52..de006b2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -29,7 +29,7 @@ model ResetPasswordInfo { id Int @id @default(autoincrement()) recoveryToken String @unique @map("recovery_token") expiresIn DateTime @updatedAt @map("expires_in") - credentialId String @unique @map("credential_id") + credentialId String @unique @map("credential_id") credential Credential @relation(fields: [credentialId], references: [id], onDelete: Cascade) @@map("reset_password_info") From 0e4b97fdc376c30f0988c5144cc7eeda233bee86 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan <101154455+Daaaiii@users.noreply.github.com> Date: Tue, 20 Feb 2024 20:07:44 -0300 Subject: [PATCH 21/29] Update prisma/schema.prisma Co-authored-by: Cosmo <48590313+viniciuscosmome@users.noreply.github.com> --- prisma/schema.prisma | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index de006b2..4ca6f1f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -39,7 +39,7 @@ model ParentProfile { id String @id @default(uuid()) fullname String childrens ChildrenProfile[] - credentialId String @unique @map("credential_id") + credentialId String @unique @map("credential_id") credential Credential @relation(fields: [credentialId], references: [id], onDelete: Cascade) updatedAt DateTime @updatedAt @map("updated_at") createdAt DateTime @default(now()) @map("created_at") From dc169037d6aabfc2b1d9d3b9f382f157e57e5648 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan <101154455+Daaaiii@users.noreply.github.com> Date: Tue, 20 Feb 2024 20:07:57 -0300 Subject: [PATCH 22/29] Update src/decorators/account.decorator.ts Co-authored-by: Cosmo <48590313+viniciuscosmome@users.noreply.github.com> --- src/decorators/account.decorator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decorators/account.decorator.ts b/src/decorators/account.decorator.ts index 22c45e4..b387469 100644 --- a/src/decorators/account.decorator.ts +++ b/src/decorators/account.decorator.ts @@ -1,4 +1,4 @@ -import { createParamDecorator, ExecutionContext } from '@nestjs/common'; +import { createParamDecorator, type ExecutionContext } from '@nestjs/common'; export const User = createParamDecorator( (data: unknown, ctx: ExecutionContext) => { From 48e3c130fea4f014b766694ed8610984804bc281 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan <101154455+Daaaiii@users.noreply.github.com> Date: Tue, 20 Feb 2024 20:08:10 -0300 Subject: [PATCH 23/29] Update src/modules/account/account.repository.ts Co-authored-by: Cosmo <48590313+viniciuscosmome@users.noreply.github.com> --- src/modules/account/account.repository.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/account/account.repository.ts b/src/modules/account/account.repository.ts index 1754f6c..02324ca 100644 --- a/src/modules/account/account.repository.ts +++ b/src/modules/account/account.repository.ts @@ -48,7 +48,7 @@ export class AccountRepository { } async getCredentialIdByEmail( - hashedemail, + hashedemail: string, ): Promise { const response = await this.prisma.credential .findUnique({ From 5161607cb58fbbd1552eb0f8fa90899d012e610b Mon Sep 17 00:00:00 2001 From: Daiane Bolzan <101154455+Daaaiii@users.noreply.github.com> Date: Tue, 20 Feb 2024 20:08:23 -0300 Subject: [PATCH 24/29] Update src/modules/account/account.repository.ts Co-authored-by: Cosmo <48590313+viniciuscosmome@users.noreply.github.com> --- src/modules/account/account.repository.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/account/account.repository.ts b/src/modules/account/account.repository.ts index 02324ca..0e67db7 100644 --- a/src/modules/account/account.repository.ts +++ b/src/modules/account/account.repository.ts @@ -80,6 +80,7 @@ export class AccountRepository { return response; } + async getCredentialId(id: string): Promise { const response = await this.prisma.credential .findFirst({ From 3c4b205cff83f1d889dcc938a3b7b6bfce4c6081 Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 20 Feb 2024 20:16:14 -0300 Subject: [PATCH 25/29] fix: remove email from getCredentialIdByEmail return --- src/modules/account/account.repository.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/modules/account/account.repository.ts b/src/modules/account/account.repository.ts index 0e67db7..54ced93 100644 --- a/src/modules/account/account.repository.ts +++ b/src/modules/account/account.repository.ts @@ -55,7 +55,6 @@ export class AccountRepository { where: { email: hashedemail }, select: { id: true, - email: true, password: true, parentProfile: { select: { @@ -80,7 +79,7 @@ export class AccountRepository { return response; } - + async getCredentialId(id: string): Promise { const response = await this.prisma.credential .findFirst({ From 99fb6260443204384fb09018b667ab48d7e9657f Mon Sep 17 00:00:00 2001 From: Daiane Bolzan Date: Tue, 20 Feb 2024 20:16:31 -0300 Subject: [PATCH 26/29] fix: remove wrong object --- src/modules/account/account.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/account/account.service.ts b/src/modules/account/account.service.ts index 13a4ba1..3b9c6cf 100644 --- a/src/modules/account/account.service.ts +++ b/src/modules/account/account.service.ts @@ -163,9 +163,9 @@ export class AccountService { // ! verificar responsabilidade única const hashedEmail = await this.hashData(email); - const account = await this.accountRepository.getCredentialIdByEmail({ + const account = await this.accountRepository.getCredentialIdByEmail( hashedEmail, - }); + ); if (!account) { throw new EmailNotFoundException(); From 742e7ce870b94dce005481e02aa4b52f670d1842 Mon Sep 17 00:00:00 2001 From: Kauane Santos Date: Tue, 16 Apr 2024 16:34:10 -0300 Subject: [PATCH 27/29] refactor: use findunique at getCredentialId --- src/modules/account/account.repository.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/account/account.repository.ts b/src/modules/account/account.repository.ts index 54ced93..645dccb 100644 --- a/src/modules/account/account.repository.ts +++ b/src/modules/account/account.repository.ts @@ -82,7 +82,7 @@ export class AccountRepository { async getCredentialId(id: string): Promise { const response = await this.prisma.credential - .findFirst({ + .findUnique({ where: { id }, select: { id: true, From daa1484d3d02ed09b16dac9c822e17cd6cf7a0f6 Mon Sep 17 00:00:00 2001 From: Kauane Santos Date: Tue, 16 Apr 2024 16:35:26 -0300 Subject: [PATCH 28/29] fix: remove id from getCredentialId response --- src/modules/account/account.repository.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/modules/account/account.repository.ts b/src/modules/account/account.repository.ts index 645dccb..0206c48 100644 --- a/src/modules/account/account.repository.ts +++ b/src/modules/account/account.repository.ts @@ -85,11 +85,9 @@ export class AccountRepository { .findUnique({ where: { id }, select: { - id: true, email: true, parentProfile: { select: { - id: true, fullname: true, childrens: { select: { From 070f1d64d860562a8d2e4ce3d1ad0c64684fa93c Mon Sep 17 00:00:00 2001 From: Kauane Santos Date: Tue, 16 Apr 2024 16:49:43 -0300 Subject: [PATCH 29/29] fix: remove id field from account entity --- src/modules/account/account.entity.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/account/account.entity.d.ts b/src/modules/account/account.entity.d.ts index c41857d..780ec7d 100644 --- a/src/modules/account/account.entity.d.ts +++ b/src/modules/account/account.entity.d.ts @@ -33,10 +33,10 @@ export type GetCredentialIdByEmailOutput = Pick< >; }; }; -export type GetCredential = Pick & { - parentProfile: Pick & { +export type GetCredential = Pick & { + parentProfile: Pick & { childrens: Array< - Pick + Pick >; }; };