Skip to content

Commit

Permalink
test1
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrhman500 committed Oct 14, 2023
1 parent 3badfdd commit f31c54b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 39 deletions.
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;
106 changes: 71 additions & 35 deletions server/src/services/patient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,54 @@ 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 addUserFamilyMember = async (patientID: string, userID: string, relation: string) => {
const filter = { _id: patientID };
const update = {
userID: new mongoose.Types.ObjectId(userID),
relation: relation
};

const updatedUser = await UserModel.findOneAndUpdate(filter, update, { new: true })
.then((user) => user)
.catch((e) => {
throw new HttpError(StatusCodes.INTERNAL_SERVER_ERROR, `Unable to add the family member${e}`);
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'
};
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({
Expand Down Expand Up @@ -75,26 +104,32 @@ const addNonUserFamilyMember = async (
gender: string,
relation: string
) => {
const filter = { _id: new mongoose.Types.ObjectId(patientID) };
const update = {
name: memberName,
nationalID: nationalID,
birthDate: birthDate,
gender: gender,
relation: relation
};
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 })
.then((user) => user)
.catch((e) => {
throw new HttpError(StatusCodes.INTERNAL_SERVER_ERROR, `Unable to add the family member${e}`);
});
return {
result: updatedUser,
status: StatusCodes.OK,
message: 'family member added successfully'
};
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 {
var sessionDiscount = 0;
Expand Down Expand Up @@ -230,5 +265,6 @@ export {
selectPrescription,
getFamily,
hasActivePackage,
getPatientHealthRecord
getPatientHealthRecord,
addFamilyMember
};
7 changes: 4 additions & 3 deletions server/src/tests/fakeData/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const generateFakeUser = async (): Promise<IUser> => {
gender: randomArrayElement(['Male', 'Female']),
phone: faker.phone.number(),
addresses: [faker.location.secondaryAddress()],
role: randomArrayElement(['Patient', 'Doctor', 'Administrator']),
role: 'Patient', //randomArrayElement(['Patient', 'Doctor', 'Administrator']),
profileImage: faker.image.avatar(),
isEmailVerified: faker.number.int() % 2 === 0,
wallet: faker.number.int(),
Expand Down Expand Up @@ -159,11 +159,12 @@ const generateFakeVacations = (): { from: Date; to: Date }[] => {
return vacations;
};

const NUM_USERS_TO_GENERATE = 10;
const NUM_USERS_TO_GENERATE = 1;

const generateFakeData = async (): Promise<void> => {
const fakeUsers: IUser[] = await Promise.all(Array.from({ length: NUM_USERS_TO_GENERATE }, generateFakeUser));
console.log(fakeUsers);
};

generateFakeData().then(() => console.log('Fake data generated successfully.'));
// generateFakeData().then(() => console.log('Fake data generated successfully.'));
console.log(generateFamilyMember());

0 comments on commit f31c54b

Please sign in to comment.