Skip to content

Commit

Permalink
--update: try-catch to error handler higher order function
Browse files Browse the repository at this point in the history
  • Loading branch information
ritiksr25 committed Dec 30, 2020
1 parent e2c05ad commit 39db634
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 401 deletions.
32 changes: 32 additions & 0 deletions config/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { sendError } = require("../utility/helpers");
const { NOT_FOUND, SERVER_ERROR } = require("../utility/statusCodes");
const { logger } = require("../utility/helpers");

module.exports.catchErrors = middlewareFunction => {
return async (req, res, next) => {
try {
await middlewareFunction(req, res, next);
} catch (err) {
next(err);
}
};
};

// not found routes
module.exports.notFound = (req, res) => {
sendError(res, "Welcome to API!! This route does not exist", NOT_FOUND);
};

module.exports.sendErrors = (err, req, res, next) => {
//logging error for backend console
console.log(err);
logger("error", "app", {
message: err.message,
status: err.status || SERVER_ERROR,
stack: err.stack,
path: req.originalUrl,
user: req.user ? ({ id, name, email, role } = req.user) : undefined
});
//sending to frontend
sendError(res, "Oops! Something went wrong.", err.status || SERVER_ERROR);
};
122 changes: 49 additions & 73 deletions controllers/admin_controller.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,75 @@
module.exports.admin = async (req, res) => {
let totalDonations = 0,
totalAmount = 0;
try {
let totalVolunteers = await Volunteer.countDocuments();
let totalBloodDonors = await BloodDonor.countDocuments();
let totalBloodRequests = await BloodRequest.countDocuments();
let donations = await Donation.find({ status: "success" }).sort({
updatedAt: "desc"
});
donations.map(donation => {
totalDonations++;
totalAmount += donation.finalAmount;
});
res.status(200).json({
message: "success",
totalBloodDonors,
totalBloodRequests,
totalVolunteers,
totalAmount,
totalDonations
});
} catch (err) {
res.status(500).json({ message: err.message, error: true, data: null });
}
let totalVolunteers = await Volunteer.countDocuments();
let totalBloodDonors = await BloodDonor.countDocuments();
let totalBloodRequests = await BloodRequest.countDocuments();
let donations = await Donation.find({ status: "success" }).sort({
updatedAt: "desc"
});
donations.map(donation => {
totalDonations++;
totalAmount += donation.finalAmount;
});
res.status(200).json({
message: "success",
totalBloodDonors,
totalBloodRequests,
totalVolunteers,
totalAmount,
totalDonations
});
};

module.exports.volunteers = async (req, res) => {
try {
let volunteers = await Volunteer.find().sort({ createdAt: "desc" });
res.status(200).json({ message: "success", volunteers });
} catch (err) {
res.status(500).json({ message: err.message, error: true, data: null });
}
let volunteers = await Volunteer.find().sort({ createdAt: "desc" });
res.status(200).json({ message: "success", volunteers });
};

