From 1681e60e31ad6f25d5a9e897a994815d393d6e70 Mon Sep 17 00:00:00 2001 From: ShahanaFarooqui Date: Thu, 22 Nov 2018 20:36:28 -0500 Subject: [PATCH] Fixed Invoices Routes Fixed Invoices Routes --- app.js | 2 ++ controllers/invoices.js | 41 +++++++++++++++++++++++++++++++++++++++++ routes/invoices.js | 8 ++++++++ 3 files changed, 51 insertions(+) create mode 100644 controllers/invoices.js create mode 100644 routes/invoices.js diff --git a/app.js b/app.js index 433b0722..16252d9c 100644 --- a/app.js +++ b/app.js @@ -17,6 +17,7 @@ const payReqRoutes = require("./routes/payReq"); const paymentsRoutes = require("./routes/payments"); const UISettingsRoutes = require("./routes/UISettings"); const LNDSettingsRoutes = require("./routes/lndConfSettings"); +const invoiceRoutes = require("./routes/invoices"); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); @@ -51,6 +52,7 @@ app.use("/api/payreq", payReqRoutes); app.use("/api/payments", paymentsRoutes); app.use("/api/uisettings", UISettingsRoutes); app.use("/api/lndconf", LNDSettingsRoutes); +app.use("/api/invoices", invoiceRoutes); // sending angular application when route doesn't match app.use((req, res, next) => { diff --git a/controllers/invoices.js b/controllers/invoices.js new file mode 100644 index 00000000..c8f9d148 --- /dev/null +++ b/controllers/invoices.js @@ -0,0 +1,41 @@ +var request = require('request'); +var options = require("../connect"); +var common = require('../common'); + +exports.listInvoices = (req, res, next) => { + options.url = common.lnd_server_url + '/invoices'; + request.get(options, (error, response, body) => { + const body_str = (undefined === body) ? '' : JSON.stringify(body); + const search_idx = (undefined === body) ? -1 : body_str.search('Not Found'); + console.log('Information Received: ' + body_str); + if(undefined === body || search_idx > -1 || body.error) { + res.status(500).json({ + message: "Fetching Invoice Info failed!", + error: (undefined === body || search_idx > -1) ? 'Error From Server!' : body.error + }); + } else { + res.status(200).json(body); + } + }); +}; + +exports.addInvoice = (req, res, next) => { + options.url = common.lnd_server_url + '/invoices'; + options.form = JSON.stringify({ + memo: req.body.memo, + value: req.body.amount + }); + console.log('Add Invoice Options Form:' + options.form); + request.post(options, (error, response, body) => { + console.log('Add Invoice Response: '); + console.log(body); + if(undefined === body || body.error) { + res.status(500).json({ + message: "Add Invoice Failed!", + error: (undefined === body) ? 'Error From Server!' : body.error + }); + } else { + res.status(201).json(body); + } + }); +}; diff --git a/routes/invoices.js b/routes/invoices.js new file mode 100644 index 00000000..b165670d --- /dev/null +++ b/routes/invoices.js @@ -0,0 +1,8 @@ +const invoicesController = require("../controllers/invoices"); +const express = require("express"); +const router = express.Router(); + +router.get("/", invoicesController.listInvoices); +router.post("/", invoicesController.addInvoice); + +module.exports = router; \ No newline at end of file