Skip to content

Commit

Permalink
Invoices are now written to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
amingilani committed Aug 17, 2015
1 parent eacc5f8 commit 2b4d064
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions api/controllers/InvoiceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ module.exports = {
message: "'price' is required."
});

// log the failure
// log the failure and drop out of the function
return log.verbose('Invoice unsuccessful', {
missing: "price",
path: req.path,
payload: req.body
});
}

var rate;
// if no currency is sent, assume it is PKR
if (!req.body.currency) {
req.body.currency = "PKR";
}

var rate;
// assign rate the value of the currency called
switch (req.body.currency) {
case 'PKR':
rate = 'ratePKR';
Expand All @@ -45,16 +50,37 @@ module.exports = {
rate = 'ratePKR';
}

// get the rateUSD from memory
redis.get('rateUSD', function(err, rateUSD) {
if (err) log.err("rateUSD fetch unsuccessful", err);
var amount = parseFloat(req.body.price) / rateUSD;
// respond with the amount
res.json({
// get the rate from memory
redis.get(rate, function(err, rateCurrent) {
if (err) log.error("rateUSD fetch unsuccessful", err);
var amount = parseFloat(req.body.price) / rateCurrent;

// create an invoice object
var invoice = {
"price": parseFloat(req.body.price),
"currency": req.body.currency,
"rate": rateCurrent,
"amount": amount
};

// insert the object into the DB
Invoice.create(invoice).exec(function(err, created) {
if (err) {
// log the error
log.error(err);
// reply with a 500 error
res.status(500);
return res.json({
success: false,
message: "Internal server error"
});
}
log.debug("Created invoice: " + created.id);
// respond with the amount
res.json({
"amount": created.amount
});
});
});


}
};

0 comments on commit 2b4d064

Please sign in to comment.