forked from dsckiet/uddeshhya_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--update: optimise and improve models, controllers, validation
- Loading branch information
Showing
14 changed files
with
390 additions
and
430 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 |
---|---|---|
@@ -1,75 +1,84 @@ | ||
const { sendSuccess } = require("../utility/helpers"); | ||
|
||
module.exports.admin = async (req, res) => { | ||
let totalDonations = 0, | ||
totalAmount = 0; | ||
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" | ||
}); | ||
let donations = await Donation.find({ status: "success" }) | ||
.sort({ | ||
updatedAt: "desc" | ||
}) | ||
.lean(); | ||
donations.map(donation => { | ||
totalDonations++; | ||
totalAmount += donation.finalAmount; | ||
}); | ||
res.status(200).json({ | ||
message: "success", | ||
|
||
let data = { | ||
totalBloodDonors, | ||
totalBloodRequests, | ||
totalVolunteers, | ||
totalAmount, | ||
totalDonations | ||
}); | ||
}; | ||
return sendSuccess(res, data); | ||
}; | ||
|
||
module.exports.volunteers = async (req, res) => { | ||
let volunteers = await Volunteer.find().sort({ createdAt: "desc" }); | ||
res.status(200).json({ message: "success", volunteers }); | ||
let volunteers = await Volunteer.find().sort({ createdAt: "desc" }).lean(); | ||
return sendSuccess(res, { volunteers }); | ||
}; | ||
|
||
module.exports.donors = async (req, res) => { | ||
let { bloodGroup } = req.body; | ||
let donors; | ||
if (bloodGroup) { | ||
donors = await BloodDonor.find({ bloodGroup }).sort({ | ||
createdAt: "desc" | ||
}); | ||
donors = await BloodDonor.find({ bloodGroup }) | ||
.sort({ | ||
createdAt: "desc" | ||
}) | ||
.lean(); | ||
} else { | ||
donors = await BloodDonor.find().sort({ createdAt: "desc" }); | ||
donors = await BloodDonor.find().sort({ createdAt: "desc" }).lean(); | ||
} | ||
|
||
res.status(200).json({ message: "success", donors }); | ||
return sendSuccess(res, { donors }); | ||
}; | ||
|
||
module.exports.bloodRequests = async (req, res) => { | ||
let requests = await BloodRequest.find().sort({ createdAt: "desc" }); | ||
res.status(200).json({ message: "success", requests }); | ||
let requests = await BloodRequest.find().sort({ createdAt: "desc" }).lean(); | ||
return sendSuccess(res, { requests }); | ||
}; | ||
|
||
module.exports.messages = async (req, res) => { | ||
let messages = await Message.find().sort({ createdAt: "desc" }); | ||
res.status(200).json({ messgae: "success", messages }); | ||
let messages = await Message.find().sort({ createdAt: "desc" }).lean(); | ||
return sendSuccess(res, { messages }); | ||
}; | ||
|
||
module.exports.donations = async (req, res) => { | ||
let totalDonations = 0, | ||
totalAmount = 0, | ||
totalCharges = 0, | ||
totalDonationAmount = 0; | ||
let donations = await Donation.find({ status: "success" }).sort({ | ||
updatedAt: "desc" | ||
}); | ||
let donations = await Donation.find({ status: "success" }) | ||
.sort({ | ||
updatedAt: "desc" | ||
}) | ||
.lean(); | ||
donations.map(donation => { | ||
totalDonations++; | ||
totalAmount += donation.finalAmount; | ||
totalCharges += donation.charges; | ||
totalDonationAmount += donation.amount; | ||
}); | ||
res.status(200).json({ | ||
message: "success", | ||
let data = { | ||
donations, | ||
totalDonations, | ||
totalCharges, | ||
totalDonationAmount, | ||
totalAmount | ||
}); | ||
}; | ||
return sendSuccess(res, data); | ||
}; |
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
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,35 +1,32 @@ | ||
module.exports.notFound = (req, res) => { | ||
res.status(404).json({ message: "API not found!!" }); | ||
}; | ||
|
||
module.exports.welcome = (req, res) => { | ||
res.status(200).json({ message: "Welcome to UDDESHYA BACKEND API!!" }); | ||
}; | ||
const { sendError, sendSuccess } = require("../utility/helpers"); | ||
const { BAD_REQUEST } = require("../utility/statusCodes"); | ||
|
||
module.exports.index = async (req, res) => { | ||
let totalProjects = await Project.countDocuments(); | ||
let projects = await Project.find().sort({ updatedAt: "desc" }).limit(3); | ||
res.status(200).json({ | ||
message: "success", | ||
totalProjects, | ||
projects | ||
}); | ||
let projects = await Project.find() | ||
.sort({ updatedAt: "desc" }) | ||
.limit(3) | ||
.lean(); | ||
return sendSuccess(res, { totalProjects, projects }); | ||
}; | ||
|
||
module.exports.volunteer = async (req, res) => { | ||
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" }); | ||
} | ||
let volunteer = await Volunteer.findOne({ email: req.body.email }).lean(); | ||
if (volunteer) | ||
return sendError(res, "You have already filled the form.", BAD_REQUEST); | ||
volunteer = await Volunteer.create(req.body); | ||
return sendSuccess(res, { volunteer }); | ||
}; | ||
|
||
module.exports.contact = async (req, res) => { | ||
let { name, email, phone, message } = req.body; | ||
await Message.create(req.body); | ||
res.status(200).json({ message: "success" }); | ||
let message = new Message({ | ||
name, | ||
email, | ||
phone, | ||
message | ||
}); | ||
|
||
message = await message.save(); | ||
return sendSuccess(res, { message }); | ||
}; |
Oops, something went wrong.