Skip to content

Commit

Permalink
Merge pull request #16 from ShubhaMahobia/14-fetch-all-doctors
Browse files Browse the repository at this point in the history
implemented the fetch all doctors API
  • Loading branch information
ShubhaMahobia authored Apr 5, 2024
2 parents b4d2303 + 1dc7cf1 commit 3358a0f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 49 deletions.
101 changes: 53 additions & 48 deletions controller/authenticationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const express = require("express");
const userPatient = require("../model/UserPatientModel");
const bcrypt = require("bcryptjs");
const userDoctor = require("../model/UserDoctorModel");
const mongoose = require('mongoose');

const mongoose = require("mongoose");

//This is a test function to check is the server is running or not.
exports.test = (req, res) => {
Expand Down Expand Up @@ -39,15 +38,14 @@ exports.isNewUser = async (req, res) => {
//This is the create Profile Function for the user(Patient)
exports.signUpPatient = async (req, res) => {
try {

//Here we are saving the identification number in encrypted form so that it will be secured in our database
const identificationNumber = req.body.identificationNumber;
const hashedIdentificationNumber = await bcrypt.hash(
identificationNumber,
12
);

//Creating patient Object
//Creating patient Object
const patient = new userPatient({
firebaseUserId: req.body.firebaseUserId,
firstName: req.body.firstName,
Expand Down Expand Up @@ -82,19 +80,18 @@ exports.signUpPatient = async (req, res) => {
message: "Internal Server Error",
});
}
}
};
//This is the create Profile Function for the user(Doctor)
exports.signUpDoctor = async (req, res) => {
try {

//Here we are saving the identification number in encrypted form so that it will be secured in our database
const identificationNumber = req.body.identificationNumber;
const hashedIdentificationNumber = await bcrypt.hash(
identificationNumber,
12
);

//Creating Doctor Object
//Creating Doctor Object
const doctor = new userDoctor({
firebaseUserId: req.body.firebaseUserId,
firstName: req.body.firstName,
Expand Down Expand Up @@ -130,54 +127,62 @@ exports.signUpDoctor = async (req, res) => {
message: "Internal Server Error",
});
}
}
};

//Login Fuction for fetching user Patient details
exports.fetchUserDetails= async (req, res) => {
try {
const email=req.body.email;
if( email==null){
return res.status(400).json({ success:false ,message:"Please provide an email"});
//Login Fuction for fetching user Patient details
exports.fetchUserDetails = async (req, res) => {
try {
const email = req.body.email;
if (email == null) {
return res
.status(400)
.json({ success: false, message: "Please provide an email" });
} else {
const user = await userPatient.findOne({ Email: email });
if (!user) {
return res.status(401).json({
success: false,
message: "Authentication failed! Email not found.",
});
} else {
return res
.status(401)
.json({ success: true, data: user, message: "User success" });
}
}
else{
const user=await userPatient.findOne({Email:email});
if(!user){
return res.status(401).json({success:false,message:"Authentication failed! Email not found."});
}
else{
return res.status(401).json({success:true,data: user, message:"User success"});
}
}


} catch (err) {
return res.status(500).json({success:false,message:"Internal Server Error"});

return res
.status(500)
.json({ success: false, message: "Internal Server Error" });
}
}

};

//Login Fuction for fetching user Doctor details
exports.fetchDoctorDetails= async (req, res) => {
try {
const email=req.body.email;
if( email==null){
return res.status(400).json({ success:false ,message:"Please provide an email"});
exports.fetchDoctorDetails = async (req, res) => {
try {
const email = req.body.email;
if (email == null) {
return res
.status(400)
.json({ success: false, message: "Please provide an email" });
} else {
const user = await userDoctor.findOne({ Email: email });
if (!user) {
return res.status(401).json({
success: false,
message: "Authentication failed! Email not found.",
});
} else {
return res
.status(401)
.json({ success: true, data: user, message: "User success" });
}
}
else{
const user=await userDoctor.findOne({Email:email});
if(!user){
return res.status(401).json({success:false,message:"Authentication failed! Email not found."});
}
else{
return res.status(401).json({success:true,data: user, message:"User success"});
}
}


} catch (err) {
return res.status(500).json({success:false,message:"Internal Server Error"});

return res
.status(500)
.json({ success: false, message: "Internal Server Error" });
}
}
};


43 changes: 43 additions & 0 deletions controller/doctorController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const express = require("express");
const bcrypt = require("bcryptjs");
const userDoctor = require("../model/UserDoctorModel");
const mongoose = require("mongoose");

//Function for fetching all the doctors
exports.fetchAllDoctors = async (req, res) => {
try {
const allDoctors = await userDoctor.find({});
return res.status(200).json({
success: true,
message: "Details fetched successfully",
data: allDoctors,
});
} catch (err) {
return res
.status(500)
.json({ success: false, message: "Internal Server Error" });
}
};

// Function to fetch a doctor by ID (firebaseId)
exports.fetchDoctorById = async (req, res) => {
try {
const doctorId = req.params.id; // Assuming the ID is passed as a route parameter
const doctor = await userDoctor.findOne({ firebaseUserId: doctorId }); // Changed to findOne with firebaseUserId
if (!doctor) {
return res
.status(404)
.json({ success: false, message: "Doctor not found" });
}

return res.status(200).json({
success: true,
message: "Doctor details fetched successfully",
data: doctor,
});
} catch (err) {
return res
.status(500)
.json({ success: false, message: "Internal Server Error" });
}
};
9 changes: 8 additions & 1 deletion routes/authenticationRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const {
fetchUserDetails,
fetchDoctorDetails,
} = require("../controller/authenticationController");

const {
fetchAllDoctors,
fetchDoctorById,
} = require("../controller/doctorController");
const router = express.Router();

router.get("/", test);
Expand All @@ -16,5 +21,7 @@ router.post("/checkNewUser", isNewUser);
router.post("/registerDoctor", signUpDoctor);
router.post("/getUser", fetchUserDetails);
router.post("/getDoctor", fetchDoctorDetails);
router.get("/getAllDoctors", fetchAllDoctors);
router.get("/getDoctorById/:id", fetchDoctorById);

module.exports = router;
module.exports = router;

0 comments on commit 3358a0f

Please sign in to comment.