module.exports.donors = async (req, res) => {
let { bloodGroup } = req.body;
let donors;
try {
if (bloodGroup) {
donors = await BloodDonor.find({ bloodGroup }).sort({
createdAt: "desc"
});
} else {
donors = await BloodDonor.find().sort({ createdAt: "desc" });
}

res.status(200).json({ message: "success", donors });
} catch (err) {
res.status(500).json({ message: err.message, error: true, data: null });
if (bloodGroup) {
donors = await BloodDonor.find({ bloodGroup }).sort({
createdAt: "desc"
});
} else {
donors = await BloodDonor.find().sort({ createdAt: "desc" });
}

res.status(200).json({ message: "success", donors });
};

module.exports.bloodRequests = async (req, res) => {
try {
let requests = await BloodRequest.find().sort({ createdAt: "desc" });
res.status(200).json({ message: "success", requests });
} catch (err) {
res.status(500).json({ message: err.message, error: true, data: null });
}
let requests = await BloodRequest.find().sort({ createdAt: "desc" });
res.status(200).json({ message: "success", requests });
};

module.exports.messages = async (req, res) => {
try {
let messages = await Message.find().sort({ createdAt: "desc" });
res.status(200).json({ messgae: "success", messages });
} catch (err) {
res.status(500).json({ message: err.message, error: true, data: null });
}
let messages = await Message.find().sort({ createdAt: "desc" });
res.status(200).json({ messgae: "success", messages });
};

module.exports.donations = async (req, res) => {
let totalDonations = 0,
totalAmount = 0,
totalCharges = 0,
totalDonationAmount = 0;
try {
let donations = await Donation.find({ status: "success" }).sort({
updatedAt: "desc"
});
donations.map(donation => {
totalDonations++;
totalAmount += donation.finalAmount;
totalCharges += donation.charges;
totalDonationAmount += donation.amount;
});
res.status(200).json({
message: "success",
donations,
totalDonations,
totalCharges,
totalDonationAmount,
totalAmount
});
} catch (err) {
res.status(500).json({ message: err.message, error: true, data: null });
}
let donations = await Donation.find({ status: "success" }).sort({
updatedAt: "desc"
});
donations.map(donation => {
totalDonations++;
totalAmount += donation.finalAmount;
totalCharges += donation.charges;
totalDonationAmount += donation.amount;
});
res.status(200).json({
message: "success",
donations,
totalDonations,
totalCharges,
totalDonationAmount,
totalAmount
});
};
28 changes: 10 additions & 18 deletions controllers/bloodPortal_controller.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
module.exports.registerDonor = async (req, res) => {
let { name, bloodGroup, phone, email, dob, address, hasDonated } = req.body;
try {
let donor = await BloodDonor.findOne({ $and: [{ email }, { phone }] });
if (donor) {
res.status(400).json({
message: "phone or email already in use."
});
} else {
await BloodDonor.create(req.body);
res.status(200).json({ message: "success" });
}
} catch (err) {
res.status(500).json({ message: err.message, error: true });
let donor = await BloodDonor.findOne({ $and: [{ email }, { phone }] });
if (donor) {
res.status(400).json({
message: "phone or email already in use."
});
} else {
await BloodDonor.create(req.body);
res.status(200).json({ message: "success" });
}
};

Expand All @@ -30,10 +26,6 @@ module.exports.requestBlood = async (req, res) => {
neededBy,
timings
} = req.body;
try {
await BloodRequest.create(req.body);
res.status(200).json({ message: "success" });
} catch (err) {
res.status(500).json({ message: err.message, error: true });
}
await BloodRequest.create(req.body);
res.status(200).json({ message: "success" });
};
79 changes: 35 additions & 44 deletions controllers/donation_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,26 @@ require("dotenv").config();

module.exports.createOrder = async (req, res) => {
let { name, email, phone, address, amount, message } = req.body;
try {
amount = Math.round(Number(amount) * 100) / 100;
let order = await createRzpOrder(amount, email);
let donation = await Donation.findOne({ order: order.id });
if (!donation) {
let newDonation = {
name,
email,
phone,
address,
amount,
message,
order: order.id
};
await Donation.create(newDonation);
}
res.status(200).json({
message: "success",
order,
key: process.env.RZP_KEY_ID
});
} catch (err) {
res.status(500).json({ message: err.message, error: true, data: null });
amount = Math.round(Number(amount) * 100) / 100;
let order = await createRzpOrder(amount, email);
let donation = await Donation.findOne({ order: order.id });
if (!donation) {
let newDonation = {
name,
email,
phone,
address,
amount,
message,
order: order.id
};
await Donation.create(newDonation);
}
res.status(200).json({
message: "success",
order,
key: process.env.RZP_KEY_ID
});
};

module.exports.payment = async (req, res) => {
Expand All @@ -36,26 +32,21 @@ module.exports.payment = async (req, res) => {
razorpay_order_id,
razorpay_signature
} = req.body;
try {
if (razorpay_payment_id) {
let donation = await Donation.findOne({
order: razorpay_order_id
});
let charges = Math.round(0.0233 * donation.amount * 100) / 100;
let finalAmount =
Math.round((donation.amount - charges) * 100) / 100;
// payment details updation
donation.status = "success";
donation.charges = charges;
donation.finalAmount = finalAmount;
donation.transactionId = razorpay_payment_id;
donation.signature = razorpay_signature;
donation = await donation.save();
res.status(200).json({ message: "success", donation });
} else {
res.status(500).json({ message: "Payment failed!!" });
}
} catch (err) {
res.status(500).json({ message: err.message, error: true, data: null });
if (razorpay_payment_id) {
let donation = await Donation.findOne({
order: razorpay_order_id
});
let charges = Math.round(0.0233 * donation.amount * 100) / 100;
let finalAmount = Math.round((donation.amount - charges) * 100) / 100;
// payment details updation
donation.status = "success";
donation.charges = charges;
donation.finalAmount = finalAmount;
donation.transactionId = razorpay_payment_id;
donation.signature = razorpay_signature;
donation = await donation.save();
res.status(200).json({ message: "success", donation });
} else {
res.status(500).json({ message: "Payment failed!!" });
}
};
48 changes: 17 additions & 31 deletions controllers/index_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,29 @@ module.exports.welcome = (req, res) => {
};

module.exports.index = async (req, res) => {
try {
let totalProjects = await Project.countDocuments();
let projects = await Project.find()
.sort({ updatedAt: "desc" })
.limit(3);
res.status(200).json({
message: "success",
totalProjects,
projects
});
} catch (err) {
res.status(500).json({ message: err.message, error: true });
}
let totalProjects = await Project.countDocuments();
let projects = await Project.find().sort({ updatedAt: "desc" }).limit(3);
res.status(200).json({
message: "success",
totalProjects,
projects
});
};

module.exports.volunteer = async (req, res) => {
try {
let volunteer = await Volunteer.findOne({ email: req.body.email });
if (volunteer) {
res.status(400).json({
message: "You have already filled the form"
});
} else {
await Volunteer.create(req.body);
res.status(200).json({ message: "success" });
}
} catch (err) {
res.status(500).json({ message: err.message, error: true });
let volunteer = await Volunteer.findOne({ email: req.body.email });
if (volunteer) {
res.status(400).json({
message: "You have already filled the form"
});
} else {
await Volunteer.create(req.body);
res.status(200).json({ message: "success" });
}
};

module.exports.contact = async (req, res) => {
let { name, email, phone, message } = req.body;
try {
await Message.create(req.body);
res.status(200).json({ message: "success" });
} catch (err) {
res.status(500).json({ message: err.message, error: true });
}
await Message.create(req.body);
res.status(200).json({ message: "success" });
};
Loading

0 comments on commit 39db634

Please sign in to comment.