|
| 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 | +}; |
0 commit comments