Skip to content
Snippets Groups Projects

added DR update API

9 open threads

Files

+ 125
0
/**
@file <file name>
@description <description>
@author <Your Name>
@created <YYYY-MM-DD>
**/
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { Logger, AppException, errorCode } from 'nest-common-utilities';
import { UpdateDoctorsRequestDto } from './dto/update-doctors-request.dto';
import { UpdateDoctorsResponseDto } from './dto/update-doctors-response.dto';
/**
* Service to encapsulate doctors
* domain logic such as create doctor update doctor.
*/
/**
*
*/
@Injectable()
export class DoctorsService {
private logger = new Logger(DoctorsService.name);
/**
* Constructs the DoctorsService.
*
* @param prisma - The Prisma service for database interactions.
*/
constructor(private prisma: PrismaService) {}
/**
* Update the doctor details by using doctor ID
*
* @param {UpdateDoctorsRequestDto} dto - The data transfer
* object containing the doctor information to be updated.
* @returns {Promise<UpdateDoctorsResponseDto>} A promise
* that resolves to the updated doctor's data.
*/
/**
* Updates an existing doctor record by deactivating the old entry and
* creating a new one with updated details.
*
* This method checks if the doctor with the given ID exists. If not,
* it throws an exception. If the doctor exists, it marks the current
* record as inactive, then inserts a new record with the updated data.
*
* @param {UpdateDoctorsRequestDto} dto - The DTO containing updated
* doctor information.
*
* @returns {Promise<UpdateDoctorsResponseDto>} The response containing
* the ID of the newly created doctor record.
*
* @throws {AppException} If no doctor is found with the given ID.
*/
async updateDoctorById(
dto: UpdateDoctorsRequestDto,
): Promise<UpdateDoctorsResponseDto> {
const {
doctorId,
userId,
doctorName,
doctorRegNo,
doctorSpecialityId,
doctorQualificationId,
contactNo,
hprCode,
emailId,
panNo,
address,
pincode,
latitude,
longitude,
stateId,
cityId,
} = dto;
const existingDoctor = await this.prisma.doctor_master.findUnique({
where: { id: doctorId },
});
if (!existingDoctor) {
throw new AppException(
`Doctor with ID ${doctorId} not found`,
errorCode.DATA_NOT_FOUND,
);
}
// Deactivate the old doctor record
await this.prisma.doctor_master.update({
where: { id: doctorId },
data: {
is_active: false,
updated_by: userId,
updated_at: new Date(),
},
});
const data = {
name: doctorName ?? '',
registration_no: doctorRegNo ?? '',
speciality_id: doctorSpecialityId ?? '',
qualification_id: doctorQualificationId ?? '',
doctor_contact: contactNo ?? '',
hpr_code: hprCode ?? '',
doctor_email_id: emailId ?? '',
pan_no: panNo,
address: address ?? '',
pincode_id: pincode ?? '',
latitude: latitude ?? null,
longitude: longitude ?? null,
state_id: stateId ?? '',
city_id: cityId ?? '',
is_active: true,
created_by: userId ?? '',
updated_by: userId ?? '',
};
//@ts-ignore
const newDoctor = await this.prisma.doctor_master.create({ data });
return {
doctorId: newDoctor.id,
};
}
}
Loading