Skip to content

Commit

Permalink
Separated middleware function in separate file
Browse files Browse the repository at this point in the history
Aman-Codes committed Jan 6, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c9e9873 commit 66cd546
Showing 11 changed files with 105 additions and 151 deletions.
7 changes: 5 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -14,9 +14,12 @@
},
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"prettier/prettier": ["error", {
"endOfLine":"auto"
}],
"linebreak-style": 0,
"no-console": "off",
"no-underscore-dangle": "off"
"no-underscore-dangle": "off",
"consistent-return": "off"
}
}
21 changes: 9 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
@@ -63,20 +63,17 @@ passport.serializeUser((user, done) => {
});

passport.deserializeUser((id, done) => {
Patient.findById(id, (err, Patient) => {
if (err) return done(err);
if (Patient) return done(null, Patient);
Doctor.findById(id, (error, Doctor) => {
if (error) return done(error);
if (Doctor) return done(null, Doctor);
AmbulanceRegistration.findById(id, (e, AmbulanceRegistration) => {
if (e) return done(e);
if (AmbulanceRegistration) return done(null, AmbulanceRegistration);
return done(e);
Patient.findById(id, (err, patient) => {
if (err) return done(err, null);
if (patient) return done(null, patient);
Doctor.findById(id, (error, doctor) => {
if (error) return done(error, null);
if (doctor) return done(null, doctor);
AmbulanceRegistration.findById(id, (e, ambulanceRegistration) => {
if (e) return done(e, null);
if (ambulanceRegistration) return done(null, ambulanceRegistration);
});
return done(error);
});
return done(err);
});
});

1 change: 0 additions & 1 deletion public/assets/lib/css/style.css
Original file line number Diff line number Diff line change
@@ -5166,7 +5166,6 @@ Contact
display: inline-block;
font-weight: 700;
letter-spacing: 0.5px;
padding: 14px 30px;
border-radius: 0.25rem !important;
background-color: #28a745;
border-color: #28a745;
2 changes: 1 addition & 1 deletion public/assets/production/css/appointmentbooking3.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/assets/production/css/loginRegister.min.css

Large diffs are not rendered by default.

50 changes: 8 additions & 42 deletions routes/ambulance.js
Original file line number Diff line number Diff line change
@@ -2,50 +2,15 @@ const express = require('express');
const passport = require('passport');
const AmbulanceRegistration = require('../models/ambulanceregistration');
const AmbulanceBooking = require('../models/ambulancebooking');
const {
ConvertChosenTime,
convert,
isLoggedIn,
isAuthorizedAmbulance,
} = require('./helper');

const router = express.Router({ mergeParams: true });

// UTILITY FUNCTIONS

function ConvertChosenTime(str) {
const date0 = new Date(str);
let hour = date0.getHours();
const minutes = date0.getMinutes();
let end = 'AM';
if (hour >= 12) {
hour -= 12;
end = 'PM';
}
const JoinedTime = [hour, minutes].join(':');
return `${JoinedTime} ${end}`;
}
function convert(str) {
const date = new Date(str);
const mnth = `0${date.getMonth() + 1}`.slice(-2);
const day = `0${date.getDate()}`.slice(-2);
return [day, mnth, date.getFullYear()].join('-');
}

// MIDDLEWARE FUNCTIONS
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}

return res.redirect('back');
}

function isAuthorizedAmbulance(req, res, next) {
if (
req.isAuthenticated() &&
req.params.ambulanceid.toString() === req.user._id.toString()
) {
return next();
}

return res.redirect('back');
}

// GET ROUTES

router.get('/ambulancelogin', (req, res) => {
@@ -80,9 +45,10 @@ router.get('/ambulancehome', isLoggedIn, (req, res) => {
});
});
AmbulancePromise.then((result) => {
console.log(result);
result.forEach((item) => {
const obj = {};

console.log(item);
obj.PatientId = item.PatientId._id;
obj.PatientName = item.PatientId.name;
obj.AmbulanceId = item.AmbulanceId;
49 changes: 7 additions & 42 deletions routes/doctor.js
Original file line number Diff line number Diff line change
@@ -3,50 +3,15 @@ const passport = require('passport');
const Appointment = require('../models/appointment');
const Prescription = require('../models/prescription');
const Doctor = require('../models/doctor');
const {
ConvertChosenTime,
convert,
isLoggedIn,
isAuthorizedDoctor,
} = require('./helper');

const router = express.Router({ mergeParams: true });

// UTILITY FUNCTIONS

function ConvertChosenTime(str) {
const date0 = new Date(str);
let hour = date0.getHours();
const minutes = date0.getMinutes();
let end = 'AM';
if (hour >= 12) {
hour -= 12;
end = 'PM';
}
const JoinedTime = [hour, minutes].join(':');
return `${JoinedTime} ${end}`;
}
function convert(str) {
const date = new Date(str);
const mnth = `0${date.getMonth() + 1}`.slice(-2);
const day = `0${date.getDate()}`.slice(-2);
return [day, mnth, date.getFullYear()].join('-');
}

// MIDDLEWARE FUNCTIONS

function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}

return res.redirect('back');
}
function isAuthorizedDoctor(req, res, next) {
if (
req.isAuthenticated() &&
req.params.doctorid.toString() === req.user._id.toString()
) {
return next();
}

return res.redirect('back');
}

// GET ROUTES

router.get('/doctorregistration', (req, res) => {
@@ -223,7 +188,7 @@ router.post(

router.get('/doctorhome/:doctorid/edit', isAuthorizedDoctor, (req, res) => {
const ShowDoctorPromise = new Promise((resolve, reject) => {
Doctor.findById(req.params.id).exec((err, item) => {
Doctor.findById(req.params.doctorid).exec((err, item) => {
if (err) {
reject(err);
} else {
64 changes: 64 additions & 0 deletions routes/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
exports.ConvertChosenTime = (str) => {
const date0 = new Date(str);
date0.setHours(date0.getHours() + 5.5);
date0.setMinutes(date0.getMinutes() + 30);
let hour = date0.getHours();
const minutes = date0.getMinutes();
let end = 'AM';
if (hour >= 12) {
hour -= 12;
end = 'PM';
}
const JoinedTime = [hour, minutes].join(':');
return `${JoinedTime} ${end}`;
};

exports.convert = (str) => {
const date0 = new Date(str);
date0.setHours(date0.getHours() + 5);
date0.setMinutes(date0.getMinutes() + 30);
const mnth = `0${date0.getMonth() + 1}`.slice(-2);
const day = `0${date0.getDate()}`.slice(-2);
return [day, mnth, date0.getFullYear()].join('-');
};

exports.isLoggedIn = (req, res, next) => {
if (req.isAuthenticated()) {
return next();
}

return res.redirect('back');
};

exports.isAuthorizedAmbulance = (req, res, next) => {
if (
req.isAuthenticated() &&
req.params.ambulanceid.toString() === req.user._id.toString()
) {
return next();
}

return res.redirect('back');
};

exports.isAuthorizedDoctor = (req, res, next) => {
if (
req.isAuthenticated() &&
req.params.doctorid.toString() === req.user._id.toString()
) {
return next();
}

return res.redirect('back');
};

exports.isAuthorizedPatient = (req, res, next) => {
if (
req.isAuthenticated() &&
req.params.patientid.toString() === req.user._id.toString()
) {
return next();
}

return res.redirect('back');
};
52 changes: 6 additions & 46 deletions routes/patient.js
Original file line number Diff line number Diff line change
@@ -6,55 +6,15 @@ const Appointment = require('../models/appointment');
const Doctor = require('../models/doctor');
const AmbulanceRegistration = require('../models/ambulanceregistration');
const AmbulanceBooking = require('../models/ambulancebooking');
const {
ConvertChosenTime,
convert,
isLoggedIn,
isAuthorizedPatient,
} = require('./helper');

const router = express.Router({ mergeParams: true });

// UTILITY FUNCTIONS

function ConvertChosenTime(str) {
const date0 = new Date(str);
date0.setHours(date0.getHours() + 5.5);
date0.setMinutes(date0.getMinutes() + 30);
let hour = date0.getHours();
const minutes = date0.getMinutes();
let end = 'AM';
if (hour >= 12) {
hour -= 12;
end = 'PM';
}
const JoinedTime = [hour, minutes].join(':');
return `${JoinedTime} ${end}`;
}
function convert(str) {
const date0 = new Date(str);
date0.setHours(date0.getHours() + 5);
date0.setMinutes(date0.getMinutes() + 30);
const mnth = `0${date0.getMonth() + 1}`.slice(-2);
const day = `0${date0.getDate()}`.slice(-2);
return [day, mnth, date0.getFullYear()].join('-');
}

// MIDDLEWARE FUNCTIONS

function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}

return res.redirect('back');
}

function isAuthorizedPatient(req, res, next) {
if (
req.isAuthenticated() &&
req.params.patientid.toString() === req.user._id.toString()
) {
return next();
}

return res.redirect('back');
}

// GET ROUTES

router.get('/patientregistration', (req, res) => {
4 changes: 2 additions & 2 deletions views/ambulancehome.ejs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

<% if (data.NODE_ENV == 'production') { %>
<!-- Production Stylesheets -->
<link rel="stylesheet" href="assets/production/css/about.min.css">
<link rel="stylesheet" href="assets/production/css/loginRegister.min.css">
<% }else { %>
<!-- Development Stylesheets -->
@@ -38,7 +38,7 @@
<h3>Hi <%= data.user.name %>
</h3> <br>

<div class="faq-group headstyle mt-30">
<div class="faq-group headstyle mt-30 contact-form">
<div id="accordion">
<div class="faq-item card">
<div class="faq-header" id="heading1">
4 changes: 2 additions & 2 deletions views/doctorhome.ejs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

<% if (data.NODE_ENV == 'production') { %>
<!-- Production Stylesheets -->
<link rel="stylesheet" href="assets/production/css/about.min.css">
<link rel="stylesheet" href="assets/production/css/loginRegister.min.css">
<% }else { %>
<!-- Development Stylesheets -->
@@ -39,7 +39,7 @@
<h3>Hi <%= data.user.name %>
</h3> <br>

<div class="faq-group headstyle mt-30">
<div class="faq-group headstyle mt-30 contact-form">
<div id="accordion">
<div class="faq-item card">
<div class="faq-header" id="heading1">

0 comments on commit 66cd546

Please sign in to comment.