Skip to content

Commit

Permalink
Merge pull request #15 from advanced-computer-lab-2023/Task-E-abdo
Browse files Browse the repository at this point in the history
Task-E-abdo: tests in progress
  • Loading branch information
3laaHisham authored Oct 14, 2023
2 parents 91ea641 + f31c54b commit 409bc56
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 11 deletions.
14 changes: 7 additions & 7 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion server/src/models/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import mongoose, { Document, Schema } from 'mongoose';

interface FamilyMemberWithID {
userID: mongoose.Types.ObjectId;
nationalID: string;
relation: 'Husband' | 'Wife' | 'Child';
}

interface FamilyMemberWithDetails {
name: string;
nationalID: string;
phone: string;
birthDate: Date;
gender: 'Male' | 'Female';
relation: 'Husband' | 'Wife' | 'Child';
}

Expand Down Expand Up @@ -144,6 +147,8 @@ const userSchema = new Schema<IUserDocument>(
name: String,
nationalID: String,
phone: String,
birthDate: Date,
gender: { type: String, enum: ['Male', 'Female'] },
relation: {
type: String,
enum: ['Husband', 'Wife', 'Child'],
Expand All @@ -152,7 +157,6 @@ const userSchema = new Schema<IUserDocument>(
}
],
validate: {
//TODO
validator: function (arr: any) {
try {
arr.forEach((elem: any) => {
Expand Down
6 changes: 5 additions & 1 deletion server/src/routes/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import express from 'express';
import controller from '../controllers/controller';
import { isAuthenticated, isAuthorized, queryParser } from '../middlewares';
import { getPatients, selectPatient } from '../services/doctor.service';
import { addFamilyMember } from '../services//patient.service';
const UserMeRouter = express.Router();

UserMeRouter.get('/me/patient/', (req, res) => controller(res)(getPatients)(req.body.doctorID, req.query));
UserMeRouter.get('/me/patient/:id', (req, res) => controller(res)(selectPatient)(req.body.doctorID, req.params.id));
UserMeRouter.put('/me/info/', (req, res) => {
// controller(res)(updateInfo)(req.body);
});

UserMeRouter.post('/me/family', (req, res) => {
// TODO if login is used we should add the patient id in the body form the jwt token
controller(res)(addFamilyMember)(req.body);
});
export default UserMeRouter;
35 changes: 35 additions & 0 deletions server/src/services/appointment.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import AppointmentModel from '../models/appointment.model';
import { StatusCodes } from 'http-status-codes';
import mongoose from 'mongoose';
import { HttpError } from '../utils';
const getUpcomingPatients = async (doctorID: string) => {
//get patients that have appointments with this doctor
const patients = await AppointmentModel.find({ doctorID: doctorID }).distinct('patientID').populate('patientID');
Expand Down Expand Up @@ -28,3 +30,36 @@ const getPatients = async (doctorID: string) => {
result: patients
};
};
const filterAppointment = async (
doctorID?: string,
patientID?: string,
status?: string,
startDate?: Date,
endDate?: Date
) => {
try {
let filter = {};
if (doctorID) filter = { ...filter, doctorID: new mongoose.Types.ObjectId(doctorID) };
if (patientID) filter = { ...filter, patientID: new mongoose.Types.ObjectId(patientID) };
if (status) filter = { ...filter, status: status };
if (startDate && endDate) {
filter = {
...filter,
startTime: { $gte: startDate, $lte: endDate },
endTime: { $gte: startDate, $lte: endDate }
};
} else if (startDate) {
filter = { ...filter, startTime: { $lte: startDate }, endTime: { $gte: startDate } };
} else {
filter = { ...filter, startTime: { $lte: { endDate } }, endTime: { $gte: endDate } };
}
const result = await AppointmentModel.find(filter);
return {
result: result,
status: StatusCodes.OK,
message: 'Successfully retrieved appointments'
};
} catch (e) {
throw new HttpError(StatusCodes.INTERNAL_SERVER_ERROR, `Error happened while retrieving appointments${e}`);
}
};
132 changes: 130 additions & 2 deletions server/src/services/patient.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import UserModel, { IPatient, IUserDocument, IUser, IDoctor } from '../models/user.model';
import UserModel, { IPatient, IUserDocument, IUser, IDoctor, FamilyMember } from '../models/user.model';
import PackageModel, { IPackageDocument } from '../models/package.model';
import mongoose, { Document } from 'mongoose';
import { getAllDoctor } from './doctor.service';
Expand All @@ -17,6 +17,118 @@ const hasActivePackage = (patient: (IPatient & Document) | IPatient): Boolean =>
const getPatientByID = async (patientId: string) => {
return UserModel.findOne({ _id: new mongoose.Types.ObjectId(patientId) });
};
const addFamilyMember = async (body: any) => {
console.log(body);
if (!(body.patientID && body.relation && body.nationalID)) {
throw new HttpError(StatusCodes.BAD_REQUEST, 'Please provide patientID, relation and nationalID');
}
if (body.name && body.birthDate && body.gender) {
return await addNonUserFamilyMember(
body.patientID,
body.name,
body.nationalID,
body.birthDate,
body.gender,
body.relation
);
} else if (body.userID) {
return await addUserFamilyMember(body.packageID, body.userID, body.relation, body.nationalID);
}
throw new HttpError(
StatusCodes.BAD_REQUEST,
'Either name, nationalID,gender,birthDate, and phone should be provided, or userID and relation should be provided.'
);
};
const addUserFamilyMember = async (patientID: string, userID: string, relation: string, nationalID: string) => {
try {
const filter = { _id: new mongoose.Types.ObjectId(patientID) };
const update = {
$push: {
family: {
userID: new mongoose.Types.ObjectId(userID),
relation: relation,
nationalID: nationalID
}
}
};

const updatedUser = await UserModel.findOneAndUpdate(filter, update, { new: true }).catch((e) => {
console.log(e);
});
return {
result: updatedUser,
status: StatusCodes.OK,
message: 'Family member added successfully'
};
} catch (error) {
throw new HttpError(StatusCodes.INTERNAL_SERVER_ERROR, `Unable to add the family member: ${error}`);
}
};

const getFamily = async (patientID: string) => {
try {
const family = await UserModel.find({
_id: new mongoose.Types.ObjectId(patientID)
}).select({
family: 1
});
let result = [];
for (const member of family as unknown as FamilyMember[]) {
let memberResult = { ...(member as any).toObject() };
if (memberResult.userID) {
const memberInfo = await UserModel.findById(memberResult.userID as mongoose.Types.ObjectId).select({
name: 1,
birthDate: 1,
gender: 1,
phone: 1
});
memberResult = { ...memberResult, ...memberInfo!.toObject() };
delete memberResult.userID;
}
result.push(memberResult);
}
return {
result: result,
status: StatusCodes.OK,
message: 'Family members retrieved successfully'
};
} catch (e) {
throw new HttpError(StatusCodes.INTERNAL_SERVER_ERROR, `Unable to add the family member${e}`);
}
};
const addNonUserFamilyMember = async (
patientID: string,
memberName: string,
nationalID: string,
birthDate: Date,
gender: string,
relation: string
) => {
try {
const filter = { _id: new mongoose.Types.ObjectId(patientID) };
const update = {
$push: {
family: {
name: memberName,
nationalID: nationalID,
birthDate: birthDate,
gender: gender,
relation: relation
}
}
};

const updatedUser = await UserModel.findOneAndUpdate(filter, update, { new: true });

return {
result: updatedUser,
status: StatusCodes.OK,
message: 'Family member added successfully'
};
} catch (error) {
throw new HttpError(StatusCodes.INTERNAL_SERVER_ERROR, `Unable to add the family member: ${error}`);
}
};

const viewAllDoctorsForPatient = async (patientId: string, doctorName?: string, specialty?: string, date?: Date) => {
try {
Expand Down Expand Up @@ -133,10 +245,26 @@ const selectPrescription = async (prescriptionID: string) => {
};
}
};
const getPatientHealthRecord = async (patientID: string) => {
try {
const result = await UserModel.findOne({ _id: new mongoose.Types.ObjectId(patientID) }).select('medicalHistory');
return {
result: result,
status: StatusCodes.OK,
message: 'Successfully retrieved health record'
};
} catch (e) {
throw new HttpError(StatusCodes.INTERNAL_SERVER_ERROR, `Error happened while retrieving health record${e}`);
}
};
export {
viewAllDoctorsForPatient,
calculateFinalSessionPrice,
getAllPrescription,
filterPrescriptions,
selectPrescription
selectPrescription,
getFamily,
hasActivePackage,
getPatientHealthRecord,
addFamilyMember
};
Loading

0 comments on commit 409bc56

Please sign in to comment.