Skip to content

Commit

Permalink
medical history server side added
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Oct 4, 2024
1 parent d3201ec commit c10e3d5
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 48 deletions.
5 changes: 5 additions & 0 deletions client/lib/api/doctor/authorizedRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ export const getPatientsOfDoctor = async (limit:number,offset:number) => {
return response.data;
}

export const getPatientMedicalHistory = async (patientId:string,limit:number,offset:number)=>{
const response = await doctorAxiosInstance.get(`/medical-history/${patientId}?limit=${limit}&offset=${offset}`);
return response.data;
}

export default doctorAxiosInstance;
21 changes: 14 additions & 7 deletions client/lib/hooks/doctor/useDoctor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { getPatientsOfDoctor } from "@/lib/api/doctor/authorizedRoutes"
import { getPatientMedicalHistory, getPatientsOfDoctor } from "@/lib/api/doctor/authorizedRoutes"
import { ErrorResponse, PaginatedResult } from "@/types"
import { IPatient } from "@/types/entities"
import { IExtendedAppointment, IPatient } from "@/types/entities"
import { useQuery } from "@tanstack/react-query"
import { AxiosError } from "axios"

export const useGetPatientsDoctor = (offset:number,limit:number)=>{
return useQuery<PaginatedResult<IPatient>,AxiosError<ErrorResponse>>({
queryKey:["doctor-patients",offset,limit],
queryFn:()=>getPatientsOfDoctor(limit,offset)
export const useGetPatientsDoctor = (offset: number, limit: number) => {
return useQuery<PaginatedResult<IPatient>, AxiosError<ErrorResponse>>({
queryKey: ["doctor-patients", offset, limit],
queryFn: () => getPatientsOfDoctor(limit, offset)
});
};
};

export const useGetMedicalHistory = (patientId: string, offset: number, limit: number) => {
return useQuery<PaginatedResult<IExtendedAppointment>, AxiosError<ErrorResponse>>({
queryKey: ["medical-history", offset, limit, patientId],
queryFn: () => getPatientMedicalHistory(patientId, limit, offset)
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import IPatient from "../../entities/IPatient";
import IRepository from "./IRepository";

export default interface IAppointmentRepository extends IRepository<IAppointment> {
updateManyBySlotIdsNotInStatuses(slotIds: string[], fields: IAppointment, notInStatuses:AppointmentStatus[]): Promise<IAppointment[] | null>;
updateManyBySlotIdsNotInStatuses(slotIds: string[], fields: IAppointment, notInStatuses: AppointmentStatus[]): Promise<IAppointment[] | null>;
findByDateAndSlot(appointmentDate: string, slotId: string): Promise<IAppointment | null>;
findManyByDateAndDoctorId(appointmentDate: string, doctorId: string): Promise<IAppointment[] | null>;
updateAppointmentStatusToConfirmed(appointmentId: string): Promise<void>;
Expand All @@ -15,12 +15,13 @@ export default interface IAppointmentRepository extends IRepository<IAppointment
limit: number,
status?: AppointmentStatus
): Promise<PaginatedResult<IAppointment>>;
findMayByPatientId(
findManyByPatientId(
patientId: string,
offset: number,
limit: number,
status?: AppointmentStatus
): Promise<PaginatedResult<IAppointment>>;
findManyByIds(ids: string[]): Promise<IAppointment[] | null>;
findPatientsByDoctorId(doctorId:string, limit:number, offset:number):Promise<PaginatedResult<IPatient>>;
findPatientsByDoctorId(doctorId: string, limit: number, offset: number): Promise<PaginatedResult<IPatient>>;
findManyAsExtendedByPatientId(patientId: string, limit: number, offset: number): Promise<PaginatedResult<IExtendedAppointment>>
}
127 changes: 93 additions & 34 deletions server/src/infrastructure/repositories/AppointmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,112 @@ export default class AppointmentRepository implements IAppointmentRepository {
return await this.model.find({ _id: { $in: ids } });
}

async findPatientsByDoctorId(doctorId: string, limit:number, offset:number): Promise<PaginatedResult<IPatient>> {
async findManyAsExtendedByPatientId(
patientId: string,
limit: number,
offset: number
): Promise<PaginatedResult<IExtendedAppointment>> {
const result = await this.model.aggregate([
{
$match: { patientId: new ObjectId(patientId) }
},
{
$lookup: {
from: "patients",
localField: "patientId",
foreignField: "_id",
as: "patient"
}
},
{
$lookup: {
from: "doctors",
localField: "doctorId",
foreignField: "_id",
as: "doctor"
}
},
{
$unwind: "$patient"
},
{
$unwind: "$doctor"
},
{
$project: {
"patient.password": 0,
"patient.token": 0,
"doctor.password": 0,
"doctor.token": 0
}
},
{
$facet: {
paginatedResults: [
{ $skip: offset },
{ $limit: limit }
],
totalCount: [
{ $count: "count" }
]
}
}
]).exec();

const appointments = result[0].paginatedResults;
const totalItems = result[0].totalCount.length > 0 ? result[0].totalCount[0].count : 0;

return getPaginatedResult(totalItems, offset, limit, appointments);
}


async findPatientsByDoctorId(doctorId: string, limit: number, offset: number): Promise<PaginatedResult<IPatient>> {
const result = await this.model.aggregate([
{
$match: { doctorId: new ObjectId(doctorId) }
$match: { doctorId: new ObjectId(doctorId) }
},
{
$group: { _id: "$patientId" }
$group: { _id: "$patientId" }
},
{
$lookup: {
from: "patients",
localField: "_id",
foreignField: "_id",
as: "patientInfo"
}
$lookup: {
from: "patients",
localField: "_id",
foreignField: "_id",
as: "patientInfo"
}
},
{
$unwind: "$patientInfo"
$unwind: "$patientInfo"
},
{
$replaceRoot: { newRoot: "$patientInfo" }
$replaceRoot: { newRoot: "$patientInfo" }
},
{
$facet: {
paginatedResults: [
{ $skip: offset },
{ $limit: limit },
{
$project: {
password: 0,
token: 0
}
$facet: {
paginatedResults: [
{ $skip: offset },
{ $limit: limit },
{
$project: {
password: 0,
token: 0
}
],
totalCount: [
{ $count: "count" }
]
}
}
],
totalCount: [
{ $count: "count" }
]
}
}
]).exec();
const patients = result[0].paginatedResults;
const totalItems = result[0].totalCount.length > 0 ? result[0].totalCount[0].count : 0;
return getPaginatedResult(totalItems, offset, limit, patients);
}
]).exec();

const patients = result[0].paginatedResults;
const totalItems = result[0].totalCount.length > 0 ? result[0].totalCount[0].count : 0;

return getPaginatedResult(totalItems, offset, limit, patients);
}


async findDetailsById(appointmentId: string): Promise<IExtendedAppointment | null> {
const objectId = new ObjectId(appointmentId);
Expand Down Expand Up @@ -133,7 +192,7 @@ export default class AppointmentRepository implements IAppointmentRepository {
return appointment[0] as IExtendedAppointment;
}

async findMayByPatientId(
async findManyByPatientId(
patientId: string,
offset: number,
limit: number,
Expand Down
2 changes: 1 addition & 1 deletion server/src/use_case/appointment/GetAppointmentUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class GetAppointmentUseCase {
limit: number,
status?: AppointmentStatus
): Promise<PaginatedResult<IAppointment> | null> {
return await this.appointmentRepository.findMayByPatientId(patientId, offset, limit, status);
return await this.appointmentRepository.findManyByPatientId(patientId, offset, limit, status);
}

async getSuccessPageDetails(paymentId: string): Promise<IAppointment | null> {
Expand Down
6 changes: 3 additions & 3 deletions server/src/use_case/doctor/GetPatientUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import IAppointmentRepository from "../../domain/interface/repositories/IAppointmentRepository";
import IValidatorService from '../../domain/interface/services/IValidatorService'
import IAppointment from "../../domain/entities/IAppointment";
import { IExtendedAppointment } from "../../domain/entities/IAppointment";
import IPatient from "../../domain/entities/IPatient";
import { PaginatedResult } from "../../types";

Expand All @@ -15,8 +15,8 @@ export default class GetPatientUseCaseDoctor {
return await this.appointmentRepository.findPatientsByDoctorId(doctorId, limit, offset);
}

async getMedicalHistory(patientId: string, offset: number, limit: number): Promise<PaginatedResult<IAppointment>> {
async getMedicalHistory(patientId: string, offset: number, limit: number): Promise<PaginatedResult<IExtendedAppointment>> {
this.validatorService.validateIdFormat(patientId)
return await this.appointmentRepository.findMayByPatientId(patientId, offset, limit)
return await this.appointmentRepository.findManyAsExtendedByPatientId(patientId, limit, offset)
}
}

0 comments on commit c10e3d5

Please sign in to comment.