-
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.
- Loading branch information
Showing
9 changed files
with
888 additions
and
360 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,129 @@ | ||
const db = require("../../database/index"); | ||
const generateId = require("../../utils/utils").generateId; | ||
const constants = require("../../utils/constants"); | ||
const {validationResult} = require("express-validator/check"); | ||
const { validationResult } = require("express-validator/check"); | ||
|
||
module.exports = { | ||
getBloodForm: (req, res) => { | ||
//CHECK ERROR INPUT | ||
let errors = validationResult(req); | ||
if (!errors.isEmpty()) return res.status(422).json({errors: errors.array()}); | ||
getBloodForm: (req, res) => { | ||
//CHECK ERROR INPUT | ||
let errors = validationResult(req); | ||
if (!errors.isEmpty()) | ||
return res.status(422).json({ errors: errors.array() }); | ||
|
||
//CHECK USER DATA | ||
console.log("bloodFormController | getBloodForm | req user data: ", req.userData); | ||
if (req.userData.role !== constants.role.donor) return res.status(403).json({error: "Forbidden !! You are not allowed to call this function"}); | ||
//CHECK USER DATA | ||
console.log( | ||
"bloodFormController | getBloodForm | req user data: ", | ||
req.userData | ||
); | ||
if (req.userData.role !== constants.role.donor) | ||
return res | ||
.status(403) | ||
.json({ | ||
error: "Forbidden !! You are not allowed to call this function", | ||
}); | ||
|
||
//EVERYTHING IS OK | ||
db.query("select * from blood where donor_id = ?", [req.userData.id], function (err, result) { | ||
//CHECK SQL ERROR | ||
if (err) return res.status(500).json({error: err}) | ||
//EVERYTHING IS OK | ||
db.query( | ||
"select * from blood where donor_id = ?", | ||
[req.userData.id], | ||
function (err, result) { | ||
//CHECK SQL ERROR | ||
if (err) return res.status(500).json({ error: err }); | ||
|
||
//RETURN DATA | ||
return res.status(200).json({message: "success", data: result}) | ||
}) | ||
//RETURN DATA | ||
return res.status(200).json({ message: "success", data: result }); | ||
} | ||
); | ||
}, | ||
postBloodForm: (req, res) => { | ||
//CHECK ERROR INPUT | ||
let errors = validationResult(req); | ||
if (!errors.isEmpty()) | ||
return res.status(422).json({ errors: errors.array() }); | ||
|
||
}, | ||
postBloodForm: (req, res) => { | ||
//CHECK ERROR INPUT | ||
let errors = validationResult(req); | ||
if (!errors.isEmpty()) return res.status(422).json({errors: errors.array()}); | ||
//CHECK USER DATA | ||
console.log( | ||
"bloodFormController | postBloodForm | req user data: ", | ||
req.userData | ||
); | ||
if (req.userData.role !== constants.role.donor) | ||
return res | ||
.status(403) | ||
.json({ | ||
error: "Forbidden !! You are not allowed to call this function", | ||
}); | ||
|
||
//CHECK USER DATA | ||
console.log("bloodFormController | postBloodForm | req user data: ", req.userData); | ||
if (req.userData.role !== constants.role.donor) return res.status(403).json({error: "Forbidden !! You are not allowed to call this function"}); | ||
//CHECK IF EVENT EXISTS | ||
db.query( | ||
"select * from event where event_id = ?", | ||
[[req.body.event_id]], | ||
function (err, result) { | ||
console.log( | ||
"bloodFormController | postBloodForm | result query event: " + | ||
JSON.stringify(result) | ||
); | ||
//CHECK SQL ERROR | ||
if (err) return res.status(500).json({ error: err }); | ||
if (result.length === 0) | ||
return res.status(423).json({ error: "event does not exists" }); | ||
|
||
//CHECK IF EVENT EXISTS | ||
db.query("select * from event where event_id = ?", [[req.body.event_id]], function (err, result) { | ||
console.log("bloodFormController | postBloodForm | result query event: " + JSON.stringify(result)); | ||
//SAVE EVENT_ID | ||
req.event_id = result[0].event_id; | ||
req.name = result[0].name; | ||
req.event_date = result[0].event_date; | ||
req.location = result[0].location; | ||
|
||
//CHECK IF ALREADY INSERTED REQUEST | ||
db.query( | ||
"select event_id from blood where event_id = ? && donor_id = ?", | ||
[[req.body.event_id], [req.userData.id]], | ||
function (err, result) { | ||
//CHECK SQL ERROR | ||
if (err) return res.status(500).json({error: err}); | ||
if (result.length === 0) return res.status(423).json({error: "event does not exists"}); | ||
if (err) return res.status(500).json({ error: err }); | ||
if (result.length !== 0) | ||
return res | ||
.status(424) | ||
.json({ error: "request for this event already exist" }); | ||
|
||
//SAVE EVENT_ID | ||
req.event_id = result[0].event_id; | ||
req.name = result[0].name; | ||
req.event_date = result[0].event_date; | ||
req.location = result[0].location; | ||
//EVERYTHING IS OK, START INSERT DATA: CREATE DATA TO INSERT | ||
let blood_id = generateId(); | ||
let event_id = req.event_id; | ||
let donor_id = req.userData.id; | ||
let donate_date = Date.now() / 1000; | ||
let amount = constants.standard_blood_donation_amount; | ||
let status = constants.approved; | ||
let event_date = req.event_date; | ||
let name = req.name; | ||
let location = req.location; | ||
let values = [ | ||
blood_id, | ||
event_id, | ||
donor_id, | ||
donate_date, | ||
amount, | ||
status, | ||
event_date, | ||
name, | ||
location, | ||
]; | ||
|
||
//CHECK IF ALREADY INSERTED REQUEST | ||
db.query("select event_id from blood where event_id = ? && donor_id = ?", [[req.body.event_id], [req.userData.id]], function (err, result) { | ||
//RUN SQL TO STORE BLOOD DONATION FORM INTO THE DATABASE | ||
db.query( | ||
"insert into blood(blood_id,event_id,donor_id,donate_date,amount,status,event_date,name,location) values ?", | ||
[[values]], | ||
function (err, result) { | ||
//CHECK SQL ERROR | ||
if (err) return res.status(500).json({error: err}); | ||
if (result.length !== 0) return res.status(424).json({error: "request for this event already exist"}); | ||
|
||
//EVERYTHING IS OK, START INSERT DATA: CREATE DATA TO INSERT | ||
let blood_id = generateId(); | ||
let event_id = req.event_id; | ||
let donor_id = req.userData.id; | ||
let donate_date = Date.now() / 1000; | ||
let amount = constants.standard_blood_donation_amount; | ||
let status = constants.approved; | ||
let event_date = req.event_date; | ||
let name = req.name; | ||
let location = req.location; | ||
let values = [blood_id, event_id, donor_id, donate_date, amount, status, event_date, name, location]; | ||
|
||
//RUN SQL TO STORE BLOOD DONATION FORM INTO THE DATABASE | ||
db.query("insert into blood values ?", [[values]], function (err, result) { | ||
//CHECK SQL ERROR | ||
if (err) return res.status(500).json({error: err}); | ||
return res.status(200).json( | ||
{ | ||
message: "success", | ||
event_id: event_id, | ||
blood_id: blood_id | ||
}); | ||
}) | ||
}); | ||
}); | ||
|
||
|
||
} | ||
}; | ||
if (err) return res.status(500).json({ error: err }); | ||
return res.status(200).json({ | ||
message: "success", | ||
event_id: event_id, | ||
blood_id: blood_id, | ||
}); | ||
} | ||
); | ||
} | ||
); | ||
} | ||
); | ||
}, | ||
}; |
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
Oops, something went wrong.