-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from BuildForSDGCohort2/Dev
Dev
- Loading branch information
Showing
10 changed files
with
2,324 additions
and
1 deletion.
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,2 @@ | ||
node_modules | ||
default.js |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# TEAM-050-Backend | ||
|
||
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/2fe1c96bb8af4f7eae751c4dfe431187)](https://app.codacy.com/gh/BuildForSDGCohort2/TEAM-050-Backend?utm_source=github.com&utm_medium=referral&utm_content=BuildForSDGCohort2/TEAM-050-Backend&utm_campaign=Badge_Grade_Settings) | ||
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/2fe1c96bb8af4f7eae751c4dfe431187)](https://app.codacy.com/gh/BuildForSDGCohort2/TEAM-050-Backend?utm_source=github.com&utm_medium=referral&utm_content=BuildForSDGCohort2/TEAM-050-Backend&utm_campaign=Badge_Grade_Settings) |
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,219 @@ | ||
const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => { | ||
/** | ||
* @param GET /api/v1/citizen | ||
* @desc displays all the registered citizens on the platform | ||
* @access public( Every one can access) | ||
*/ | ||
const citizens = async (req, res) => { | ||
const citizens = await Citizens.find({}); | ||
res.status(200).json({ | ||
totalCitizens: citizens.length, | ||
citizens: citizens.map((citizen) => { | ||
return { | ||
citizen, | ||
request: { | ||
"view Citizen": { | ||
type: "GET", | ||
url: `http://localhost:3000/api/v1/citizen/profile/${citizen._id}`, | ||
description: | ||
"Click on the url to view all the detail about this citizen", | ||
}, | ||
"Register New Citizen": { | ||
type: "POST", | ||
url: "http://localhost:3000/api/v1/citizen/register", | ||
description: | ||
"Follow the provided url to make a registration. If you are using postman to, the request will be a post request", | ||
}, | ||
Login: { | ||
type: "POST", | ||
url: "http://localhost:3000/api/v1/citizen/login", | ||
description: | ||
"Registered citizens can follow the provided url to login to their profile page. If you are using postman to, the request will be a post request", | ||
}, | ||
"Delete citizen": { | ||
type: "DELETE", | ||
url: `http://localhost:3000/api/v1/citizen/delete/${citizen._id}`, | ||
description: | ||
"Registered citizens can follow the provided url to login to their profile page. If you are using postman to, the request will be a post request", | ||
}, | ||
}, | ||
}; | ||
}), | ||
}); | ||
}; | ||
|
||
/** | ||
* @param POST /api/v1/citizen/register | ||
* @desc route to register a citizen | ||
* @access public( Every one can access) | ||
*/ | ||
const register = async (req, res) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
|
||
try { | ||
const { | ||
name, | ||
email, | ||
password, | ||
// POB, | ||
nationality, | ||
passport, | ||
fingerPrint, | ||
periodOfResidence, | ||
age, | ||
passportPages, | ||
} = req.body; | ||
|
||
const user = await Citizens.findOne({ email }); | ||
|
||
if (user) return res.status(400).json(`${email} is already in use`); | ||
|
||
const citizen = new Citizens({ | ||
name, | ||
email, | ||
password, | ||
passport, | ||
nationality, | ||
periodOfResidence, | ||
}); | ||
|
||
const salt = await bcrypt.genSalt(10); | ||
const hash = await bcrypt.hash(password, salt); | ||
citizen.password = hash; | ||
|
||
await citizen.save(); | ||
|
||
res.status(201).json({ | ||
msg: `${citizen.name.first} ${citizen.name.last} is successfully registered`, | ||
request: { | ||
Login: { | ||
type: "POST", | ||
url: "http://localhost:3000/api/v1/citizen/login", | ||
description: | ||
"Registered citizens can follow the provided url to login to their profile page. If you are using postman to, the request will be a post request", | ||
}, | ||
}, | ||
}); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
/** | ||
* @param POST /api/v1/citizen/login | ||
* @desc route for citizens to signin on the platform | ||
* @access public( Every one can access) | ||
*/ | ||
const login = async (req, res) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
|
||
try { | ||
const { email, password } = req.body; | ||
|
||
const user = await Citizens.findOne({ email }); | ||
|
||
if (!user) | ||
return res.status(401).json({ | ||
msg: `Invalid Credentials`, | ||
request: { | ||
Register: { | ||
type: "POST", | ||
url: "http://localhost:3000/api/v1/citizen/register", | ||
description: | ||
"Follow the provided url to make a registration. If you are using postman to, the request will be a post request", | ||
}, | ||
}, | ||
}); | ||
|
||
const isMatch = await bcrypt.compare(password, user.password); | ||
console.log(isMatch); | ||
|
||
if (!isMatch) | ||
return res.status(401).json({ | ||
msg: `Invalid Credentials`, | ||
request: { | ||
Register: { | ||
type: "POST", | ||
url: "http://localhost:3000/api/v1/citizen/register", | ||
description: | ||
"Follow the provided url to make a registration. If you are using postman to, the request will be a post request", | ||
}, | ||
}, | ||
}); | ||
|
||
const payload = { | ||
user: user._id, | ||
}; | ||
const token = jwt.sign(payload, mySecrete, { expiresIn: "1hr" }); | ||
const heads = await res.setHeader("x-auth-header", token); | ||
|
||
res.json({ | ||
token, | ||
heads, | ||
}); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
} | ||
}; | ||
|
||
/** | ||
* @param DELETE /api/v1/citizen/delete/:id | ||
* @desc gives citizen the ability to delete their account from the platform | ||
* @access protected( only signed in citizens and admin can access this route) | ||
*/ | ||
const deltCitizen = async (req, res) => { | ||
const citizen = await Citizens.findByIdAndDelete(req.params.id); | ||
res.status(200).json({ | ||
msg: `${citizen.name.first} ${citizen.name.last} with the id ${citizen._id} is successfully deleted from the database`, | ||
request: { | ||
Register: { | ||
type: "POST", | ||
url: "http://localhost:3000/api/v1/citizen/register", | ||
description: | ||
"Follow the provided url to make a registration. If you are using postman to, the request will be a post request", | ||
}, | ||
Login: { | ||
type: "POST", | ||
url: "http://localhost:3000/api/v1/citizen/login", | ||
description: | ||
"Registered citizens can follow the provided url to login to their profile page. If you are using postman to, the request will be a post request", | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
/** | ||
* @param GET /api/v1/citizen/profile/:id | ||
* @desc displays citizens dashboard | ||
* @access public( only signed in citizens can access) | ||
*/ | ||
const profile = async (req, res) => { | ||
res.json("citizen can view profile"); | ||
}; | ||
|
||
/** | ||
* @param POST /api/v1/citizen/logout | ||
* @desc citizen can logout of the platform | ||
* @access protected( only logged in citizen can access) | ||
*/ | ||
const logout = async (req, res) => { | ||
res.json("citizen can logout"); | ||
}; | ||
|
||
return { | ||
deltCitizen, | ||
citizens, | ||
register, | ||
login, | ||
logout, | ||
profile, | ||
}; | ||
}; | ||
|
||
module.exports = citizenActions; |
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,19 @@ | ||
const { validationResult } = require("express-validator"); | ||
|
||
const validation = (body) => { | ||
const regForm = [ | ||
body("email").isEmail(), | ||
body("password").isLength({ min: 5 }), | ||
]; | ||
|
||
const loginForm = [ | ||
body("email").isEmail(), | ||
body("password").not().notEmpty(), | ||
]; | ||
return { | ||
regForm, | ||
loginForm, | ||
}; | ||
}; | ||
|
||
module.exports = validation; |
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,90 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const { Schema } = mongoose; | ||
|
||
const citizenSchema = new Schema({ | ||
name: { | ||
first: { | ||
type: String, | ||
required: true, | ||
}, | ||
last: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
}, | ||
// POB: { | ||
// type: Date, | ||
// required: true, | ||
// }, | ||
nationality: { | ||
type: String, | ||
required: true, | ||
}, | ||
passport: { | ||
number: { | ||
type: Number, | ||
require: true, | ||
}, | ||
|
||
date: { | ||
type: Date, | ||
required: true, | ||
}, | ||
placeOfIssuance: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
// fingerPrint: { | ||
// type: String, | ||
// }, | ||
// periodOfResidence: { | ||
// type: Date, | ||
// required: true, | ||
// }, | ||
// age: { | ||
// type: String, | ||
// required: true, | ||
// }, | ||
// passportPages: { | ||
// dataPage1: { | ||
// type: String, | ||
// required: true, | ||
// }, | ||
// dataPage2: { | ||
// type: String, | ||
// required: true, | ||
// }, | ||
// dataPage3: { | ||
// type: String, | ||
// required: true, | ||
// }, | ||
// VisasPage: { | ||
// type: String, | ||
// required: true, | ||
// }, | ||
// entryStamp: { | ||
// type: String, | ||
// required: true, | ||
// }, | ||
// departureStamp: { | ||
// type: String, | ||
// required: true, | ||
// }, | ||
// fingerPrint: { | ||
// type: String, | ||
// }, | ||
// }, | ||
}); | ||
|
||
module.exports = mongoose.model("citizen", citizenSchema); |
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,34 @@ | ||
const express = require("express"); | ||
const Citizens = require("./../model/citizen.model"); | ||
const { mySecrete } = require("./../../config/default"); | ||
const jwt = require("jsonwebtoken"); | ||
const bcrypt = require("bcrypt"); | ||
const { body, validationResult } = require("express-validator"); | ||
const { regForm, loginForm } = require("./../middleware/formValidation")(body); | ||
const { | ||
register, | ||
login, | ||
logout, | ||
profile, | ||
citizens, | ||
deltCitizen, | ||
} = require("./../controller/citizen.Controller")( | ||
Citizens, | ||
bcrypt, | ||
mySecrete, | ||
jwt, | ||
validationResult | ||
); | ||
|
||
const { Router } = express; | ||
|
||
const citizenRouter = Router(); | ||
|
||
citizenRouter.route("/register").post(register); | ||
citizenRouter.route("/login").post(loginForm, login); | ||
citizenRouter.route("/logout").post(logout); | ||
citizenRouter.route("/").get(citizens); | ||
citizenRouter.route("/profile/:id").get(profile); | ||
citizenRouter.route("/delete/:id").delete(deltCitizen); | ||
|
||
module.exports = citizenRouter; |
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,13 @@ | ||
const express = require("express"); | ||
const bodyParser = require("body-parser"); | ||
const { PORT, url } = require("./config/default"); | ||
require("./config/db")(); | ||
|
||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
app.use("/api/v1/citizen", require("./api/routes/citizen.route")); | ||
|
||
app.listen(PORT, () => console.log(`Server is running on ${url}:${PORT}`)); |
Oops, something went wrong.