-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
235 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// login / register hospitals | ||
import asyncHandler from "express-async-handler"; | ||
import generateToken from "../utils/generateToken.js"; | ||
import Hospital from "../models/hospitalModel.js"; | ||
import axios from "axios"; | ||
|
||
const authUser = asyncHandler(async (req, res) => { | ||
const { email, password } = req.body; | ||
|
||
const user = await User.findOne({ email }); | ||
|
||
if (user && (await user.matchPassword(password))) { | ||
res.json({ | ||
_id: user._id, | ||
name: user.name, | ||
email: user.email, | ||
isAdmin: user.isAdmin, | ||
token: generateToken(user._id), | ||
}); | ||
} else { | ||
res.status(401); | ||
throw new Error("Invalid Email or password"); | ||
} | ||
}); | ||
|
||
const registerHospital = asyncHandler(async (req, res) => { | ||
const data = await axios.get(`http://ip-api.com/json`); | ||
const lat = data.data.lat; | ||
const lon = data.data.lon; | ||
const { name, email, password, contactNo, address, availability } = req.body; | ||
|
||
const userExist = await Hospital.findOne({ email }); | ||
|
||
const user = await Hospital.create({ | ||
name, | ||
email, | ||
password, | ||
contactNo, | ||
address, | ||
ambulance: { | ||
availability, | ||
location: { | ||
lat, | ||
lon, | ||
}, | ||
}, | ||
}); | ||
if (userExist) { | ||
res.status(400); | ||
throw new Error("User Already exists!"); | ||
} else { | ||
res.json({ | ||
id: user._id, | ||
name: user.name, | ||
email: user.email, | ||
contactNo: user.contactNo, | ||
address: user.address, | ||
ambulance: user.ambulance, | ||
availability: user.availability, | ||
token: generateToken(user._id), | ||
}); | ||
} | ||
}); | ||
|
||
const getHospitals = asyncHandler(async (req, res) => { | ||
const user = await Hospital.find({}); | ||
res.json(user); | ||
}); | ||
|
||
export { registerHospital, getHospitals }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import mongoose from "mongoose"; | ||
import bcrypt from "bcryptjs"; | ||
|
||
const hospitalSchema = mongoose.Schema( | ||
{ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
address: { | ||
type: String, | ||
required: true, | ||
}, | ||
contactNo: { | ||
type: Number, | ||
required: true, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
}, | ||
ambulance: [ | ||
{ | ||
availabilty: { | ||
type: Boolean, | ||
required: false, | ||
}, | ||
location: { | ||
lat: { | ||
type: String, | ||
required: false, | ||
}, | ||
lon: { | ||
type: String, | ||
required: false, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
hospitalSchema.methods.matchPassword = async function (enteredPassword) { | ||
return await bcrypt.compare(enteredPassword, this.password); | ||
}; | ||
|
||
hospitalSchema.pre("save", async function (next) { | ||
if (!this.isModified("password")) { | ||
next(); | ||
} | ||
|
||
const salt = await bcrypt.genSalt(10); | ||
this.password = await bcrypt.hash(this.password, salt); | ||
}); | ||
|
||
const Hospital = mongoose.model("Hospital", hospitalSchema); | ||
export default Hospital; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from "express"; | ||
import { | ||
getHospitals, | ||
registerHospital, | ||
} from "../controllers/hospitalController.js"; | ||
import { protect } from "../middleware/authMiddleware.js"; | ||
|
||
const router = express.Router(); | ||
router.route("/").post(registerHospital).get(getHospitals); | ||
// router.post("/login", authUser); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters