|
| 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 Hospital = require("../model/HospitalModel"); |
| 7 | +const mongoose = require("mongoose"); |
| 8 | +const geolib = require("geolib"); |
| 9 | + |
| 10 | +//Function for Booking the Hospital |
| 11 | +exports.addHospital = async (req, res) => { |
| 12 | + try { |
| 13 | + //Creating Hospital Object |
| 14 | + const newHospital = new Hospital({ |
| 15 | + name: req.body.name, |
| 16 | + rating: req.body.rating, |
| 17 | + hours: req.body.hours, |
| 18 | + address: req.body.address, |
| 19 | + phone: req.body.phone, |
| 20 | + description: req.body.description, |
| 21 | + longitude: req.body.longitude, |
| 22 | + latitude: req.body.latitude, |
| 23 | + imageLink: req.body.imageLink, |
| 24 | + noOfBed: req.body.noOfBed, |
| 25 | + specilization: req.body.specilization, |
| 26 | + browserLink: req.body.browserLink, |
| 27 | + }); |
| 28 | + const hospitalExist = await Hospital.findOne({ |
| 29 | + address: req.body.address, |
| 30 | + }); |
| 31 | + // Checking for unique Identification Number for every Hospital - |
| 32 | + if (hospitalExist) { |
| 33 | + return res.status(400).json({ |
| 34 | + success: false, |
| 35 | + message: "Identification Number Already Exist in Database", |
| 36 | + }); |
| 37 | + } |
| 38 | + await newHospital.save(); //Saving command for saving Hospital to database |
| 39 | + return res.status(200).json({ |
| 40 | + success: true, |
| 41 | + message: "Hospital added successfully", |
| 42 | + }); |
| 43 | + } catch (error) { |
| 44 | + console.log(error); |
| 45 | + return res.status(400).json({ |
| 46 | + success: false, |
| 47 | + message: "Internal Server Error", |
| 48 | + }); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +// Function for fetching the Hospital by ID |
| 53 | +exports.getAllHospitalDetails = async (req, res) => { |
| 54 | + try { |
| 55 | + // Find the All Hospital |
| 56 | + const hospitals = await Hospital.find(); |
| 57 | + |
| 58 | + // Check if Hospital is not found |
| 59 | + if (!hospitals) { |
| 60 | + return res.status(404).json({ |
| 61 | + success: false, |
| 62 | + message: "Hospital not found", |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + return res.status(200).json({ |
| 67 | + success: true, |
| 68 | + message: "Hospital details fetched successfully", |
| 69 | + data: hospitals, |
| 70 | + }); |
| 71 | + } catch (error) { |
| 72 | + console.log(error); |
| 73 | + return res.status(500).json({ |
| 74 | + success: false, |
| 75 | + message: "Internal Server Error", |
| 76 | + }); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +// Function for fetching the Hospital by ID |
| 81 | +exports.getHospitalDetails = async (req, res) => { |
| 82 | + try { |
| 83 | + const hospitalId = req.params.id; // Extract the Hospital ID from request parameters |
| 84 | + |
| 85 | + // Check if Hospital ID is provided |
| 86 | + if (!hospitalId) { |
| 87 | + return res.status(400).json({ |
| 88 | + success: false, |
| 89 | + message: "Hospital ID is required", |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + // Find the Hospital by ID |
| 94 | + const hospital = await Hospital.findOne({ _id: hospitalId }); |
| 95 | + |
| 96 | + // Check if Hospital is not found |
| 97 | + if (!hospital) { |
| 98 | + return res.status(404).json({ |
| 99 | + success: false, |
| 100 | + message: "Hospital not found", |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + return res.status(200).json({ |
| 105 | + success: true, |
| 106 | + message: "Hospital details fetched successfully", |
| 107 | + hospital: { |
| 108 | + name: hospital.name, |
| 109 | + rating: hospital.rating, |
| 110 | + hours: hospital.hours, |
| 111 | + address: hospital.address, |
| 112 | + phone: hospital.phone, |
| 113 | + description: hospital.description, |
| 114 | + longitude: hospital.longitude, |
| 115 | + latitude: hospital.latitude, |
| 116 | + imageLink: hospital.imageLink, |
| 117 | + noOfBed: hospital.noOfBed, |
| 118 | + specilization: hospital.specilization, |
| 119 | + browserLink: hospital.browserLink, |
| 120 | + }, |
| 121 | + }); |
| 122 | + } catch (error) { |
| 123 | + console.log(error); |
| 124 | + return res.status(500).json({ |
| 125 | + success: false, |
| 126 | + message: "Internal Server Error", |
| 127 | + }); |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +//function to fetch hospitals based on patient latitude and longitude |
| 132 | +exports.fetchHospitalsByLocation = async (req, res) => { |
| 133 | + try { |
| 134 | + const { latitude, longitude } = req.body; // Assuming latitude and longitude are passed in the request body |
| 135 | + const radius = req.query.radius || 10000; // Default radius of 10km, can be overridden in query parameter |
| 136 | + |
| 137 | + // Fetch all hospitals from the database |
| 138 | + const hospitals = await Hospital.find(); |
| 139 | + |
| 140 | + // If latitude and longitude are provided, filter hospitals based on distance from patient's location |
| 141 | + if (latitude && longitude) { |
| 142 | + const nearbyHospitals = hospitals.filter((hospital) => { |
| 143 | + const hospitalLocation = { |
| 144 | + latitude: hospital.latitude, |
| 145 | + longitude: hospital.longitude, |
| 146 | + }; |
| 147 | + const distance = geolib.getDistance( |
| 148 | + { latitude, longitude }, |
| 149 | + hospitalLocation |
| 150 | + ); |
| 151 | + return distance <= radius; |
| 152 | + }); |
| 153 | + res.json(nearbyHospitals); |
| 154 | + } else { |
| 155 | + // If latitude and longitude are not provided, return all hospitals |
| 156 | + res.json(hospitals); |
| 157 | + } |
| 158 | + } catch (error) { |
| 159 | + console.error("Error fetching hospitals:", error); |
| 160 | + res.status(500).json({ |
| 161 | + success: false, |
| 162 | + message: "Internal server error", |
| 163 | + }); |
| 164 | + } |
| 165 | +}; |
0 commit comments