Skip to content

Commit

Permalink
Merge pull request #12 from BuildForSDGCohort2/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
udofia2 authored Sep 26, 2020
2 parents ea4b34b + fe55e18 commit 42abf71
Show file tree
Hide file tree
Showing 21 changed files with 690 additions and 298 deletions.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node --optimize_for_size --max_old_space_size=460 --gc_interval=100 app.js
82 changes: 68 additions & 14 deletions api/controller/citizen.Controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const Offenses = require("./../model/offense.model");

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({});
const citizens = await Citizens.find({}).populate("offenses");
res.status(200).json({
totalCitizens: citizens.length,
citizens: citizens.map((citizen) => {
Expand All @@ -24,6 +26,12 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {
description:
"Follow the provided url to make a registration. If you are using postman to, the request will be a post request",
},
"Edit Citizen details": {
type: "PATCH",
url: `http://localhost:3000/api/v1/citizen/edit/${citizen._id}`,
description:
"Citizen profile can be edited and updated on the profile page. If you are using postman to, the request will be a post request",
},
Login: {
type: "POST",
url: "http://localhost:3000/api/v1/citizen/login",
Expand All @@ -47,7 +55,7 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {
* @desc route to register a citizen
* @access public( Every one can access)
*/
const register = async (req, res) => {
const register = async (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
Expand All @@ -72,12 +80,14 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {
if (user) return res.status(400).json(`${email} is already in use`);

const citizen = new Citizens({
// _id: mongoose.Types.ObjectId,
name,
email,
password,
passport,
nationality,
periodOfResidence,
profileImage: req.file.path,
});

const salt = await bcrypt.genSalt(10);
Expand All @@ -86,6 +96,8 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {

await citizen.save();

console.log(citizen);

res.status(201).json({
msg: `${citizen.name.first} ${citizen.name.last} is successfully registered`,
request: {
Expand Down Expand Up @@ -118,6 +130,8 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {

const user = await Citizens.findOne({ email });

console.log(user);

if (!user)
return res.status(401).json({
msg: `Invalid Credentials`,
Expand Down Expand Up @@ -151,26 +165,27 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {
user: user._id,
};
const token = jwt.sign(payload, mySecrete, { expiresIn: "1hr" });
const heads = await res.setHeader("x-auth-header", token);

res.json({
msg: "you are now logged in",
token,
heads,
});
} catch (err) {
res.status(500).json(err);
}
};


/**
* @param GET /api/v1/citizen/profile/:id
* @desc displays citizens dashboard
* @access public( only signed in citizens can access)
*/
const profile = async (req, res) => {
const citizen = await Citizens.findOne({_id: req.params.id})
res.json(citizen);
const citizen = await Citizens.findOne({ _id: req.params.id });

const offense = await Offenses.find({ citizen: req.params.id });
// console.log(offense)
res.json({ citizenID: citizen, offense });
};

/**
Expand All @@ -179,15 +194,26 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {
* @access protected( only logged in citizen can access)
*/
const update = async (req, res) => {
const citizen = await Citizens.findByIdAndUpdate(req.params.id, req.body)
const file = req.file;

const citizen = await Citizens.findOne({ _id: req.params.id });

if (file)
await citizen.updateOne({ $set: { profileImage: req.file.path } });

await citizen.updateOne({ $set: { password } });
console.log(citizen);
// const citizen = await Citizens.findByIdAndUpdate(req.params.id, {
// password,
// file
// });
res.json({
msg: "citizen had been edited, your profile is now updated.",
citizen
})
citizen,
});
};


/**
/**
* @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)
Expand All @@ -213,7 +239,6 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {
});
};


/**
* @param POST /api/v1/citizen/logout
* @desc citizen can logout of the platform
Expand All @@ -223,14 +248,43 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {
res.json("citizen can logout");
};

/**
* @param POST /api/v1/citizen/password/reset
* @desc citizen can logout of the platform
* @access protected( only logged in citizen can access)
*/
const resetPassword = async (req, res) => {
let { email, newPassword } = req.body;

const citizen = await Citizens.findOne({ email });

if (!citizen) return res.json("please register");

if (!newPassword)
return res.json({
msg: "provide a new password",
});

const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(newPassword, salt);
newPassword = hash;

await citizen.updateOne({ $set: { password: newPassword } });
res.json({
msg: "password reset successful",
citizen,
});
};

return {
deltCitizen,
citizens,
register,
login,
logout,
profile,
update
update,
resetPassword,
};
};

Expand Down
10 changes: 10 additions & 0 deletions api/controller/offense.Controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Offenses = require("./../model/offense.model");
module.exports.offenses = async (req, res) => {
const offense = await Offenses.find({}).populate("citizen");
res.json(offense);
};

module.exports.offense = async (req, res) => {
const offense = await Offenses.findById(req.params.id);
res.json(offense);
};
Loading

0 comments on commit 42abf71

Please sign in to comment.