Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
neerajvijay1997 committed Nov 21, 2022
0 parents commit ba20b2f
Show file tree
Hide file tree
Showing 20 changed files with 5,459 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
.env
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Information about API:
### Theme:
- We’re going to design an API for the doctors of a Hospital which has been allocated by the
govt for testing and quarantine + well being of COVID-19 patients
- There can be 2 types of Users
- Doctors
- Patients
- Doctors can log in
- Each time a patient visits, the doctor will follow 2 steps
- Register the patient in the app (using phone number, if the patient already exists, just
return the patient info in the API)
- After the checkup, create a Report
- Patient Report will have the following fields
- Created by doctor
- Status (You can use enums if you want to):
- Can be either of: [Negative, Travelled-Quarantine, Symptoms-Quarantine,
Positive-Admit]

- Date

# Instructions about SetUp:

First start with downloading the code and and write npm install on code editor, it will install all dependencies on your editor.
You will need a code editor and mongoDB setup on your computer.
We will use postman to check the api is working or not,So download postman on your computer.

1.Now use this **http://localhost:5000/api/v1/doctors/register** route to register doctor in API and add the info as shown in image

![1](https://user-images.githubusercontent.com/101451924/185754068-67021b2a-af59-4fff-9af5-4a783bc46a90.png)


2.Use this **http://localhost:5000/api/v1/doctors/login** to login as a doctor and add info as shown in image

![2](https://user-images.githubusercontent.com/101451924/185754072-595c68a2-1d27-4e9e-b21d-b2f6b44202c6.png)

3.Use this **http://localhost:5000/api/v1/patients/register** and add the token in authorization area which is recieved in second point

![3](https://user-images.githubusercontent.com/101451924/185754077-8e82542a-dfd9-4ff7-b097-4821bdb52e41.png)


4. Use this **http://localhost:5000/api/v1/patients/62c31cc86a84003324d04cb5/create_report** to create report and add status you can see the types of
status in report model.

![4](https://user-images.githubusercontent.com/101451924/185754082-588c13a9-177b-4d16-a527-74dedca6584d.png)


5. Use this **http://localhost:5000/api/v1/patients/62c31cc86a84003324d04cb5/all_reports** to get all the reports.

![5](https://user-images.githubusercontent.com/101451924/185754089-35b8db22-dfcb-4eee-899c-b3d3a014b365.png)

# DEPLOYEMENT
Project already deployed on Heroku(For better exp: use Postman for accesing above Routes)

Do Visit: https://hospitalapi0.herokuapp.com/api/v1/doctors/register
44 changes: 44 additions & 0 deletions config/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const jwt = require('jsonwebtoken');
const Doctor = require("../models/doctor")
//Format of token
//Authorizaiton : Bearer <access_token>


//verify token
exports.verifyToken = async (req, res, next) => {

console.log("Bearer Token"+req.headers['authorization']);
let token;

if (req.headers.authorization && req.headers.authorization.startsWith("Bearer")) {
token = req.headers.authorization.split(" ")[1];
console.log("TOKEN : "+token);
req.token = token;
}

if (!token) {
console.log("Token Error");
return res.status(401).json({
success: false,
message: "Unauthroized access"
});
}

try {

const decoded = await jwt.verify(token, 'secret');
console.log("DECODED TOKEN : "+decoded);

req.doctor = await Doctor.findById(decoded.id);
next();


} catch (err) {
console.log(err);
return res.status(401).json({
success: false,
message: "Unauthroized access"
});
}
};

15 changes: 15 additions & 0 deletions config/mongoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose = require('mongoose');
require('dotenv').config();

let DB_URL = process.env. DB_URL


//connect to DB
mongoose.connect( DB_URL || 'mongodb://localhost/hospital_API', { useNewUrlParser: true ,useUnifiedTopology: true });
const db = mongoose.connection;

db.on('error', console.error.bind(console, "Error connecting to db"));

db.once('open', function () {
console.log("Successfully connected to db");
});
30 changes: 30 additions & 0 deletions config/passport_jwt_strategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const passport = require('passport');
const JWTStrategy = require('passport-jwt').Strategy;
const ExtractJWT = require('passport-jwt').ExtractJwt;

const Doctor = require('../models/doctor');

// Here we are using passport authentication
let opts = {
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: "secrethospitalkey"
}


passport.use(new JWTStrategy(opts, function(jwtPayLoad, done){

Doctor.findById(jwtPayLoad._id, function(err, user){
if (err){
console.log('Error in finding user from JWT');
return done(err,false);}

if (user){
return done(null, user);
}else{
return done(null, false);
}
})

}));

module.exports = passport;
72 changes: 72 additions & 0 deletions controllers/api/v1/doctor_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const Doctor=require('../../../models/doctor');
const jwt=require('jsonwebtoken');


//Register the doctor in app
module.exports.register = async function(req,res) {
try {

const doctor= await Doctor.create(req.body);

return res.status(200).json({
success: true,
message:doctor
});

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

//Doctor Login
module.exports.login= async (req, res)=>{
try {

let { email, password } = req.body;

if (!email || !password) {
return res.status(400).json({
success: false,
msg:'No email or password'
});
}

let doctor = await Doctor.findOne({ email: email });
if (!doctor) {
return res.status(401).json({
success: false,
msg: "Invalid Username or Password!"
});
}

// Check if password matches // we are calling function from Doctor model bcrypt function.
const isMatch = await doctor.matchPassword(password);
// Error handling if invalid password
if (!isMatch) {
return res.status(401).json({
success: false,
msg: "Invalid Username or Password!"
});
}

// Get JWT token
const token = doctor.getSignedJwtToken();

// Return response
res.status(200).json({
success: true,
token,
msg: `Log In Sucessful! Keep the Token safely ${doctor.username}!`
});

} catch (error) {
console.log(error);
res.status(400).json({
success: false,
msg:'Error Occoured!'
});
}
}
45 changes: 45 additions & 0 deletions controllers/api/v1/patient_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const Patient = require('../../../models/patient');
const Report = require('../../../models/report');

exports.register= async (req, res) => {

const doctor =req.doctor._id; // get the doctor id


try {
const { name, phone } = req.body; //destructure the name and phone from body
let patient;
patient = await Patient.find({
phone
});

//if there is patient success if not then create
if (patient.length > 0) {
return res.status(200).json({
success: true,
body: patient[0]
});
}


patient = await Patient.create({
name,
phone,
doctor
});
// Return response
return res.status(201).json({
success: true,
body: patient,
msg:'Patient Registered Sucessfully!'
});
} catch (err) {
// Error handling
return res.status(401).json({
success: false,
msg:'Error Occoured!'
});
}
};


68 changes: 68 additions & 0 deletions controllers/api/v1/report_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const Patient = require('../../../models/patient');
const Doctor = require('../../../models/doctor');
const Report = require('../../../models/report');

module.exports.create_report= async function(req,res){


console.log("Inside report controller");

const doctor =req.doctor._id;
console.log("Dr:"+ doctor);

try{
console.log("Inside try");

const report = await Report.create({
doctor:doctor,
patient:req.params.id,
status:req.body.status
});

return res.status(200).json({
success:true
});
}
catch (err) {
// Error handling
return res.status(401).json({
success: false,
msg:err.message,
});
}
}

//find patient with id and send report
module.exports.all_reports= async function(req,res){
try{
const reports = Report.find({ "patient": req.params.id });
reports.exec(function (err, report) {
return res.send(report);
})
}
catch (err) {
// Error handling
return res.status(401).json({
success: false,
msg:err.message,
});
}

}

//send report by status
module.exports.report_by_status = async (req,res) => {

try {
const reports = Report.find({ "status": req.params.status });
reports.exec(function (err, rep) {
return res.send(rep);
});

} catch (err) {
return res.status(500).json({
message: err.message
});
}

}
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require('express');
const port = process.env.PORT || 5000;
const db = require('./config/mongoose');

const passport = require('passport');
const passportJWT = require('./config/passport_jwt_strategy');

const app = express();

app.use(express.json());
app.use(express.urlencoded({
extended: true
}));

//redirecting routes
app.use('/', require('./routes'));

app.listen(port, function (err) {
if (err) { console.log('error'); return; }

console.log(`server is running on ${port}`);
});
Loading

0 comments on commit ba20b2f

Please sign in to comment.