From 7cdb2646e32bd7ee5043f4577eabbbaa21a77f57 Mon Sep 17 00:00:00 2001 From: Luana Date: Sun, 22 Oct 2023 21:30:04 -0300 Subject: [PATCH 01/32] config(CC-81): add folders and files to create clinic endpoint --- .../src/controllers/api/api.module.ts | 4 +- .../create-clinic/create-clinic.controller.ts | 38 ++++++++ .../create-clinic/create-clinic.e2e-spec.ts | 94 +++++++++++++++++++ .../nestjs-create-clinic.service.ts | 10 ++ .../create-psychologist/client.http | 0 .../create-psychologist.controller.ts | 2 +- .../create-psychologist.e2e-spec.ts | 0 .../create-psychologist/docs.ts | 0 .../nestjs-create-psychologist.service.ts | 0 .../postgresql-prisma-clinic-mapper.ts | 46 +++++++++ .../adapters/tests/factories/make-clinic.ts | 45 +++++++++ 11 files changed, 236 insertions(+), 3 deletions(-) create mode 100644 libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts create mode 100644 libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts create mode 100644 libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/nestjs-create-clinic.service.ts rename libs/core-rest-api/adapters/src/controllers/api/use-case/{ => psychologist}/create-psychologist/client.http (100%) rename libs/core-rest-api/adapters/src/controllers/api/use-case/{ => psychologist}/create-psychologist/create-psychologist.controller.ts (95%) rename libs/core-rest-api/adapters/src/controllers/api/use-case/{ => psychologist}/create-psychologist/create-psychologist.e2e-spec.ts (100%) rename libs/core-rest-api/adapters/src/controllers/api/use-case/{ => psychologist}/create-psychologist/docs.ts (100%) rename libs/core-rest-api/adapters/src/controllers/api/use-case/{ => psychologist}/create-psychologist/nestjs-create-psychologist.service.ts (100%) create mode 100644 libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts create mode 100644 libs/core-rest-api/adapters/tests/factories/make-clinic.ts diff --git a/libs/core-rest-api/adapters/src/controllers/api/api.module.ts b/libs/core-rest-api/adapters/src/controllers/api/api.module.ts index f08fe1c..930da1a 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/api.module.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/api.module.ts @@ -6,8 +6,8 @@ import { PostgreSqlPrismaOrmService } from '../../database/infra/prisma/prisma.s import { DatabaseRepositoriesModule } from '../../database/repositories/repositories.module'; import { envSchema } from '../../env/env'; import { EnvModule } from '../../env/env.module'; -import { CreatePsychologistController } from './use-case/create-psychologist/create-psychologist.controller'; -import { NestjsCreatePsychologistService } from './use-case/create-psychologist/nestjs-create-psychologist.service'; +import { CreatePsychologistController } from './use-case/psychologist/create-psychologist/create-psychologist.controller'; +import { NestjsCreatePsychologistService } from './use-case/psychologist/create-psychologist/nestjs-create-psychologist.service'; @Module({ imports: [ diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts new file mode 100644 index 0000000..b3e3381 --- /dev/null +++ b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts @@ -0,0 +1,38 @@ +// eslint-disable-next-line @nx/enforce-module-boundaries +import { CreateClinicDto } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto'; +import { GlobalAppHttpException } from '@clinicControl/core-rest-api/core/src/shared/errors/globalAppHttpException'; +import { applicationValidateOrReject } from '@clinicControl/core-rest-api/core/src/shared/validators/validate-or-reject'; +import { Body, Controller, Post, UseGuards } from '@nestjs/common'; +import { ApiTags } from '@nestjs/swagger'; +import { plainToInstance } from 'class-transformer'; +import { ApiKeyGuard } from '../../../guards/api-key.guard'; +import { NestjsCreateClinicService } from './nestjs-create-clinic.service'; + +@ApiTags() +@Controller({ + path: 'clinic', +}) +export class CreateClinicController { + constructor( + private createClinicService: NestjsCreateClinicService + ) {} + + @Post('create') + @UseGuards(ApiKeyGuard) + async execute( + @Body() createClinicDto: CreateClinicDto + ): Promise { + try { + const createClinicDtoInstance = plainToInstance( + CreateClinicDto, + createClinicDto + ); + + await applicationValidateOrReject(createClinicDtoInstance); + + await this.createClinicService.execute(createClinicDto); + } catch (error: unknown) { + throw new GlobalAppHttpException(error); + } + } +} diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts new file mode 100644 index 0000000..108b0ef --- /dev/null +++ b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -0,0 +1,94 @@ +import request from 'supertest'; + +import { INestApplication } from '@nestjs/common'; +import { Test } from '@nestjs/testing'; + +import { makePsychologist } from '../../../../../tests/factories/make-psychologist'; +import { PostgreSqlPrismaOrmService } from '../../../../database/infra/prisma/prisma.service'; +import { ApiModule } from '../../api.module'; + +describe('[E2E] - Create Clinic', () => { + let app: INestApplication; + let prisma: PostgreSqlPrismaOrmService; + + beforeAll(async () => { + const moduleRef = await Test.createTestingModule({ + imports: [ApiModule], + }).compile(); + + app = moduleRef.createNestApplication(); + + prisma = moduleRef.get(PostgreSqlPrismaOrmService); + + await app.init(); + }); + + it('[POST] - Should successfully create a new clinic', async () => { + const newClinic = makePsychologist(); + + const response = await request(app.getHttpServer()) + .post('/psychologist/create') + .set('api-key', 'api-key') + .send(newPsychologist); + + expect(response.statusCode).toBe(201); + + const userOnDatabase = await prisma.psychologist.findUnique({ + where: { + email: 'novo_usuario_teste@gmail.com', + }, + }); + + expect(userOnDatabase).toBeTruthy(); + }); + + it('[POST] - Should return an error when trying to create a new psychologist without api-key', async () => { + const newPsychologist = makePsychologist({ + email: 'novo_usuario_teste_not_created@gmail.com', + }); + + const response = await request(app.getHttpServer()) + .post('/psychologist/create') + .send(newPsychologist); + + expect(response.statusCode).toBe(401); + + const userOnDatabase = await prisma.psychologist.findUnique({ + where: { + email: 'novo_usuario_teste_not_created@gmail.com', + }, + }); + + expect(userOnDatabase).toBeFalsy(); + expect(response.body.message).toBe('API key is missing'); + }); + + it('[POST] - Should return an error when trying to create a new psychologist that already exists', async () => { + const newPsychologist = makePsychologist({ + email: 'novo_usuario_teste_new_entrie@gmail.com', + }); + + const response = await request(app.getHttpServer()) + .post('/psychologist/create') + .set('api-key', 'api-key') + .send(newPsychologist); + + expect(response.statusCode).toBe(201); + + const userOnDatabase = await prisma.psychologist.findUnique({ + where: { + email: 'novo_usuario_teste_new_entrie@gmail.com', + }, + }); + + expect(userOnDatabase).toBeTruthy(); + + const response_new_post = await request(app.getHttpServer()) + .post('/psychologist/create') + .set('api-key', 'api-key') + .send(newPsychologist); + + expect(response_new_post.statusCode).toBe(409); + expect(response_new_post.body.message).toBe('psychologist already exists'); + }); +}); diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/nestjs-create-clinic.service.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/nestjs-create-clinic.service.ts new file mode 100644 index 0000000..c85c3d7 --- /dev/null +++ b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/nestjs-create-clinic.service.ts @@ -0,0 +1,10 @@ +import { ClinicDatabaseRepository } from '@clinicControl/core-rest-api/core/src/domains/clinic/repositories/database-repository'; +import { CreateClinicService } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic.service'; +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class NestjsCreateClinicService extends CreateClinicService { + constructor(clinicDatabaseRepository: ClinicDatabaseRepository) { + super(clinicDatabaseRepository); + } +} diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/client.http similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/client.http rename to libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/client.http diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/create-psychologist.controller.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.controller.ts similarity index 95% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/create-psychologist.controller.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.controller.ts index 7215d05..54cf99b 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/create-psychologist.controller.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.controller.ts @@ -5,7 +5,7 @@ import { applicationValidateOrReject } from '@clinicControl/core-rest-api/core/s import { Body, Controller, Post, UseGuards } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { plainToInstance } from 'class-transformer'; -import { ApiKeyGuard } from '../../guards/api-key.guard'; +import { ApiKeyGuard } from '../../../guards/api-key.guard'; import { NestjsCreatePsychologistService } from './nestjs-create-psychologist.service'; @ApiTags() diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/create-psychologist.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.e2e-spec.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/create-psychologist.e2e-spec.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.e2e-spec.ts diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/docs.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/docs.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/docs.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/docs.ts diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/nestjs-create-psychologist.service.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/nestjs-create-psychologist.service.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/create-psychologist/nestjs-create-psychologist.service.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/nestjs-create-psychologist.service.ts diff --git a/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts b/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts new file mode 100644 index 0000000..e2f4209 --- /dev/null +++ b/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts @@ -0,0 +1,46 @@ +import { ClinicEntity } from '@clinicControl/core-rest-api/core/src/domains/clinic/entities/clinic/entity'; +import { CreateClinicDto } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto'; +import { + Plan, + Role, +} from '@clinicControl/core-rest-api/core/src/shared/interfaces/payments'; +import { + Prisma, + Clinic as PrismaClinicDto, +} from '@prisma/client'; + +export class PostgresqlPrismaClinicMapper { + static toDomain(raw: PrismaClinicDto): ClinicEntity { + return new ClinicEntity({ + ...raw, + role: raw.role as unknown as Role, + plan: raw.plan as unknown as Plan, + }); + } + + static toDomainMany(raw: PrismaClinicDto[]): ClinicEntity[] { + return raw.map((psychologist) => this.toDomain(psychologist)); + } + + static toPrismaCreate(raw: CreateClinicDto): Prisma.ClinicCreateArgs { + + return { + data: { + ...raw, + }, + }; + } + + static toPrismaUpdate(raw: UpdateClinicDto): Prisma.ClinicUpdateArgs { + return { + data: { + ...raw, + // role: raw.role as unknown as PrismaRole, + // plan: raw.plan as unknown as PrismaPlan, + }, + where: { + id: raw.id, + }, + }; + } +} diff --git a/libs/core-rest-api/adapters/tests/factories/make-clinic.ts b/libs/core-rest-api/adapters/tests/factories/make-clinic.ts new file mode 100644 index 0000000..8218c79 --- /dev/null +++ b/libs/core-rest-api/adapters/tests/factories/make-clinic.ts @@ -0,0 +1,45 @@ +import { fakerPT_BR as faker } from '@faker-js/faker'; +import { Injectable } from '@nestjs/common'; + +import { ClinicEntity } from '@clinicControl/core-rest-api/core/src/domains/clinic/entities/clinic/entity'; +import { CreateClinicDto } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto'; +import { randomUUID } from 'crypto'; +import { PostgreSqlPrismaOrmService } from '../../src/database/infra/prisma/prisma.service'; +import { PostgresqlPrismaPsychologistMapper } from '../../src/database/mappers/postgresql-prisma-psychologist-mapper'; + +/* + Creating a factory for the Clinic entity, which will be used to create tests for the domain's use cases +*/ +export function makeClinic( + override: Partial = {} +): ClinicEntity { + const newClinic = new ClinicEntity({ + name: 'Nova Clinica', + city: faker.location.city(), + state: faker.location.state(), + psychologistId: randomUUID(), //TODO -> PSYCHOLOGIST ID + ...override, + }); + + return newClinic; +} + +/* + Creating a factory for Prisma's Clinic entity, which will be used to create e2e tests (controllers) +*/ +@Injectable() +export class ClinicFactory { + constructor(private postgreSqlPrismaOrmService: PostgreSqlPrismaOrmService) {} + + async makePrismaClinic( + clinic: CreateClinicDto + ): Promise { + const newPrismaClinic = makeClinic(clinic); + + await this.postgreSqlPrismaOrmService['clinic'].create( + PostgresqlPrismaPsychologistMapper.toPrismaCreate(newPrismaClinic) + ); + + return newPrismaClinic; + } +} From 65d8c6cf198c4512cfe206cfd7ff6a84aaf5684a Mon Sep 17 00:00:00 2001 From: Luana Date: Mon, 23 Oct 2023 09:04:21 -0300 Subject: [PATCH 02/32] feat(CC-81): adjust test --- .../api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts index 108b0ef..0cad330 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -24,6 +24,7 @@ describe('[E2E] - Create Clinic', () => { }); it('[POST] - Should successfully create a new clinic', async () => { + const {id} = makePsychologist(); const newClinic = makePsychologist(); const response = await request(app.getHttpServer()) From 0b714fc854d630754c53c328fe957c123d4b2fcb Mon Sep 17 00:00:00 2001 From: Luana Date: Thu, 26 Oct 2023 21:41:07 -0300 Subject: [PATCH 03/32] feat(CC-81): add prisma clinic repository and rename psychologist repository --- .../postgres-prisma-orm-clinic-repository.ts | 109 ++++++++++++++++++ ...sql-prisma-orm-psychologist-repository.ts} | 2 +- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts rename libs/core-rest-api/adapters/src/database/repositories/psychologist/{postgresql-prisma-orm-repository.ts => postgresql-prisma-orm-psychologist-repository.ts} (97%) diff --git a/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts b/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts new file mode 100644 index 0000000..c500597 --- /dev/null +++ b/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts @@ -0,0 +1,109 @@ +import { ClinicEntity } from '@clinicControl/core-rest-api/core/src/domains/clinic/entities/clinic/entity'; +import { ClinicDatabaseRepository } from '@clinicControl/core-rest-api/core/src/domains/clinic/repositories/database-repository'; +import { CreateClinicDto } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto'; +import { UpdateClinicDto } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/update-clinic/update-clinic-dto'; +import { CLINIC_ERROR_MESSAGES } from '@clinicControl/core-rest-api/core/src/shared/errors/error-messages'; +import { ConflictException, Injectable } from '@nestjs/common'; +import { PostgreSqlPrismaOrmService } from '../../../database/infra/prisma/prisma.service'; +import { PostgresqlPrismaClinicMapper } from '../../mappers/postgresql-prisma-clinic-mapper'; + +@Injectable() +export class PostgresqlPrismaOrmClinicRepository + implements ClinicDatabaseRepository +{ + constructor( + private postgreSqlPrismaOrmService: PostgreSqlPrismaOrmService, + ) {} + + async createClinic( + clinic: CreateClinicDto + ): Promise { + const isClinicExists = await this.findClinicByName(clinic.name); + + if (isClinicExists) { + throw new ConflictException( + CLINIC_ERROR_MESSAGES['CONFLICTING_NAME'] + ); + } + + const toPrismaEntity = PostgresqlPrismaClinicMapper.toPrismaCreate({ + ...clinic, + + }); + + const newClinic = await this.postgreSqlPrismaOrmService['clinic'].create( + toPrismaEntity + ); + + return PostgresqlPrismaClinicMapper.toDomain(newClinic); + } + + async findClinicByName(name: string): Promise { + + const clinic = await this.postgreSqlPrismaOrmService['clinic'].findFirst( //-> Method find first, keep it? + { + where: { + name: name, + }, + } + ); + + if (!clinic) { + return null; + } + + return PostgresqlPrismaClinicMapper.toDomain(clinic); + } + + async findClinicById(id: string): Promise { + const clinic = await this.postgreSqlPrismaOrmService['clinic'].findUnique( + { + where: { + id, + }, + } + ); + + if (!clinic) { + return null; + } + + return PostgresqlPrismaClinicMapper.toDomain(clinic); + } + + async getClinics(): Promise { + const clinics = await this.postgreSqlPrismaOrmService[ + 'clinic' + ].findMany(); + + return PostgresqlPrismaClinicMapper.toDomainMany(clinics); + } + + async updateClinic(newClinic: UpdateClinicDto): Promise { + const oldClinic = await this.findClinicById(newClinic.id); + + if (!oldClinic) { + throw new ConflictException(CLINIC_ERROR_MESSAGES['CONFLICTING_NAME']); + } + + const toPrismaEntity = + PostgresqlPrismaClinicMapper.toPrismaUpdate(newClinic); + + await this.postgreSqlPrismaOrmService['clinic'].update(toPrismaEntity); + } + + async deleteClinic(name: string): Promise { + const isClinicExists = await this.findClinicByName(name); + + if (!isClinicExists) { + throw new ConflictException(CLINIC_ERROR_MESSAGES['CLINIC_NOT_FOUND']); + } + + const clinicId = isClinicExists.id + await this.postgreSqlPrismaOrmService['clinic'].delete({ + where: { + id: clinicId, + }, + }); + } +} diff --git a/libs/core-rest-api/adapters/src/database/repositories/psychologist/postgresql-prisma-orm-repository.ts b/libs/core-rest-api/adapters/src/database/repositories/psychologist/postgresql-prisma-orm-psychologist-repository.ts similarity index 97% rename from libs/core-rest-api/adapters/src/database/repositories/psychologist/postgresql-prisma-orm-repository.ts rename to libs/core-rest-api/adapters/src/database/repositories/psychologist/postgresql-prisma-orm-psychologist-repository.ts index a293171..09f06b2 100644 --- a/libs/core-rest-api/adapters/src/database/repositories/psychologist/postgresql-prisma-orm-repository.ts +++ b/libs/core-rest-api/adapters/src/database/repositories/psychologist/postgresql-prisma-orm-psychologist-repository.ts @@ -5,7 +5,7 @@ import { UpdatePsychologistDto } from '@clinicControl/core-rest-api/core/src/dom import { HashGenerator } from '@clinicControl/core-rest-api/core/src/shared/cryptography/hash-generator'; import { PSYCHOLOGIST_ERROR_MESSAGES } from '@clinicControl/core-rest-api/core/src/shared/errors/error-messages'; import { ConflictException, Injectable } from '@nestjs/common'; -import { PostgreSqlPrismaOrmService } from '../../../database/infra/prisma/prisma.service'; +import { PostgreSqlPrismaOrmService } from '../../infra/prisma/prisma.service'; import { PostgresqlPrismaPsychologistMapper } from '../../mappers/postgresql-prisma-psychologist-mapper'; @Injectable() From a8c737e8dfa3a79cf46cf47e76f9e9360fac3937 Mon Sep 17 00:00:00 2001 From: Luana Date: Thu, 26 Oct 2023 21:43:32 -0300 Subject: [PATCH 04/32] feat(CC-81): add prisma clinic mapper --- .../postgresql-prisma-clinic-mapper.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts b/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts index e2f4209..e8aefc2 100644 --- a/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts +++ b/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts @@ -1,9 +1,6 @@ import { ClinicEntity } from '@clinicControl/core-rest-api/core/src/domains/clinic/entities/clinic/entity'; import { CreateClinicDto } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto'; -import { - Plan, - Role, -} from '@clinicControl/core-rest-api/core/src/shared/interfaces/payments'; +import { UpdateClinicDto } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/update-clinic/update-clinic-dto'; import { Prisma, Clinic as PrismaClinicDto, @@ -13,13 +10,11 @@ export class PostgresqlPrismaClinicMapper { static toDomain(raw: PrismaClinicDto): ClinicEntity { return new ClinicEntity({ ...raw, - role: raw.role as unknown as Role, - plan: raw.plan as unknown as Plan, }); } static toDomainMany(raw: PrismaClinicDto[]): ClinicEntity[] { - return raw.map((psychologist) => this.toDomain(psychologist)); + return raw.map((clinic) => this.toDomain(clinic)); } static toPrismaCreate(raw: CreateClinicDto): Prisma.ClinicCreateArgs { @@ -27,7 +22,13 @@ export class PostgresqlPrismaClinicMapper { return { data: { ...raw, - }, + city: 'xxxx', + name: 'yyyy', + state: 'SP', + updatedAt: new Date() + } + + }; } @@ -35,8 +36,6 @@ export class PostgresqlPrismaClinicMapper { return { data: { ...raw, - // role: raw.role as unknown as PrismaRole, - // plan: raw.plan as unknown as PrismaPlan, }, where: { id: raw.id, From 56b7d4e54e8afb6b7864071f8c9acd865e69d4e3 Mon Sep 17 00:00:00 2001 From: Luana Date: Thu, 26 Oct 2023 21:44:34 -0300 Subject: [PATCH 05/32] feat(CC-81): make clinic factory --- .../adapters/tests/factories/make-clinic.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libs/core-rest-api/adapters/tests/factories/make-clinic.ts b/libs/core-rest-api/adapters/tests/factories/make-clinic.ts index 8218c79..a909028 100644 --- a/libs/core-rest-api/adapters/tests/factories/make-clinic.ts +++ b/libs/core-rest-api/adapters/tests/factories/make-clinic.ts @@ -3,21 +3,21 @@ import { Injectable } from '@nestjs/common'; import { ClinicEntity } from '@clinicControl/core-rest-api/core/src/domains/clinic/entities/clinic/entity'; import { CreateClinicDto } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto'; -import { randomUUID } from 'crypto'; import { PostgreSqlPrismaOrmService } from '../../src/database/infra/prisma/prisma.service'; -import { PostgresqlPrismaPsychologistMapper } from '../../src/database/mappers/postgresql-prisma-psychologist-mapper'; +import { PostgresqlPrismaClinicMapper } from '../../src/database/mappers/postgresql-prisma-clinic-mapper'; /* Creating a factory for the Clinic entity, which will be used to create tests for the domain's use cases */ export function makeClinic( + psychologistId: string, override: Partial = {} ): ClinicEntity { const newClinic = new ClinicEntity({ name: 'Nova Clinica', city: faker.location.city(), state: faker.location.state(), - psychologistId: randomUUID(), //TODO -> PSYCHOLOGIST ID + psychologistId: psychologistId, ...override, }); @@ -34,10 +34,10 @@ export class ClinicFactory { async makePrismaClinic( clinic: CreateClinicDto ): Promise { - const newPrismaClinic = makeClinic(clinic); + const newPrismaClinic = makeClinic(clinic.psychologistId, clinic); await this.postgreSqlPrismaOrmService['clinic'].create( - PostgresqlPrismaPsychologistMapper.toPrismaCreate(newPrismaClinic) + PostgresqlPrismaClinicMapper.toPrismaCreate(newPrismaClinic) ); return newPrismaClinic; From 0ffbb4d833f5467bb7977fb7ffa251a2e7d3cac5 Mon Sep 17 00:00:00 2001 From: Luana Date: Thu, 26 Oct 2023 21:45:41 -0300 Subject: [PATCH 06/32] feat(CC-81): add repository in repositories module --- .../src/database/repositories/repositories.module.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/libs/core-rest-api/adapters/src/database/repositories/repositories.module.ts b/libs/core-rest-api/adapters/src/database/repositories/repositories.module.ts index 1d79e6e..101a241 100644 --- a/libs/core-rest-api/adapters/src/database/repositories/repositories.module.ts +++ b/libs/core-rest-api/adapters/src/database/repositories/repositories.module.ts @@ -1,8 +1,10 @@ +import { ClinicDatabaseRepository } from '@clinicControl/core-rest-api/core/src/domains/clinic/repositories/database-repository'; import { PsychologistDatabaseRepository } from '@clinicControl/core-rest-api/core/src/domains/psychologist/repositories/database-repository'; import { Module } from '@nestjs/common'; import { CryptographyModule } from '../../cryptography/cryptography.module'; import { PostgreSqlPrismaOrmService } from '../infra/prisma/prisma.service'; -import { PostgresqlPrismaOrmPsychologistRepository } from './psychologist/postgresql-prisma-orm-repository'; +import { PostgresqlPrismaOrmClinicRepository } from './clinic/postgres-prisma-orm-clinic-repository'; +import { PostgresqlPrismaOrmPsychologistRepository } from './psychologist/postgresql-prisma-orm-psychologist-repository'; @Module({ imports: [CryptographyModule], @@ -13,6 +15,10 @@ import { PostgresqlPrismaOrmPsychologistRepository } from './psychologist/postgr provide: PsychologistDatabaseRepository, useClass: PostgresqlPrismaOrmPsychologistRepository, }, + { + provide: ClinicDatabaseRepository, + useClass: PostgresqlPrismaOrmClinicRepository, + }, ], exports: [ PostgreSqlPrismaOrmService, @@ -20,6 +26,10 @@ import { PostgresqlPrismaOrmPsychologistRepository } from './psychologist/postgr provide: PsychologistDatabaseRepository, useClass: PostgresqlPrismaOrmPsychologistRepository, }, + { + provide: ClinicDatabaseRepository, + useClass: PostgresqlPrismaOrmClinicRepository, + }, ], }) export class DatabaseRepositoriesModule {} From 9cdff17ae465b422cceb7341b6f1e7c0a3fe553b Mon Sep 17 00:00:00 2001 From: Luana Date: Thu, 26 Oct 2023 21:46:38 -0300 Subject: [PATCH 07/32] feat(CC-81): add create clinic e2e test and refactor folder organization --- .../create-clinic/create-clinic.e2e-spec.ts | 77 +++++-------------- .../create-psychologist.e2e-spec.ts | 6 +- 2 files changed, 24 insertions(+), 59 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts index 0cad330..f860d16 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -3,9 +3,12 @@ import request from 'supertest'; import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; -import { makePsychologist } from '../../../../../tests/factories/make-psychologist'; -import { PostgreSqlPrismaOrmService } from '../../../../database/infra/prisma/prisma.service'; -import { ApiModule } from '../../api.module'; +import { PsychologistEntity } from '@clinicControl/core-rest-api/core/src/domains/psychologist/entities/psychologist/entity'; +import { Plan, Role } from '@clinicControl/core-rest-api/core/src/shared/interfaces/payments'; +import { makeClinic } from '@clinicControl/root/libs/core-rest-api/adapters/tests/factories/make-clinic'; +import { faker } from '@faker-js/faker'; +import { PostgreSqlPrismaOrmService } from '../../../../../database/infra/prisma/prisma.service'; +import { ApiModule } from '../../../api.module'; describe('[E2E] - Create Clinic', () => { let app: INestApplication; @@ -24,72 +27,34 @@ describe('[E2E] - Create Clinic', () => { }); it('[POST] - Should successfully create a new clinic', async () => { - const {id} = makePsychologist(); - const newClinic = makePsychologist(); - - const response = await request(app.getHttpServer()) + const newPsychologist = + new PsychologistEntity({ + name: 'Novo Usuário Teste', + email: 'novo_usuario_teste@gmail.com', + password: faker.internet.password({ length: 8 }), + plan: Plan.BASIC, + role: Role.ADMIN + }) + + await request(app.getHttpServer()) .post('/psychologist/create') .set('api-key', 'api-key') .send(newPsychologist); - expect(response.statusCode).toBe(201); - - const userOnDatabase = await prisma.psychologist.findUnique({ + const createdPsychologist = await prisma.psychologist.findUnique({ where: { email: 'novo_usuario_teste@gmail.com', }, }); - expect(userOnDatabase).toBeTruthy(); - }); - - it('[POST] - Should return an error when trying to create a new psychologist without api-key', async () => { - const newPsychologist = makePsychologist({ - email: 'novo_usuario_teste_not_created@gmail.com', - }); - - const response = await request(app.getHttpServer()) - .post('/psychologist/create') - .send(newPsychologist); - - expect(response.statusCode).toBe(401); - - const userOnDatabase = await prisma.psychologist.findUnique({ - where: { - email: 'novo_usuario_teste_not_created@gmail.com', - }, - }); - - expect(userOnDatabase).toBeFalsy(); - expect(response.body.message).toBe('API key is missing'); - }); - - it('[POST] - Should return an error when trying to create a new psychologist that already exists', async () => { - const newPsychologist = makePsychologist({ - email: 'novo_usuario_teste_new_entrie@gmail.com', - }); + const psychologistId = createdPsychologist ? createdPsychologist?.id : '' + const newClinic = makeClinic(psychologistId); const response = await request(app.getHttpServer()) - .post('/psychologist/create') + .post('/clinic/create') .set('api-key', 'api-key') - .send(newPsychologist); + .send(newClinic); expect(response.statusCode).toBe(201); - - const userOnDatabase = await prisma.psychologist.findUnique({ - where: { - email: 'novo_usuario_teste_new_entrie@gmail.com', - }, - }); - - expect(userOnDatabase).toBeTruthy(); - - const response_new_post = await request(app.getHttpServer()) - .post('/psychologist/create') - .set('api-key', 'api-key') - .send(newPsychologist); - - expect(response_new_post.statusCode).toBe(409); - expect(response_new_post.body.message).toBe('psychologist already exists'); }); }); diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.e2e-spec.ts index a925ac7..b521cc2 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.e2e-spec.ts @@ -3,9 +3,9 @@ import request from 'supertest'; import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; -import { makePsychologist } from '../../../../../tests/factories/make-psychologist'; -import { PostgreSqlPrismaOrmService } from '../../../../database/infra/prisma/prisma.service'; -import { ApiModule } from '../../api.module'; +import { makePsychologist } from '../../../../../../tests/factories/make-psychologist'; +import { PostgreSqlPrismaOrmService } from '../../../../../database/infra/prisma/prisma.service'; +import { ApiModule } from '../../../api.module'; describe('[E2E] - Create Psychologist Account', () => { let app: INestApplication; From 87e4fe2ca1d99090712d0c431d7d20df963cd549 Mon Sep 17 00:00:00 2001 From: Luana Date: Sun, 29 Oct 2023 20:32:11 -0300 Subject: [PATCH 08/32] adjust(CC-81): make adjustments in mapper and controller --- .../create-clinic/create-clinic.controller.ts | 19 +++++++------------ .../postgresql-prisma-clinic-mapper.ts | 6 +++--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts index b3e3381..452798f 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts @@ -1,11 +1,8 @@ // eslint-disable-next-line @nx/enforce-module-boundaries import { CreateClinicDto } from '@clinicControl/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto'; import { GlobalAppHttpException } from '@clinicControl/core-rest-api/core/src/shared/errors/globalAppHttpException'; -import { applicationValidateOrReject } from '@clinicControl/core-rest-api/core/src/shared/validators/validate-or-reject'; -import { Body, Controller, Post, UseGuards } from '@nestjs/common'; +import { Body, Controller, Post } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; -import { plainToInstance } from 'class-transformer'; -import { ApiKeyGuard } from '../../../guards/api-key.guard'; import { NestjsCreateClinicService } from './nestjs-create-clinic.service'; @ApiTags() @@ -18,19 +15,17 @@ export class CreateClinicController { ) {} @Post('create') - @UseGuards(ApiKeyGuard) async execute( @Body() createClinicDto: CreateClinicDto ): Promise { try { - const createClinicDtoInstance = plainToInstance( - CreateClinicDto, - createClinicDto - ); + // const createClinicDtoInstance = plainToInstance( + // CreateClinicDto, + // createClinicDto + // ); + // await applicationValidateOrReject(createClinicDtoInstance); - await applicationValidateOrReject(createClinicDtoInstance); - - await this.createClinicService.execute(createClinicDto); + await this.createClinicService.execute(createClinicDto); } catch (error: unknown) { throw new GlobalAppHttpException(error); } diff --git a/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts b/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts index e8aefc2..fc65733 100644 --- a/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts +++ b/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts @@ -22,9 +22,9 @@ export class PostgresqlPrismaClinicMapper { return { data: { ...raw, - city: 'xxxx', - name: 'yyyy', - state: 'SP', + city: raw.city, + name: raw.name, + state: raw.state, updatedAt: new Date() } From 21dcc38a922287daff4dd6909aa7395b7eb830c6 Mon Sep 17 00:00:00 2001 From: Luana Date: Sun, 29 Oct 2023 21:19:45 -0300 Subject: [PATCH 09/32] feat(CC-81): add client.http --- .../api/use-case/clinic/create-clinic/client.http | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/client.http diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/client.http new file mode 100644 index 0000000..45a9fef --- /dev/null +++ b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/client.http @@ -0,0 +1,13 @@ +@baseUrl = http://localhost:3333/core + +# @name create_clinic +POST http://localhost:3333/core/clinic/create HTTP/1.1 +Content-Type: application/json +api-key: api-key + +{ + "name": "Nova Clínica", + "psychologistId": "1684ce5b-637c-4527-9e36-0f8231e618ca", + "city": "Fake City", + "state": "Fake State", +} From 729eba70c82a0057ac0c39a784d8f0a7daaf020d Mon Sep 17 00:00:00 2001 From: luanavfg Date: Mon, 30 Oct 2023 09:14:49 -0300 Subject: [PATCH 10/32] refactor(CC-81): rename use-case folder to use-cases in adapters --- .../api/use-case/clinic/create-clinic/client.http | 13 ------------- .../create-clinic/create-clinic.controller.ts | 0 .../clinic/create-clinic/create-clinic.e2e-spec.ts | 0 .../create-clinic/nestjs-create-clinic.service.ts | 0 .../psychologist/create-psychologist/client.http | 0 .../create-psychologist.controller.ts | 0 .../create-psychologist.e2e-spec.ts | 0 .../psychologist/create-psychologist/docs.ts | 0 .../nestjs-create-psychologist.service.ts | 0 9 files changed, 13 deletions(-) delete mode 100644 libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/client.http rename libs/core-rest-api/adapters/src/controllers/api/{use-case => use-cases}/clinic/create-clinic/create-clinic.controller.ts (100%) rename libs/core-rest-api/adapters/src/controllers/api/{use-case => use-cases}/clinic/create-clinic/create-clinic.e2e-spec.ts (100%) rename libs/core-rest-api/adapters/src/controllers/api/{use-case => use-cases}/clinic/create-clinic/nestjs-create-clinic.service.ts (100%) rename libs/core-rest-api/adapters/src/controllers/api/{use-case => use-cases}/psychologist/create-psychologist/client.http (100%) rename libs/core-rest-api/adapters/src/controllers/api/{use-case => use-cases}/psychologist/create-psychologist/create-psychologist.controller.ts (100%) rename libs/core-rest-api/adapters/src/controllers/api/{use-case => use-cases}/psychologist/create-psychologist/create-psychologist.e2e-spec.ts (100%) rename libs/core-rest-api/adapters/src/controllers/api/{use-case => use-cases}/psychologist/create-psychologist/docs.ts (100%) rename libs/core-rest-api/adapters/src/controllers/api/{use-case => use-cases}/psychologist/create-psychologist/nestjs-create-psychologist.service.ts (100%) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/client.http deleted file mode 100644 index 45a9fef..0000000 --- a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/client.http +++ /dev/null @@ -1,13 +0,0 @@ -@baseUrl = http://localhost:3333/core - -# @name create_clinic -POST http://localhost:3333/core/clinic/create HTTP/1.1 -Content-Type: application/json -api-key: api-key - -{ - "name": "Nova Clínica", - "psychologistId": "1684ce5b-637c-4527-9e36-0f8231e618ca", - "city": "Fake City", - "state": "Fake State", -} diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.controller.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/create-clinic.e2e-spec.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/nestjs-create-clinic.service.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/nestjs-create-clinic.service.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/clinic/create-clinic/nestjs-create-clinic.service.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/nestjs-create-clinic.service.ts diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/client.http rename to libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.controller.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/create-psychologist.controller.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.controller.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/create-psychologist.controller.ts diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/create-psychologist.e2e-spec.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/create-psychologist.e2e-spec.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/create-psychologist.e2e-spec.ts diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/docs.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/docs.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/docs.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/docs.ts diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/nestjs-create-psychologist.service.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/nestjs-create-psychologist.service.ts similarity index 100% rename from libs/core-rest-api/adapters/src/controllers/api/use-case/psychologist/create-psychologist/nestjs-create-psychologist.service.ts rename to libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/nestjs-create-psychologist.service.ts From 07f86089b66b8bc5dfdd4be0f553f7c5413a5a25 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Mon, 30 Oct 2023 09:15:24 -0300 Subject: [PATCH 11/32] refactor(CC-81): adjust typo error in json - client.http --- .../api/use-cases/clinic/create-clinic/client.http | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http new file mode 100644 index 0000000..7355d83 --- /dev/null +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http @@ -0,0 +1,13 @@ +@baseUrl = http://localhost:3333/core + +# @name create_clinic +POST http://localhost:3333/core/clinic/create HTTP/1.1 +Content-Type: application/json +api-key: api-key + +{ + "name": "Nova Clínica", + "psychologistId": "1684ce5b-637c-4527-9e36-0f8231e618ca", + "city": "Fake City", + "state": "Fake State" +} From 90d49fc17db61f080c42f5425ef6892207f14c7f Mon Sep 17 00:00:00 2001 From: luanavfg Date: Mon, 30 Oct 2023 09:15:49 -0300 Subject: [PATCH 12/32] feat(CC-81): add clinic controller and service to api module --- .../adapters/src/controllers/api/api.module.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/api.module.ts b/libs/core-rest-api/adapters/src/controllers/api/api.module.ts index 930da1a..0df3e91 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/api.module.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/api.module.ts @@ -6,8 +6,10 @@ import { PostgreSqlPrismaOrmService } from '../../database/infra/prisma/prisma.s import { DatabaseRepositoriesModule } from '../../database/repositories/repositories.module'; import { envSchema } from '../../env/env'; import { EnvModule } from '../../env/env.module'; -import { CreatePsychologistController } from './use-case/psychologist/create-psychologist/create-psychologist.controller'; -import { NestjsCreatePsychologistService } from './use-case/psychologist/create-psychologist/nestjs-create-psychologist.service'; +import { CreateClinicController } from './use-cases/clinic/create-clinic/create-clinic.controller'; +import { NestjsCreateClinicService } from './use-cases/clinic/create-clinic/nestjs-create-clinic.service'; +import { CreatePsychologistController } from './use-cases/psychologist/create-psychologist/create-psychologist.controller'; +import { NestjsCreatePsychologistService } from './use-cases/psychologist/create-psychologist/nestjs-create-psychologist.service'; @Module({ imports: [ @@ -20,7 +22,7 @@ import { NestjsCreatePsychologistService } from './use-case/psychologist/create- isGlobal: true, }), ], - controllers: [CreatePsychologistController], - providers: [PostgreSqlPrismaOrmService, NestjsCreatePsychologistService], + controllers: [CreatePsychologistController, CreateClinicController], + providers: [PostgreSqlPrismaOrmService, NestjsCreatePsychologistService, NestjsCreateClinicService], }) export class ApiModule {} From 8bc3d927b9e8e7a6e21796ceea7ad461a70ffbd8 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Mon, 30 Oct 2023 09:31:15 -0300 Subject: [PATCH 13/32] config(CC-81): adjust client.http --- .../controllers/api/use-cases/clinic/create-clinic/client.http | 2 +- .../api/use-cases/psychologist/create-psychologist/client.http | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http index 7355d83..4fe7bd2 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http @@ -7,7 +7,7 @@ api-key: api-key { "name": "Nova Clínica", - "psychologistId": "1684ce5b-637c-4527-9e36-0f8231e618ca", + "psychologistId": "126498da-35c5-4acd-a596-80b8912c2706", "city": "Fake City", "state": "Fake State" } diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http index 0011c1e..d6e248b 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http @@ -8,7 +8,7 @@ api-key: api-key { "name": "Novo Usuario", - "email": "novo_usuario@gmail.com", + "email": "novo_usuario_12@gmail.com", "password": "01212012", "role": "PSYCHOLOGIST", "plan": "PREMIUM" From 6fb6ad4a791583cc39dab42c0aed086a152c6003 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Mon, 6 Nov 2023 09:28:22 -0300 Subject: [PATCH 14/32] feat(CC-81): add response message in clinic creation --- .../create-clinic/create-clinic.controller.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts index 452798f..91bd54d 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts @@ -5,6 +5,9 @@ import { Body, Controller, Post } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { NestjsCreateClinicService } from './nestjs-create-clinic.service'; +interface createClinicResponse { + message: string +} @ApiTags() @Controller({ path: 'clinic', @@ -17,15 +20,10 @@ export class CreateClinicController { @Post('create') async execute( @Body() createClinicDto: CreateClinicDto - ): Promise { + ): Promise{ try { - // const createClinicDtoInstance = plainToInstance( - // CreateClinicDto, - // createClinicDto - // ); - // await applicationValidateOrReject(createClinicDtoInstance); - - await this.createClinicService.execute(createClinicDto); + await this.createClinicService.execute(createClinicDto); + return {message: 'Clinic created successfully'} } catch (error: unknown) { throw new GlobalAppHttpException(error); } From 9d3eb6a737739b973cd1cddfc8d8382b7f1d5d1f Mon Sep 17 00:00:00 2001 From: luanavfg Date: Mon, 6 Nov 2023 09:29:47 -0300 Subject: [PATCH 15/32] adjust(CC-81): adjust types --- libs/core-rest-api/adapters/tests/factories/make-clinic.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/core-rest-api/adapters/tests/factories/make-clinic.ts b/libs/core-rest-api/adapters/tests/factories/make-clinic.ts index a909028..e23ba91 100644 --- a/libs/core-rest-api/adapters/tests/factories/make-clinic.ts +++ b/libs/core-rest-api/adapters/tests/factories/make-clinic.ts @@ -10,14 +10,14 @@ import { PostgresqlPrismaClinicMapper } from '../../src/database/mappers/postgre Creating a factory for the Clinic entity, which will be used to create tests for the domain's use cases */ export function makeClinic( - psychologistId: string, + psychologistId?: string, override: Partial = {} ): ClinicEntity { const newClinic = new ClinicEntity({ name: 'Nova Clinica', city: faker.location.city(), state: faker.location.state(), - psychologistId: psychologistId, + psychologistId: psychologistId ?? '', ...override, }); From ac81064b56ae61b7b663711bb0c41634da624419 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Mon, 6 Nov 2023 09:30:27 -0300 Subject: [PATCH 16/32] adjust(CC-81): adjust create clinic test --- .../clinic/create-clinic/create-clinic.e2e-spec.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts index f860d16..b2192ee 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -27,7 +27,7 @@ describe('[E2E] - Create Clinic', () => { }); it('[POST] - Should successfully create a new clinic', async () => { - const newPsychologist = + const newPsychologist = new PsychologistEntity({ name: 'Novo Usuário Teste', email: 'novo_usuario_teste@gmail.com', @@ -35,7 +35,7 @@ describe('[E2E] - Create Clinic', () => { plan: Plan.BASIC, role: Role.ADMIN }) - + await request(app.getHttpServer()) .post('/psychologist/create') .set('api-key', 'api-key') @@ -47,12 +47,10 @@ describe('[E2E] - Create Clinic', () => { }, }); - const psychologistId = createdPsychologist ? createdPsychologist?.id : '' - const newClinic = makeClinic(psychologistId); + const newClinic = makeClinic(createdPsychologist?.id); const response = await request(app.getHttpServer()) .post('/clinic/create') - .set('api-key', 'api-key') .send(newClinic); expect(response.statusCode).toBe(201); From 7d98798f32559ec96a2a224172ddfb65d6e04862 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Mon, 6 Nov 2023 09:31:08 -0300 Subject: [PATCH 17/32] chore(CC-81): delete comment --- .../clinic/postgres-prisma-orm-clinic-repository.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts b/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts index c500597..c8d6f29 100644 --- a/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts +++ b/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts @@ -28,7 +28,7 @@ export class PostgresqlPrismaOrmClinicRepository const toPrismaEntity = PostgresqlPrismaClinicMapper.toPrismaCreate({ ...clinic, - + }); const newClinic = await this.postgreSqlPrismaOrmService['clinic'].create( @@ -40,7 +40,7 @@ export class PostgresqlPrismaOrmClinicRepository async findClinicByName(name: string): Promise { - const clinic = await this.postgreSqlPrismaOrmService['clinic'].findFirst( //-> Method find first, keep it? + const clinic = await this.postgreSqlPrismaOrmService['clinic'].findFirst( { where: { name: name, From 9004d17763d00268d19f0fc124045a2c07e289e8 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Wed, 8 Nov 2023 08:20:56 -0300 Subject: [PATCH 18/32] refactor(CC-81): remove hardcoded clinic name --- libs/core-rest-api/adapters/tests/factories/make-clinic.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libs/core-rest-api/adapters/tests/factories/make-clinic.ts b/libs/core-rest-api/adapters/tests/factories/make-clinic.ts index e23ba91..4247ed4 100644 --- a/libs/core-rest-api/adapters/tests/factories/make-clinic.ts +++ b/libs/core-rest-api/adapters/tests/factories/make-clinic.ts @@ -14,13 +14,12 @@ export function makeClinic( override: Partial = {} ): ClinicEntity { const newClinic = new ClinicEntity({ - name: 'Nova Clinica', + name: faker.word.noun(), city: faker.location.city(), state: faker.location.state(), psychologistId: psychologistId ?? '', ...override, }); - return newClinic; } From acebf955d36e5855f9463d04ed4e8580a7443518 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Wed, 8 Nov 2023 08:21:48 -0300 Subject: [PATCH 19/32] refactor(CC-81): change psychologist creation in clinic test to makePsychologist() --- .../clinic/create-clinic/create-clinic.e2e-spec.ts | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts index b2192ee..c5af08d 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -3,10 +3,8 @@ import request from 'supertest'; import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; -import { PsychologistEntity } from '@clinicControl/core-rest-api/core/src/domains/psychologist/entities/psychologist/entity'; -import { Plan, Role } from '@clinicControl/core-rest-api/core/src/shared/interfaces/payments'; import { makeClinic } from '@clinicControl/root/libs/core-rest-api/adapters/tests/factories/make-clinic'; -import { faker } from '@faker-js/faker'; +import { makePsychologist } from '@clinicControl/root/libs/core-rest-api/adapters/tests/factories/make-psychologist'; import { PostgreSqlPrismaOrmService } from '../../../../../database/infra/prisma/prisma.service'; import { ApiModule } from '../../../api.module'; @@ -27,14 +25,7 @@ describe('[E2E] - Create Clinic', () => { }); it('[POST] - Should successfully create a new clinic', async () => { - const newPsychologist = - new PsychologistEntity({ - name: 'Novo Usuário Teste', - email: 'novo_usuario_teste@gmail.com', - password: faker.internet.password({ length: 8 }), - plan: Plan.BASIC, - role: Role.ADMIN - }) + const newPsychologist = makePsychologist(); await request(app.getHttpServer()) .post('/psychologist/create') From 734730777eae802bb7ebe31f0c5f790ad2f444bf Mon Sep 17 00:00:00 2001 From: luanavfg Date: Thu, 9 Nov 2023 08:46:58 -0300 Subject: [PATCH 20/32] adjust(CC-81): adjust dto attributes --- .../core/src/domains/clinic/entities/clinic/dto.ts | 2 +- .../use-cases/create-clinic/create-clinic-dto.ts | 10 +--------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/libs/core-rest-api/core/src/domains/clinic/entities/clinic/dto.ts b/libs/core-rest-api/core/src/domains/clinic/entities/clinic/dto.ts index fcc2f4f..5618d9e 100644 --- a/libs/core-rest-api/core/src/domains/clinic/entities/clinic/dto.ts +++ b/libs/core-rest-api/core/src/domains/clinic/entities/clinic/dto.ts @@ -22,7 +22,7 @@ export class ClinicDto { @IsOptional() @IsDate() - createdAt?: Date; + createdAt!: Date; @IsOptional() @IsDate() diff --git a/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto.ts b/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto.ts index 6551ccc..a9d3df0 100644 --- a/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto.ts +++ b/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic-dto.ts @@ -1,4 +1,4 @@ -import { IsDate, IsOptional, IsString } from 'class-validator'; +import { IsOptional, IsString } from 'class-validator'; export class CreateClinicDto { @IsString() @@ -16,12 +16,4 @@ export class CreateClinicDto { @IsString() state!: string; - - @IsOptional() - @IsDate() - createdAt?: Date; - - @IsOptional() - @IsDate() - updatedAt?: Date | null; } From b94ec7566efbafab82c388d49489260bba864dc8 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Thu, 9 Nov 2023 09:39:58 -0300 Subject: [PATCH 21/32] chore(CC-81): update client.http for client and psychologist --- .../clinic/create-clinic/client.http | 2 +- .../create-psychologist/client.http | 35 ------------------- .../psychologist/psychologist-client.http | 2 +- 3 files changed, 2 insertions(+), 37 deletions(-) delete mode 100644 libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http index 4fe7bd2..330844d 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http @@ -7,7 +7,7 @@ api-key: api-key { "name": "Nova Clínica", - "psychologistId": "126498da-35c5-4acd-a596-80b8912c2706", + "psychologistId": "2b99214d-6642-4aaf-a9c6-e54e53ad52d0", "city": "Fake City", "state": "Fake State" } diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http deleted file mode 100644 index 2636005..0000000 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/create-psychologist/client.http +++ /dev/null @@ -1,35 +0,0 @@ -@baseUrl = http://localhost:3333/core -@authToken = {{loginauthenticate.response.body.access_token}} - -### Create New User - -# @name create_psychologist -POST http://localhost:3333/core/psychologist/create HTTP/1.1 -Content-Type: application/json -api-key: api-key - -{ - "name": "Novo Usuario", - "email": "novo_usuario_12@gmail.com", - "password": "01212012", - "role": "PSYCHOLOGIST", - "plan": "PREMIUM" -} - -### Authenticate User - -# @name loginauthenticate -POST http://localhost:3333/core/psychologist/login HTTP/1.1 -Content-Type: application/json - -{ - "email": "novo_usuario@gmail.com", - "password": "01212012" -} - -### - -# @name me -GET http://localhost:3333/core/psychologist/me HTTP/1.1 -Content-Type: application/json -Authorization: Bearer {{authToken}} diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/psychologist-client.http b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/psychologist-client.http index 2636005..ef2ffe0 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/psychologist-client.http +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/psychologist-client.http @@ -10,7 +10,7 @@ api-key: api-key { "name": "Novo Usuario", - "email": "novo_usuario_12@gmail.com", + "email": "novo_usuario_23@gmail.com", "password": "01212012", "role": "PSYCHOLOGIST", "plan": "PREMIUM" From 91a76d6e936fb98a5704b8116f824b273e46b3a1 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Thu, 9 Nov 2023 09:40:25 -0300 Subject: [PATCH 22/32] adjust(CC-81): adjust error name --- libs/core-rest-api/core/src/shared/errors/error-messages.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core-rest-api/core/src/shared/errors/error-messages.ts b/libs/core-rest-api/core/src/shared/errors/error-messages.ts index 29fb23d..983e62f 100644 --- a/libs/core-rest-api/core/src/shared/errors/error-messages.ts +++ b/libs/core-rest-api/core/src/shared/errors/error-messages.ts @@ -7,7 +7,7 @@ export const PSYCHOLOGIST_ERROR_MESSAGES = { }; export const CLINIC_ERROR_MESSAGES = { - CONFLICTING_NAME: 'clinic already exists', + CONFLICTING_CREDENTIALS: 'clinic already exists', CLINIC_NOT_FOUND: 'clinic not found', }; From 9ecb2240fb24cc737623cabd71b54154b5055644 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Thu, 9 Nov 2023 09:41:11 -0300 Subject: [PATCH 23/32] adjust(CC-81): adjust error name --- .../authenticate-psychologist.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core-rest-api/core/src/domains/psychologist/use-cases/authenticate-psychologist/authenticate-psychologist.service.ts b/libs/core-rest-api/core/src/domains/psychologist/use-cases/authenticate-psychologist/authenticate-psychologist.service.ts index 35273bc..cd73c2c 100644 --- a/libs/core-rest-api/core/src/domains/psychologist/use-cases/authenticate-psychologist/authenticate-psychologist.service.ts +++ b/libs/core-rest-api/core/src/domains/psychologist/use-cases/authenticate-psychologist/authenticate-psychologist.service.ts @@ -1,6 +1,6 @@ -import { PSYCHOLOGIST_ERROR_MESSAGES } from '@clinicControl/core-rest-api/core/src/shared/errors/error-messages'; import { UnauthorizedException } from '@nestjs/common'; import { BcryptHasherService } from '../../../../shared/cryptography/use-cases/bcrypt-hasher.service'; +import { PSYCHOLOGIST_ERROR_MESSAGES } from '../../../../shared/errors/error-messages'; import { PsychologistEntity } from '../../entities/psychologist/entity'; import { PsychologistDatabaseRepository } from '../../repositories/database-repository'; import { AuthenticatePsychologistDto } from './authenticate-psychologist-dto'; From 241f902bead0aa177fb35e9fe46b291684cfcf77 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Thu, 9 Nov 2023 09:41:23 -0300 Subject: [PATCH 24/32] adjust(CC-81): adjust conflicts --- .../adapters/src/controllers/api/api.module.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/api.module.ts b/libs/core-rest-api/adapters/src/controllers/api/api.module.ts index 8ec5be5..4aa4d6c 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/api.module.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/api.module.ts @@ -11,10 +11,9 @@ import { EnvModule } from '../../env/env.module'; import { CreateClinicController } from './use-cases/clinic/create-clinic/create-clinic.controller'; import { NestjsCreateClinicService } from './use-cases/clinic/create-clinic/nestjs-create-clinic.service'; import { AuthenticatePsychologistController } from './use-cases/psychologist/authenticate-psychologist/authenticate-psychologist.controller'; -import { CreatePsychologistController } from './use-cases/psychologists/psychologist/create-psychologist/create-psychologist.controller'; - import { NestjsAuthenticatePsychologistService } from './use-cases/psychologist/authenticate-psychologist/nestjs-authenticate-psychologist.service'; -import { NestjsCreatePsychologistService } from './use-cases/psychologists/psychologist/create-psychologist/nestjs-create-psychologist.service'; +import { CreatePsychologistController } from './use-cases/psychologist/create-psychologist/create-psychologist.controller'; +import { NestjsCreatePsychologistService } from './use-cases/psychologist/create-psychologist/nestjs-create-psychologist.service'; @Module({ imports: [ @@ -33,6 +32,6 @@ import { NestjsCreatePsychologistService } from './use-cases/psychologists/psych PostgreSqlPrismaOrmService, NestjsCreatePsychologistService, NestjsAuthenticatePsychologistService, - , NestjsCreateClinicService], + NestjsCreateClinicService], }) export class ApiModule {} From 29060412ac9ce3db6e9205a6c851cc019f93d29c Mon Sep 17 00:00:00 2001 From: luanavfg Date: Thu, 9 Nov 2023 09:41:38 -0300 Subject: [PATCH 25/32] adjust(CC-81): adjust imports --- .../authenticate-psychologist.e2e-spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/authenticate-psychologist/authenticate-psychologist.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/authenticate-psychologist/authenticate-psychologist.e2e-spec.ts index 3c6cbb2..5cc20ff 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/authenticate-psychologist/authenticate-psychologist.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/authenticate-psychologist/authenticate-psychologist.e2e-spec.ts @@ -6,8 +6,8 @@ import { Test } from '@nestjs/testing'; import { fakerPT_BR as faker } from '@faker-js/faker'; import { hash } from 'bcryptjs'; -import { DatabaseRepositoriesModule } from '@clinicControl/core-rest-api/adapters/src/database/repositories/repositories.module'; import { PsychologistFactory } from '../../../../../../tests/factories/make-psychologist'; +import { DatabaseRepositoriesModule } from '../../../../../database/repositories/repositories.module'; import { ApiModule } from '../../../api.module'; describe('[E2E] - Authenticate Psychologist', () => { From 7bd1a1003db753ac0cb6c1d950df315df7f029e3 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Thu, 9 Nov 2023 09:42:59 -0300 Subject: [PATCH 26/32] adjust(CC-81): add psychologistId in clinic use-cases to verify if it exists --- .../repositories/database-in-memory-repository.ts | 12 ++++++------ .../clinic/repositories/database-repository.ts | 4 ++-- .../use-cases/create-clinic/create-clinic.service.ts | 7 ++++--- .../use-cases/create-clinic/create-clinic.spec.ts | 2 +- .../use-cases/delete-clinic/delete-clinic.service.ts | 8 ++++---- .../use-cases/delete-clinic/delete-clinic.spec.ts | 4 ++-- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/libs/core-rest-api/core/src/domains/clinic/repositories/database-in-memory-repository.ts b/libs/core-rest-api/core/src/domains/clinic/repositories/database-in-memory-repository.ts index 366305e..9ea7881 100644 --- a/libs/core-rest-api/core/src/domains/clinic/repositories/database-in-memory-repository.ts +++ b/libs/core-rest-api/core/src/domains/clinic/repositories/database-in-memory-repository.ts @@ -11,10 +11,10 @@ export class InMemoryClinicDatabaseRepository private clinics: ClinicEntity[] = []; async createClinic(clinic: CreateClinicDto): Promise { - const isClinicExists = await this.findClinicByName(clinic.name); + const isClinicExists = await this.findClinicByNameAndPsychologistId(clinic.name, clinic.psychologistId); if (isClinicExists) { - throw new ConflictException(CLINIC_ERROR_MESSAGES['CONFLICTING_NAME']); + throw new ConflictException(CLINIC_ERROR_MESSAGES['CONFLICTING_CREDENTIALS']); } const newClinic = new ClinicEntity(clinic); @@ -28,8 +28,8 @@ export class InMemoryClinicDatabaseRepository return this.clinics; } - async findClinicByName(name: string): Promise { - return this.clinics.find((clinic) => clinic.name === name) ?? null; + async findClinicByNameAndPsychologistId(name: string, psychologistId: string): Promise { + return this.clinics.find((clinic) => clinic.name === name && clinic.psychologistId === psychologistId) ?? null; } async findClinicById(id: string): Promise { @@ -55,8 +55,8 @@ export class InMemoryClinicDatabaseRepository this.clinics[clinicIndex] = updatedClinic; } - async deleteClinic(name: string): Promise { - const isClinicExists = await this.findClinicByName(name); + async deleteClinic(name: string, psychologistId: string): Promise { + const isClinicExists = await this.findClinicByNameAndPsychologistId(name, psychologistId); if (!isClinicExists) { throw new ConflictException(CLINIC_ERROR_MESSAGES['CLINIC_NOT_FOUND']); diff --git a/libs/core-rest-api/core/src/domains/clinic/repositories/database-repository.ts b/libs/core-rest-api/core/src/domains/clinic/repositories/database-repository.ts index f1ef394..03ba994 100644 --- a/libs/core-rest-api/core/src/domains/clinic/repositories/database-repository.ts +++ b/libs/core-rest-api/core/src/domains/clinic/repositories/database-repository.ts @@ -5,8 +5,8 @@ import { UpdateClinicDto } from '../use-cases/update-clinic/update-clinic-dto'; export abstract class ClinicDatabaseRepository { abstract createClinic(clinic: CreateClinicDto): Promise; abstract getClinics(): Promise; - abstract findClinicByName(name: string): Promise; + abstract findClinicByNameAndPsychologistId(name: string, psychologistId: string): Promise; abstract findClinicById(id: string): Promise; abstract updateClinic(clinic: UpdateClinicDto): Promise; - abstract deleteClinic(name: string): Promise; + abstract deleteClinic(name: string, psychologistId: string): Promise; } diff --git a/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic.service.ts b/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic.service.ts index dceb386..a355c68 100644 --- a/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic.service.ts +++ b/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic.service.ts @@ -21,12 +21,13 @@ export class CreateClinicService { await applicationValidateOrReject(createClinicDtoInstance); - const isClinicExist = await this.clinicDatabaseRepository.findClinicByName( - createClinicDto.name + const isClinicExist = await this.clinicDatabaseRepository.findClinicByNameAndPsychologistId( + createClinicDto.name, + createClinicDto.psychologistId, ); if (isClinicExist) { - throw new ConflictException(CLINIC_ERROR_MESSAGES['CONFLICTING_NAME']); + throw new ConflictException(CLINIC_ERROR_MESSAGES['CONFLICTING_CREDENTIALS']); } // Create diff --git a/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic.spec.ts b/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic.spec.ts index 2db40dc..ea201ff 100644 --- a/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic.spec.ts +++ b/libs/core-rest-api/core/src/domains/clinic/use-cases/create-clinic/create-clinic.spec.ts @@ -25,7 +25,7 @@ describe('[clinic] Create Clinic Service', () => { it('should create a new clinic', async () => { const createClinic = await service.execute(fakeClinic); - const clinic = await databaseRepository.findClinicByName(createClinic.name); + const clinic = await databaseRepository.findClinicByNameAndPsychologistId(createClinic.name, createClinic.psychologistId); expect(clinic?.name).toEqual(createClinic.name); expect(createClinic.name).toEqual(fakeClinic.name); diff --git a/libs/core-rest-api/core/src/domains/clinic/use-cases/delete-clinic/delete-clinic.service.ts b/libs/core-rest-api/core/src/domains/clinic/use-cases/delete-clinic/delete-clinic.service.ts index 7f662d3..f5b3cfd 100644 --- a/libs/core-rest-api/core/src/domains/clinic/use-cases/delete-clinic/delete-clinic.service.ts +++ b/libs/core-rest-api/core/src/domains/clinic/use-cases/delete-clinic/delete-clinic.service.ts @@ -5,10 +5,10 @@ import { ClinicDatabaseRepository } from '../../repositories/database-repository export class DeleteClinicService { constructor(private clinicDatabaseRepository: ClinicDatabaseRepository) {} - async execute(name: string): Promise { + async execute(name: string, psychologistId: string): Promise { // Validate if clinic exists in db - const isClinicExists = await this.clinicDatabaseRepository.findClinicByName( - name + const isClinicExists = await this.clinicDatabaseRepository.findClinicByNameAndPsychologistId( + name, psychologistId ); if (!isClinicExists) { @@ -16,6 +16,6 @@ export class DeleteClinicService { } // Delete clinic - await this.clinicDatabaseRepository.deleteClinic(name); + await this.clinicDatabaseRepository.deleteClinic(name, psychologistId); } } diff --git a/libs/core-rest-api/core/src/domains/clinic/use-cases/delete-clinic/delete-clinic.spec.ts b/libs/core-rest-api/core/src/domains/clinic/use-cases/delete-clinic/delete-clinic.spec.ts index 32171d4..ff16429 100644 --- a/libs/core-rest-api/core/src/domains/clinic/use-cases/delete-clinic/delete-clinic.spec.ts +++ b/libs/core-rest-api/core/src/domains/clinic/use-cases/delete-clinic/delete-clinic.spec.ts @@ -25,7 +25,7 @@ describe('[clinic] Delete Clinic Service', () => { it('should delete a new clinic', async () => { const createClinic = await databaseRepository.createClinic(fakeClinic); - await service.execute(createClinic.name); + await service.execute(createClinic.name, createClinic.psychologistId); const getClinics = await databaseRepository.getClinics(); @@ -33,7 +33,7 @@ describe('[clinic] Delete Clinic Service', () => { }); it('should throw conflict exception if clinic do not exists', async () => { - await expect(service.execute(fakeClinic.name)).rejects.toThrow( + await expect(service.execute(fakeClinic.name, fakeClinic.psychologistId)).rejects.toThrow( ConflictException ); }); From 20179d25530c0cc067952b1fafc3ac7244172385 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Thu, 9 Nov 2023 09:43:24 -0300 Subject: [PATCH 27/32] adjust(CC-81): add psychologistId in clinic postgres prisma repository to verify if it exists --- .../postgres-prisma-orm-clinic-repository.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts b/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts index c8d6f29..78123aa 100644 --- a/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts +++ b/libs/core-rest-api/adapters/src/database/repositories/clinic/postgres-prisma-orm-clinic-repository.ts @@ -18,11 +18,11 @@ export class PostgresqlPrismaOrmClinicRepository async createClinic( clinic: CreateClinicDto ): Promise { - const isClinicExists = await this.findClinicByName(clinic.name); + const isClinicExists = await this.findClinicByNameAndPsychologistId(clinic.name, clinic.psychologistId); if (isClinicExists) { throw new ConflictException( - CLINIC_ERROR_MESSAGES['CONFLICTING_NAME'] + CLINIC_ERROR_MESSAGES['CONFLICTING_CREDENTIALS'] ); } @@ -38,12 +38,13 @@ export class PostgresqlPrismaOrmClinicRepository return PostgresqlPrismaClinicMapper.toDomain(newClinic); } - async findClinicByName(name: string): Promise { + async findClinicByNameAndPsychologistId(name: string, psychologistId: string): Promise { const clinic = await this.postgreSqlPrismaOrmService['clinic'].findFirst( { where: { - name: name, + name, + psychologistId: psychologistId }, } ); @@ -83,7 +84,7 @@ export class PostgresqlPrismaOrmClinicRepository const oldClinic = await this.findClinicById(newClinic.id); if (!oldClinic) { - throw new ConflictException(CLINIC_ERROR_MESSAGES['CONFLICTING_NAME']); + throw new ConflictException(CLINIC_ERROR_MESSAGES['CONFLICTING_CREDENTIALS']); } const toPrismaEntity = @@ -92,8 +93,8 @@ export class PostgresqlPrismaOrmClinicRepository await this.postgreSqlPrismaOrmService['clinic'].update(toPrismaEntity); } - async deleteClinic(name: string): Promise { - const isClinicExists = await this.findClinicByName(name); + async deleteClinic(name: string, psychologistId: string): Promise { + const isClinicExists = await this.findClinicByNameAndPsychologistId(name, psychologistId); if (!isClinicExists) { throw new ConflictException(CLINIC_ERROR_MESSAGES['CLINIC_NOT_FOUND']); From e9d5b7d951db1cbdd4763289633ad82b8b92c975 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Fri, 10 Nov 2023 16:43:12 -0300 Subject: [PATCH 28/32] feat(CC-81): add configs for auth --- .../use-cases/clinic/create-clinic/client.http | 15 ++++++++++++++- .../create-clinic/create-clinic.e2e-spec.ts | 13 ++++++++++++- .../psychologist/psychologist-client.http | 4 ++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http index 330844d..dac6373 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/client.http @@ -1,9 +1,22 @@ @baseUrl = http://localhost:3333/core +@authToken = {{loginauthenticate.response.body.access_token}} + +# @name loginauthenticate +POST http://localhost:3333/core/psychologist/login HTTP/1.1 +Content-Type: application/json + +{ + "email": "novo_usuario_33@gmail.com", + "password": "01212012" +} + +#### # @name create_clinic + POST http://localhost:3333/core/clinic/create HTTP/1.1 Content-Type: application/json -api-key: api-key +Authorization: Bearer {{authToken}} { "name": "Nova Clínica", diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts index c5af08d..2373289 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -5,6 +5,8 @@ import { Test } from '@nestjs/testing'; import { makeClinic } from '@clinicControl/root/libs/core-rest-api/adapters/tests/factories/make-clinic'; import { makePsychologist } from '@clinicControl/root/libs/core-rest-api/adapters/tests/factories/make-psychologist'; +import { faker } from '@faker-js/faker'; +import { hash } from 'bcryptjs'; import { PostgreSqlPrismaOrmService } from '../../../../../database/infra/prisma/prisma.service'; import { ApiModule } from '../../../api.module'; @@ -25,7 +27,8 @@ describe('[E2E] - Create Clinic', () => { }); it('[POST] - Should successfully create a new clinic', async () => { - const newPsychologist = makePsychologist(); + const password = faker.internet.password({ length: 8 }); + const newPsychologist = makePsychologist({password: await hash(password, 8)}); await request(app.getHttpServer()) .post('/psychologist/create') @@ -38,6 +41,14 @@ describe('[E2E] - Create Clinic', () => { }, }); + const login = await request(app.getHttpServer()) + .post('/psychologist/login') + .send({ + email: newPsychologist.email, + password, + }); + + console.log('login', login.body.access_token) const newClinic = makeClinic(createdPsychologist?.id); const response = await request(app.getHttpServer()) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/psychologist-client.http b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/psychologist-client.http index ef2ffe0..a38935b 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/psychologist-client.http +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/psychologist/psychologist-client.http @@ -10,7 +10,7 @@ api-key: api-key { "name": "Novo Usuario", - "email": "novo_usuario_23@gmail.com", + "email": "novo_usuario_33@gmail.com", "password": "01212012", "role": "PSYCHOLOGIST", "plan": "PREMIUM" @@ -23,7 +23,7 @@ POST http://localhost:3333/core/psychologist/login HTTP/1.1 Content-Type: application/json { - "email": "novo_usuario@gmail.com", + "email": "novo_usuario_33@gmail.com", "password": "01212012" } From 0670b961156e79e982ef6ec21c936081221409d7 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Thu, 16 Nov 2023 09:41:11 -0300 Subject: [PATCH 29/32] feat(CC-81): adjust e2e create clinic test --- .../create-clinic/create-clinic.e2e-spec.ts | 114 +++++++++++++----- 1 file changed, 87 insertions(+), 27 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts index 2373289..8d417b1 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -3,58 +3,118 @@ import request from 'supertest'; import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; +import { DatabaseRepositoriesModule } from '@clinicControl/core-rest-api/adapters/src/database/repositories/repositories.module'; +import { ClinicEntity } from '@clinicControl/core-rest-api/core/src/domains/clinic/entities/clinic/entity'; +import { PsychologistEntity } from '@clinicControl/core-rest-api/core/src/domains/psychologist/entities/psychologist/entity'; +import { BcryptHasherService } from '@clinicControl/core-rest-api/core/src/shared/cryptography/use-cases/bcrypt-hasher.service'; import { makeClinic } from '@clinicControl/root/libs/core-rest-api/adapters/tests/factories/make-clinic'; -import { makePsychologist } from '@clinicControl/root/libs/core-rest-api/adapters/tests/factories/make-psychologist'; +import { PsychologistFactory } from '@clinicControl/root/libs/core-rest-api/adapters/tests/factories/make-psychologist'; import { faker } from '@faker-js/faker'; -import { hash } from 'bcryptjs'; +import { JwtService } from '@nestjs/jwt'; import { PostgreSqlPrismaOrmService } from '../../../../../database/infra/prisma/prisma.service'; import { ApiModule } from '../../../api.module'; describe('[E2E] - Create Clinic', () => { - let app: INestApplication; let prisma: PostgreSqlPrismaOrmService; + let app: INestApplication; + let psychologistFactory: PsychologistFactory; + let jwt: JwtService; + let psychologistId: string; + let access_token: string; + let invalid_access_token: string; + let psychologist: PsychologistEntity; + let password: string; + let clinic: ClinicEntity beforeAll(async () => { const moduleRef = await Test.createTestingModule({ - imports: [ApiModule], + imports: [ApiModule, DatabaseRepositoriesModule], + providers: [PsychologistFactory] }).compile(); app = moduleRef.createNestApplication(); prisma = moduleRef.get(PostgreSqlPrismaOrmService); + psychologistFactory = moduleRef.get(PsychologistFactory); + jwt = moduleRef.get(JwtService); await app.init(); + + // hashing a static known password to use in tests + const bcrypt = new BcryptHasherService(); + password = faker.internet.password({ length: 8 }); + const hashedPassword = await bcrypt.hash(password); + + // creating a psychologist account to use in tests + psychologist = await psychologistFactory.makePrismaPsychologist({ + password: hashedPassword, + }); + + psychologistId = psychologist.id; + access_token = jwt.sign({ + id: psychologistId, + name: psychologist.name, + email: psychologist.email, + }); + + invalid_access_token = jwt.sign({ psychologistId }); + clinic = makeClinic(psychologistId); }); it('[POST] - Should successfully create a new clinic', async () => { - const password = faker.internet.password({ length: 8 }); - const newPsychologist = makePsychologist({password: await hash(password, 8)}); - - await request(app.getHttpServer()) - .post('/psychologist/create') - .set('api-key', 'api-key') - .send(newPsychologist); - - const createdPsychologist = await prisma.psychologist.findUnique({ - where: { - email: 'novo_usuario_teste@gmail.com', - }, - }); + const createdClinicResponse = await request(app.getHttpServer()) + .post('/clinic/create') + .set('Authorization', `Bearer ${access_token}`) + .send(clinic); - const login = await request(app.getHttpServer()) - .post('/psychologist/login') - .send({ - email: newPsychologist.email, - password, - }); + expect(createdClinicResponse.statusCode).toBe(201); + expect(createdClinicResponse.body.message).toBe('Clinic created successfully'); + }); - console.log('login', login.body.access_token) - const newClinic = makeClinic(createdPsychologist?.id); + it('[POST] - Should unsuccessfully try to create a new clinic without token', async () => { + const newClinic = makeClinic(psychologistId); - const response = await request(app.getHttpServer()) + const createdClinicResponse = await request(app.getHttpServer()) .post('/clinic/create') .send(newClinic); - expect(response.statusCode).toBe(201); + expect(createdClinicResponse.statusCode).toBe(401); + expect(createdClinicResponse.body.message).toBe('Invalid JWT token'); + }); + + it('[POST] - Should unsuccessfully try to create a new clinic without body request', async () => { + const newClinic = makeClinic(psychologistId); + + const createdClinicResponse = await request(app.getHttpServer()) + .post('/clinic/create') + .set('Authorization', `Bearer ${access_token}`) + // .send(newClinic); + + console.log('createdClinicResponse', createdClinicResponse.text) + expect(createdClinicResponse.statusCode).toBe(400); + + expect(createdClinicResponse.body.message).toBe('Validation failed'); + expect(createdClinicResponse.text).toBe('{"message":"Validation failed","causes":[{"property":"name","constraints":{"isString":"name must be a string"}},{"property":"psychologistId","constraints":{"isString":"psychologistId must be a string"}},{"property":"city","constraints":{"isString":"city must be a string"}},{"property":"state","constraints":{"isString":"state must be a string"}}]}'); + }); + + it('[POST] - Should unsuccessfully try to create a clinic that already exists', async () => { + const createdClinicResponse = await request(app.getHttpServer()) + .post('/clinic/create') + .set('Authorization', `Bearer ${access_token}`) + .send(clinic); + + expect(createdClinicResponse.statusCode).toBe(409); + expect(createdClinicResponse.body.message).toBe('clinic already exists'); + }); + + it('[POST] - Should unsuccessfully try to create a clinic with an invalid psychologist Id', async () => { + clinic = makeClinic('invalid-psychologist-id'); + const createdClinicResponse = await request(app.getHttpServer()) + .post('/clinic/create') + .set('Authorization', `Bearer ${access_token}`) + .send(clinic); + + expect(createdClinicResponse.statusCode).toBe(500); + expect(createdClinicResponse.body.message).toBe('Internal server error'); }); }); From b94d41750e9b2036e7c4143fc5ac881119838430 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Fri, 17 Nov 2023 08:06:31 -0300 Subject: [PATCH 30/32] fix(CC-81): adjust errors and delete logs --- .../use-cases/clinic/create-clinic/create-clinic.controller.ts | 2 +- .../use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts | 2 -- .../src/database/mappers/postgresql-prisma-clinic-mapper.ts | 3 --- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts index 91bd54d..ace442c 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.controller.ts @@ -8,7 +8,7 @@ import { NestjsCreateClinicService } from './nestjs-create-clinic.service'; interface createClinicResponse { message: string } -@ApiTags() +@ApiTags('Clinic') @Controller({ path: 'clinic', }) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts index 8d417b1..db35e36 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -88,9 +88,7 @@ describe('[E2E] - Create Clinic', () => { const createdClinicResponse = await request(app.getHttpServer()) .post('/clinic/create') .set('Authorization', `Bearer ${access_token}`) - // .send(newClinic); - console.log('createdClinicResponse', createdClinicResponse.text) expect(createdClinicResponse.statusCode).toBe(400); expect(createdClinicResponse.body.message).toBe('Validation failed'); diff --git a/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts b/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts index fc65733..0e12b82 100644 --- a/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts +++ b/libs/core-rest-api/adapters/src/database/mappers/postgresql-prisma-clinic-mapper.ts @@ -25,10 +25,7 @@ export class PostgresqlPrismaClinicMapper { city: raw.city, name: raw.name, state: raw.state, - updatedAt: new Date() } - - }; } From 230a8352af51f8d30dca624222f93d10d1c8c594 Mon Sep 17 00:00:00 2001 From: luanavfg Date: Fri, 17 Nov 2023 08:20:35 -0300 Subject: [PATCH 31/32] test(CC-81): adjust create clinic e2e test --- .../use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts index db35e36..84b4ca7 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -113,6 +113,6 @@ describe('[E2E] - Create Clinic', () => { .send(clinic); expect(createdClinicResponse.statusCode).toBe(500); - expect(createdClinicResponse.body.message).toBe('Internal server error'); + expect(createdClinicResponse.body.message).contain('An error occurred while processing your request'); }); }); From 484feca65d6c55577c1a033504b859f5d8bc687e Mon Sep 17 00:00:00 2001 From: luanavfg Date: Fri, 17 Nov 2023 08:48:26 -0300 Subject: [PATCH 32/32] adjust(CC-81): adjust error treatment and test --- .../use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts | 4 ++-- .../core/src/shared/errors/globalAppHttpException.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts index 84b4ca7..bc3ea85 100644 --- a/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts +++ b/libs/core-rest-api/adapters/src/controllers/api/use-cases/clinic/create-clinic/create-clinic.e2e-spec.ts @@ -112,7 +112,7 @@ describe('[E2E] - Create Clinic', () => { .set('Authorization', `Bearer ${access_token}`) .send(clinic); - expect(createdClinicResponse.statusCode).toBe(500); - expect(createdClinicResponse.body.message).contain('An error occurred while processing your request'); + expect(createdClinicResponse.statusCode).toBe(400); + expect(createdClinicResponse.body.message).contain("Invalid `this.postgreSqlPrismaOrmService['clinic'].create()`"); }); }); diff --git a/libs/core-rest-api/core/src/shared/errors/globalAppHttpException.ts b/libs/core-rest-api/core/src/shared/errors/globalAppHttpException.ts index f6a1538..db0dde1 100644 --- a/libs/core-rest-api/core/src/shared/errors/globalAppHttpException.ts +++ b/libs/core-rest-api/core/src/shared/errors/globalAppHttpException.ts @@ -54,7 +54,7 @@ export class GlobalAppHttpException { exceptionMessage += ` Prisma Error Code: ${error.code}.`; } - if (error.code === 'P2002') { + if (error.code === 'P2002' || error.code === 'P2003') { // Remover linhas e espaços extras const relevantLines = error.message .split('\n')