Skip to content

Commit 4675306

Browse files
Merge pull request #28 from ShubhaMahobia/26-implement-prescription-model
"Add prescription routes and controller functions for generating and…
2 parents ee19c27 + fe2b70e commit 4675306

File tree

3 files changed

+163
-4
lines changed

3 files changed

+163
-4
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
const express = require("express");
2+
const bcrypt = require("bcryptjs");
3+
const UserDoctor = require("../model/UserDoctorModel");
4+
const UserPatient = require("../model/UserPatientModel");
5+
const Appointment = require("../model/AppointmentModel");
6+
const Prescription = require("../model/PrescriptionModel");
7+
const mongoose = require("mongoose");
8+
9+
//Function for generate the Prescription
10+
exports.generatePrecription = async (req, res) => {
11+
try {
12+
13+
//Creating Prescription Object
14+
const newPrescription = new Prescription({
15+
doctorId: req.body.doctorId,
16+
patientId: req.body.patientId,
17+
documentId: req.body.documentId,
18+
date: req.body.date,
19+
title:req.body.title,
20+
description: req.body.description,
21+
documentLink: req.body.documentLink,
22+
});
23+
const prescriptionExist = await Prescription.findOne({
24+
documentLink: req.body.documentLink,
25+
});
26+
// Checking for unique Identification Number for every Prescription -
27+
if (prescriptionExist) {
28+
return res.status(400).json({
29+
success: false,
30+
message: "Prescription Number Already Exist in Database",
31+
});
32+
}
33+
await newPrescription.save(); //Saving command for saving apointments to database
34+
return res.status(200).json({
35+
success: true,
36+
message: "Prescription Generated successfully",
37+
});
38+
} catch (error) {
39+
console.log(error);
40+
return res.status(400).json({
41+
success: false,
42+
message: "Internal Server Error",
43+
});
44+
}
45+
};
46+
47+
// Function for fetching the prescription by ID
48+
exports.getPrescriptionDetails = async (req, res) => {
49+
try {
50+
const prescriptionId = req.params.id; // Extract the prescription ID from request parameters
51+
52+
// Check if prescription ID is provided
53+
if (!prescriptionId) {
54+
return res.status(400).json({
55+
success: false,
56+
message: "prescription ID is required",
57+
});
58+
}
59+
60+
// Find the prescription by ID
61+
const prescription = await Prescription.findOne({ _id: prescriptionId });
62+
63+
// Check if prescription is not found
64+
if (!prescription) {
65+
return res.status(404).json({
66+
success: false,
67+
message: "prescription not found",
68+
});
69+
}
70+
71+
// Find patient details using patientId
72+
const patient = await UserPatient.findOne({
73+
firebaseUserId: prescription.patientId,
74+
});
75+
76+
// Find doctor details using doctorId
77+
const doctor = await UserDoctor.findOne({
78+
firebaseUserId: prescription.doctorId,
79+
});
80+
81+
if (!patient || !doctor) {
82+
return res.status(404).json({
83+
success: false,
84+
message: "Patient or Doctor details not found",
85+
});
86+
}
87+
88+
return res.status(200).json({
89+
success: true,
90+
message: "prescription details fetched successfully",
91+
prescription: {
92+
date: prescription.date,
93+
title:prescription.title,
94+
description: prescription.description,
95+
meetingLink: prescription.meetingLink,
96+
patient: patient,
97+
doctor: doctor,
98+
},
99+
});
100+
} catch (error) {
101+
console.log(error);
102+
return res.status(500).json({
103+
success: false,
104+
message: "Internal Server Error",
105+
});
106+
}
107+
};

model/PrescriptionModel.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const mongoose = require("mongoose");
2+
3+
const PrescriptionSchema = new mongoose.Schema({
4+
doctorId: {
5+
type: String,
6+
required: true,
7+
},
8+
patientId: {
9+
type: String,
10+
required: true,
11+
},
12+
13+
date: {
14+
type: String,
15+
required: true,
16+
},
17+
18+
title: {
19+
type: String,
20+
required: true,
21+
},
22+
23+
description: {
24+
type: String,
25+
required: true,
26+
},
27+
documentId: {
28+
type: String,
29+
required: true,
30+
},
31+
32+
documentLink: {
33+
type: String,
34+
required: true,
35+
},
36+
});
37+
38+
const prescription = mongoose.model("Prescription", PrescriptionSchema);
39+
40+
module.exports = prescription;

routes/authenticationRoutes.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ const {
1212
const {
1313
fetchAllDoctors,
1414
fetchDoctorById,
15-
updateDoctorProfile
15+
updateDoctorProfile,
1616
} = require("../controller/doctorController");
1717

1818
const { updatePatientProfile } = require("../controller/patientController");
1919

20-
const { bookAppointment ,getAppointmentDetails} = require("../controller/appointmentController");
20+
const {
21+
bookAppointment,
22+
getAppointmentDetails,
23+
} = require("../controller/appointmentController");
24+
25+
const {
26+
generatePrecription,
27+
getPrescriptionDetails,
28+
} = require("../controller/prescriptionController");
2129

2230
const router = express.Router();
2331

@@ -30,10 +38,14 @@ router.post("/getDoctor", fetchDoctorDetails);
3038
router.get("/getAllDoctors", fetchAllDoctors);
3139
router.get("/getDoctorById/:id", fetchDoctorById);
3240
router.put("/updatePatient/:id", updatePatientProfile);
33-
router.put("/updateDoctor/:id",updateDoctorProfile);
41+
router.put("/updateDoctor/:id", updateDoctorProfile);
3442

3543
//Appointment route
3644
router.post("/bookAppointment", bookAppointment);
37-
router.get('/getAppointments/:id', getAppointmentDetails);
45+
router.get("/getAppointments/:id", getAppointmentDetails);
46+
47+
//Prescription route
48+
router.post("/generatePrescription", generatePrecription);
49+
router.get("/getPrescription/:id", getPrescriptionDetails);
3850

3951
module.exports = router;

0 commit comments

Comments
 (0)