Skip to content

Commit

Permalink
search optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
findthelorax committed Mar 3, 2024
1 parent 94eed3a commit e018ce6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
4 changes: 2 additions & 2 deletions controllers/doctorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports.createDoctor = async (req, res) => {
exports.getAllDoctors = async (req, res) => {
try {
const { profileId } = req.params;
const profile = await models.Profile.findById(profileId);
const profile = await models.Profile.findById(profileId).populate('doctors');

if (!profile) throw new Error('Profile not found');

Expand All @@ -45,7 +45,7 @@ exports.getAllDoctors = async (req, res) => {
exports.getOneDoctor = async (req, res) => {
try {
const { profileId, doctorId } = req.params;
const profile = await models.Profile.findById(profileId);
const profile = await models.Profile.findById(profileId).populate('doctors');

if (!profile) throw new Error('Profile not found');

Expand Down
41 changes: 25 additions & 16 deletions controllers/medicationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ exports.createMedication = async (req, res) => {
description,
unitOfMeasurement,
dose,
frequency,
quantity,
associatedDrug: drug,
doctor: doctor._id,
pharmacy: pharmacy._id,
frequency,
dateAdded: new Date(),
doctor,
pharmacy,
};
const medicationDoc = profile.medications.create(newMedication);
profile.medications.push(medicationDoc);
Expand All @@ -57,34 +57,39 @@ exports.createMedication = async (req, res) => {
}
};

exports.getAllMedications = async(req,res) => {
exports.deleteAllMedications = async(req,res) => {
try {
const { profileId } = req.params;
const profile = await models.Profile.findById(profileId).populate(['medications.associatedDrug']);
const profile = await models.Profile.findById(profileId);

if (!profile) {
return helpers.incomplete(res, 'Profile not found');
return res.status(404).json({ message: 'Profile not found' });
}

helpers.success(res, profile.medications);
profile.medications = [];

await profile.save();
helpers.success(res, { message: 'All meds cleared' });
} catch (err) {
helpers.error(res,err);
}
};

exports.deleteAllMedications = async(req,res) => {
exports.getAllMedications = async(req,res) => {
try {
const { profileId } = req.params;
const profile = await models.Profile.findById(profileId);

const profile = await models.Profile.findById(profileId).populate('medications.associatedDrug');
if (!profile) {
return res.status(404).json({ message: 'Profile not found' });
return helpers.incomplete(res, 'Profile not found');
}

profile.medications = [];
const medications = profile.medications.map(medication => {
const doctor = profile.doctors.id(medication.doctor);
const pharmacy = profile.pharmacy.id(medication.pharmacy);
return { ...medication._doc, doctor, pharmacy };
});

await profile.save();
helpers.success(res, { message: 'All meds cleared' });
helpers.success(res, medications);
} catch (err) {
helpers.error(res,err);
}
Expand All @@ -105,7 +110,11 @@ exports.getMedicationById = async(req,res) => {
return res.status(404).json({ message: 'Medication not found' });
}

helpers.success(res, medication);
const doctor = profile.doctors.id(medication.doctor);
const pharmacy = profile.pharmacy.id(medication.pharmacy);
const medicationWithDoctorAndPharmacy = { ...medication._doc, doctor, pharmacy };

helpers.success(res, medicationWithDoctorAndPharmacy);
} catch (err) {
helpers.error(res,err);
}
Expand All @@ -115,7 +124,7 @@ exports.updateMedication = async(req,res) => {
try {
const { profileId, medId } = req.params;
const updates = req.body;
const profile = await models.Profile.findById(profileId).populate('medications.associatedDrug');
const profile = await models.Profile.findById(profileId);

if (!profile) {
return res.status(404).json({ message: 'Profile not found' });
Expand Down
2 changes: 0 additions & 2 deletions controllers/profileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ exports.getProfile = async (req, res) => {
try {
const { profileId, userId } = req.params;
const getProfile = await models.Profile.findOne({ _id: profileId, users: userId });

if (!getProfile) throw new Error('Profile not found');

getProfile ? success(res, getProfile) : incomplete(res, 'Profile not found');
Expand All @@ -55,7 +54,6 @@ exports.updateProfile = async (req, res) => {
try {
const { profileId, userId } = req.params;
const updatedProfile = await models.Profile.findOneAndUpdate({ _id: profileId, users: userId }, req.body, { new: true, runValidators: true });

updatedProfile ? success(res, updatedProfile) : incomplete(res, 'Update failed');
} catch (err) {
error(res, err);
Expand Down
2 changes: 1 addition & 1 deletion models/drugModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const DrugSchema = new mongoose.Schema({
'food-interactions': [DrugFoodInteractionSchema],
'drug-interactions': [DrugInteractionSchema],
"external-links": { type: Map, of: DrugExternalLinkSchema },
}, { id: false });
});

DrugSchema.index({ name: 'text' });
DrugSchema.index({ 'drugbank-id': 1 });
Expand Down
8 changes: 2 additions & 6 deletions models/medicationModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,8 @@ const MedicationSchema = new Schema({
required: true,
},
// prescriber: String,
doctor: {
doctor: { type: Schema.Types.ObjectId, ref: 'Doctor' },
},
pharmacy: {
pharmacy: { type: Schema.Types.ObjectId, ref: 'Pharmacy' }
},
doctor: { type: Schema.Types.ObjectId, ref: 'Doctor' },
pharmacy: { type: Schema.Types.ObjectId, ref: 'Pharmacy' },
associatedDrug: { type: Schema.Types.ObjectId, ref: 'Drug' },
medicationIntakes: [MedicationIntakeSchema],
});
Expand Down

0 comments on commit e018ce6

Please sign in to comment.