-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from ItaloRAmaral/99-update-patient-endpoint
feat(CC-99): update patient endpoint
- Loading branch information
Showing
7 changed files
with
261 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,14 @@ Authorization: Bearer {{authToken}} | |
DELETE http://localhost:3333/core/patient/{{$dotenv PATIENT_ID}}/delete HTTP/1.1 | ||
Content-Type: application/json | ||
Authorization: Bearer {{authToken}} | ||
|
||
### Update a patient | ||
# @name update_patient | ||
PATCH http://localhost:3333/core/patient/{{$dotenv PATIENT_ID}}/update HTTP/1.1 | ||
Content-Type: application/json | ||
Authorization: Bearer {{authToken}} | ||
|
||
{ | ||
"name": "teste", | ||
"email": "[email protected]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
apps/core-rest-api/src/app/adapters/controllers/api/use-cases/patient/update-patient/docs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { OperationObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface'; | ||
|
||
const description = ` | ||
--- | ||
\`Experimental\` | ||
--- | ||
### Update an existing Patient | ||
This endpoint help you to update an existing Patient. | ||
You must at least provide one of the following body parameters | ||
`; | ||
|
||
export const patchMethodDocs: Partial<OperationObject> = { | ||
summary: 'Update an existing Patient', | ||
description, | ||
}; |
33 changes: 33 additions & 0 deletions
33
...e-rest-api/src/app/adapters/controllers/api/use-cases/patient/update-patient/input-dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { IsEnum, IsMobilePhone, IsOptional, IsString, IsUUID } from 'class-validator'; | ||
import { PaymentMethod } from '../../../../../../core/shared/interfaces/payments'; | ||
|
||
export class UpdatePatientControllerReqBodyInputDto { | ||
@IsOptional() | ||
@IsString() | ||
name?: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
email?: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
cpf?: string; | ||
|
||
@IsOptional() | ||
@IsMobilePhone('pt-BR') | ||
telephone?: string; | ||
|
||
@IsOptional() | ||
@IsEnum(PaymentMethod) | ||
paymentMethod?: PaymentMethod; | ||
|
||
@IsOptional() | ||
@IsString() | ||
clinicId?: string; | ||
} | ||
|
||
export class UpdatePatientControllerReqParamsInputDto { | ||
@IsUUID() | ||
id!: string; | ||
} |
11 changes: 11 additions & 0 deletions
11
...dapters/controllers/api/use-cases/patient/update-patient/nestjs-update-patient.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { PatientDatabaseRepository } from '../../../../../../../app/core/domains/patient/repositories/database-repository'; | ||
import { UpdatePatientService } from '../../../../../../../app/core/domains/patient/use-cases/update-patient/update-patient.service'; | ||
|
||
@Injectable() | ||
export class NestjsUpdatePatientService extends UpdatePatientService { | ||
constructor(patientDatabaseRepository: PatientDatabaseRepository) { | ||
super(patientDatabaseRepository); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...pp/adapters/controllers/api/use-cases/patient/update-patient/update-patient.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { BadRequestException, Body, Controller, Param, Patch } from '@nestjs/common'; | ||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'; | ||
import { patchMethodDocs } from './docs'; | ||
|
||
import { GlobalAppHttpException } from '../../../../../../shared/errors/globalAppHttpException'; | ||
import { | ||
UpdatePatientControllerReqBodyInputDto, | ||
UpdatePatientControllerReqParamsInputDto, | ||
} from './input-dto'; | ||
import { NestjsUpdatePatientService } from './nestjs-update-patient.service'; | ||
|
||
@ApiTags('Patient') | ||
@ApiBearerAuth() | ||
@Controller({ | ||
path: 'patient', | ||
}) | ||
export class UpdatePatientController { | ||
constructor(private updatePatientService: NestjsUpdatePatientService) {} | ||
|
||
@Patch(':id/update') | ||
@ApiOperation(patchMethodDocs) | ||
async execute( | ||
@Param() { id }: UpdatePatientControllerReqParamsInputDto, | ||
@Body() updatePsychologistDto: UpdatePatientControllerReqBodyInputDto, | ||
) { | ||
try { | ||
const isReqBodyEmpty = Object.keys(updatePsychologistDto).length === 0; | ||
|
||
if (isReqBodyEmpty) { | ||
throw new BadRequestException('Must provide at least one field to update'); | ||
} | ||
|
||
await this.updatePatientService.execute({ id, ...updatePsychologistDto }); | ||
|
||
return { message: 'Patient updated successfully' }; | ||
} catch (error: unknown) { | ||
throw new GlobalAppHttpException(error); | ||
} | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
.../app/adapters/controllers/api/use-cases/patient/update-patient/update-patient.e2e-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import request from 'supertest'; | ||
|
||
import { INestApplication } from '@nestjs/common'; | ||
|
||
import { faker } from '@faker-js/faker'; | ||
import { setupE2ETest } from '../../../../../../../../tests/utils/e2e-tests-initial-setup'; | ||
import { PatientEntity } from '../../../../../../core/domains/patient/entities/patient/entity'; | ||
|
||
describe('[E2E] - Update Psychologist Account', () => { | ||
let app: INestApplication; | ||
|
||
let patient: PatientEntity; | ||
let access_token: string; | ||
let invalid_access_token: string; | ||
|
||
beforeAll(async () => { | ||
const setup = await setupE2ETest(); | ||
app = setup.app; | ||
|
||
patient = setup.patient; | ||
|
||
access_token = setup.access_token; | ||
invalid_access_token = setup.invalid_access_token; | ||
}); | ||
|
||
it('[PATCH] - Should successfully update a patient account', async () => { | ||
const updateInfos = { | ||
name: 'New Name', | ||
email: '[email protected]', | ||
}; | ||
|
||
const response = await request(app.getHttpServer()) | ||
.patch(`/patient/${patient.id}/update`) | ||
.set('Authorization', `Bearer ${access_token}`) | ||
.send(updateInfos); | ||
|
||
expect(response.statusCode).toBe(200); | ||
expect(response.body.message).toBe('Patient updated successfully'); | ||
}); | ||
|
||
it('[PATCH] - Should return an error when trying to update a patient without access_token', async () => { | ||
const updateInfos = { | ||
name: 'New Name', | ||
}; | ||
|
||
const response = await request(app.getHttpServer()) | ||
.patch(`/patient/${patient.id}/update`) | ||
.send(updateInfos); | ||
|
||
expect(response.statusCode).toBe(401); | ||
expect(response.body.message).toBe('Invalid JWT token'); | ||
}); | ||
|
||
it('[PATCH] - Should return an error when trying to update a patient with invalid access_token', async () => { | ||
const updateInfos = { | ||
name: 'New Name', | ||
}; | ||
|
||
const response = await request(app.getHttpServer()) | ||
.patch(`/patient/${patient.id}/update`) | ||
.set('Authorization', `Bearer ${invalid_access_token}`) | ||
.send(updateInfos); | ||
|
||
expect(response.statusCode).toBe(401); | ||
expect(response.body.message).toBe('Invalid JWT token'); | ||
}); | ||
|
||
it('[PATCH] - Should return an error when trying to update a patient with invalid id', async () => { | ||
const updateInfos = { | ||
name: 'New Name', | ||
}; | ||
|
||
const response = await request(app.getHttpServer()) | ||
.patch(`/patient/invalid_id/update`) | ||
.set('Authorization', `Bearer ${access_token}`) | ||
.send(updateInfos); | ||
|
||
expect(response.statusCode).toBe(400); | ||
expect(response.body.message).toEqual(['id must be a UUID']); | ||
}); | ||
|
||
it('[PATCH] - Should return an error when trying to update a patient with non existent id', async () => { | ||
const nonExistentId = faker.string.uuid(); | ||
|
||
const updateInfos = { | ||
name: 'New Name', | ||
}; | ||
|
||
const response = await request(app.getHttpServer()) | ||
.patch(`/patient/${nonExistentId}/update`) | ||
.set('Authorization', `Bearer ${access_token}`) | ||
.send(updateInfos); | ||
|
||
expect(response.statusCode).toBe(404); | ||
expect(response.body.message).toBe('patient not found'); | ||
}); | ||
|
||
it('[PATCH] - Should return an error when trying to update a patient with empty request body', async () => { | ||
const updateInfos = {}; | ||
|
||
const response = await request(app.getHttpServer()) | ||
.patch(`/psychologist/${patient.id}/update`) | ||
.set('Authorization', `Bearer ${access_token}`) | ||
.send(updateInfos); | ||
|
||
expect(response.statusCode).toBe(400); | ||
expect(response.body.message).toBe('Must provide at least one field to update'); | ||
}); | ||
|
||
it('[PATCH] - Should return an error when trying to update a patient with an invalid body type params', async () => { | ||
const updateInfos = { | ||
name: 123, | ||
email: 123, | ||
}; | ||
|
||
const response = await request(app.getHttpServer()) | ||
.patch(`/psychologist/${patient.id}/update`) | ||
.set('Authorization', `Bearer ${access_token}`) | ||
.send(updateInfos); | ||
|
||
expect(response.statusCode).toBe(400); | ||
expect(response.body.message).toEqual([ | ||
'name must be a string', | ||
'email must be a string', | ||
]); | ||
}); | ||
}); |