diff --git a/src/modules/patient-svc/dto/patient.dto.ts b/src/modules/patient-svc/dto/patient.dto.ts index 3437d77..93c0c8d 100644 --- a/src/modules/patient-svc/dto/patient.dto.ts +++ b/src/modules/patient-svc/dto/patient.dto.ts @@ -35,6 +35,17 @@ export class CreatePatientDto { @IsOptional() @IsString() city?: string; + + @IsOptional() + @IsString() + addedBy?: string +} + +export class EditPatientDto extends CreatePatientDto { + @IsNotEmpty() + @IsUUID() + @IsString() + id: string; } export class PatientResponseDto { diff --git a/src/modules/patient-svc/entities/patient.entity.ts b/src/modules/patient-svc/entities/patient.entity.ts index 04fb1d0..3c1448f 100644 --- a/src/modules/patient-svc/entities/patient.entity.ts +++ b/src/modules/patient-svc/entities/patient.entity.ts @@ -28,4 +28,7 @@ export class Patient extends BaseEntity { @Column({ nullable: true }) city?: string; + + @Column({ nullable: true }) + addedBy?: string } diff --git a/src/modules/patient-svc/patient-svc.service.test.ts b/src/modules/patient-svc/patient-svc.service.test.ts index 041795f..d3dee1e 100644 --- a/src/modules/patient-svc/patient-svc.service.test.ts +++ b/src/modules/patient-svc/patient-svc.service.test.ts @@ -68,8 +68,11 @@ describe('PatientSvcService', () => { create: jest.fn().mockReturnValue(mockPatient), save: jest.fn().mockResolvedValue(mockPatient), createQueryBuilder: jest.fn().mockReturnValue({ + select: jest.fn().mockReturnThis(), + where: jest.fn().mockReturnThis(), whereInIds: jest.fn().mockReturnThis(), getMany: jest.fn().mockResolvedValue([mockPatient, mockPatient]), + getRawMany: jest.fn().mockResolvedValue([mockPatient, mockPatient]), }), } as unknown as Repository; diff --git a/src/modules/patient-svc/patient-svc.service.ts b/src/modules/patient-svc/patient-svc.service.ts index 76553ed..dfddc0d 100644 --- a/src/modules/patient-svc/patient-svc.service.ts +++ b/src/modules/patient-svc/patient-svc.service.ts @@ -8,7 +8,7 @@ import { _403, _404 } from '../../common/constants/errors'; import { Repository } from 'typeorm'; import { UserType } from '../../common/constants/enums'; import { Transaction } from '../smart-contract/entities/transaction.entity'; -import { CreatePatientDto, PatientResponseDto } from './dto/patient.dto'; +import { CreatePatientDto, EditPatientDto, PatientResponseDto } from './dto/patient.dto'; import { Patient } from './entities/patient.entity'; @Injectable() @@ -36,6 +36,18 @@ export class PatientSvcService { return await this.patientRepository.save(patient); } + async updatePatient(patientDto: EditPatientDto): Promise { + const patient = await this.patientRepository.findOne({ + where: { id: patientDto.id }, + }); + + const updateData = patientDto; + delete updateData.id; + await this.patientRepository.save( {...patient, ...updateData } ); + + return { ...patient,...updateData}; + } + /** * This function is used to find a patient by his/her phone number */ @@ -78,13 +90,27 @@ export class PatientSvcService { .groupBy('transaction.ownerId') .getRawMany(); + const addedByUser = await this.patientRepository + .createQueryBuilder('patient') + .select('patient.id', 'id') + .where('patient.added_by = :payerId', { payerId }) + .getRawMany(); + const addedByUserUniqueIds = addedByUser.map( result => result.id ); + const uniquePatientIds = uniquePatientIdsQuery.map( (result) => result.ownerId, ); + const combinedPatientIds = Object.values( [...uniquePatientIds, ...addedByUserUniqueIds ].reduce( ( acc, item ) => { + acc[ item ] = item; + return acc; + }, {} )); + + console.log('combined', combinedPatientIds ); + const patients = await this.patientRepository .createQueryBuilder('patient') - .whereInIds(uniquePatientIds) + .whereInIds(combinedPatientIds) .getMany(); return patients.map((patient) => { diff --git a/src/modules/payer-svc/payer-svc.controller.ts b/src/modules/payer-svc/payer-svc.controller.ts index 7225fb6..74bc1d0 100644 --- a/src/modules/payer-svc/payer-svc.controller.ts +++ b/src/modules/payer-svc/payer-svc.controller.ts @@ -23,6 +23,7 @@ import { _403, _404, _409 } from '../../common/constants/errors'; import { CachingService } from '../caching/caching.service'; import { CreatePatientDto, + EditPatientDto, PatientResponseDto, } from '../patient-svc/dto/patient.dto'; import { PatientSvcService } from '../patient-svc/patient-svc.service'; @@ -125,6 +126,17 @@ export class PayerSvcController { return await this.patientService.registerPatient(createPatientAccountDto); } + @Put('patient') + @Roles(UserRole.PAYER) + @ApiOperation({ + summary: 'This API is used update the Patient by PAYER.', + }) + async updatePatient( + @Body() editPatientAccountDto: EditPatientDto, + ): Promise { + return await this.patientService.updatePatient(editPatientAccountDto); + } + @Post('send-invite') @Roles(UserRole.PAYER) @ApiOperation({