diff --git a/backend/api/account/accountController.js b/backend/api/account/accountController.js index 433614b..928a4fe 100644 --- a/backend/api/account/accountController.js +++ b/backend/api/account/accountController.js @@ -3,10 +3,9 @@ const accountService = require('./accountService'); const getAccountAllowance = async (req, res) => { try { - const response = await accountService.getAccountAllowance(); - + const username = req.body.username; + const response = await accountService.getAccountAllowance(username); debug.info(`Account Allowance Response: ${JSON.stringify(response)}`); - if (!response.success) res.status(500).json(response); else res.status(200).json(response); @@ -18,10 +17,10 @@ const getAccountAllowance = async (req, res) => { const payInvoice = async (req, res) => { try { - const response = await accountService.getAccountAllowance(); - - debug.info(`Account Allowance Response: ${JSON.stringify(response)}`); + const invoice = req.body.invoice; + const response = await accountService.payInvoice(invoice); + debug.info(`Pay Invoice Response: ${JSON.stringify(response)}`); if (!response.success) res.status(500).json(response); else res.status(200).json(response); @@ -33,13 +32,12 @@ const payInvoice = async (req, res) => { const createInvoice = async (req, res) => { try { - const response = await accountService.getAccountAllowance(); - - debug.info(`Account Allowance Response: ${JSON.stringify(response)}`); - + const amountMillisats = req.body.amountMillisats; + const description = req.body.description; + const response = await accountService.createInvoice(amountMillisats, description); + debug.info(`Create Invoice Response: ${JSON.stringify(response)}`); if (!response.success) res.status(500).json(response); else res.status(200).json(response); - } catch (error) { debug.error(error.stack); res.status(500).json({ message: error.message, error: error.stack }); diff --git a/backend/api/account/accountService.js b/backend/api/account/accountService.js index f94b8ba..16c000a 100644 --- a/backend/api/account/accountService.js +++ b/backend/api/account/accountService.js @@ -5,49 +5,41 @@ const accounts = require('../../db/collection'); const getAccountAllowance = async (username) => { try { // get allowance from firestore - return { success: true, message: charge }; + return { success: true, message: nodes }; } catch (error) { - debug.error(error.stack, error.status, error.message); + debug.error(error.stack); throw new Error(error); } }; -const getAccountBalance = async (username) => { +const getAccountBalance = async () => { try { - - return { success: true, message: charge }; + const response = await senseiNodes.getBalance(); + return { success: true, message: response.balance_satoshis }; } catch (error) { - debug.error(error.stack, error.status, error.message); + debug.error(error.stack); throw new Error(error); } }; -const payInvoice = async (req, res) => { +const payInvoice = async (invoice) => { try { - const invoice = req.body.invoice; const response = await senseiNodes.payInvoice(invoice); - debug.info(`Pay Invoice Response: ${JSON.stringify(response)}`); - if (!response.success) res.status(500).json(response); - else res.status(200).json(response); + return { success: true, message: response }; } catch (error) { debug.error(error.stack); - res.status(500).json({ message: error.message, error: error.stack }); + throw new Error(error); } }; -const createInvoice = async (req, res) => { +const createInvoice = async (amountMillisats, description) => { try { - const amountMillisats = req.body.amountMillisats; - const description = req.body.description; const response = await senseiNodes.createInvoice(amountMillisats, description); - debug.info(`Create Invoice Response: ${JSON.stringify(response)}`); - if (!response.success) res.status(500).json(response); - else res.status(200).json(response); - + return { success: true, message: response }; } catch (error) { debug.error(error.stack); - res.status(500).json({ message: error.message, error: error.stack }); + throw new Error(error); } }; -module.exports = { getAccountAllowance, payInvoice, createInvoice }; +module.exports = { getAccountAllowance, payInvoice, createInvoice, getAccountBalance }; diff --git a/backend/api/admin/adminService.js b/backend/api/admin/adminService.js index 61896b1..dfc852a 100644 --- a/backend/api/admin/adminService.js +++ b/backend/api/admin/adminService.js @@ -8,7 +8,7 @@ const getAllBalances = async (req, res) => { const response = await senseiAdmin.listNodes(); let nodes = []; for (let node of response.nodes) { - node.balance = (await senseiNodes.getBalance(node.username)).balance_satoshis; + node.balance = (await senseiNodes.getBalance()).balance_satoshis; nodes.push(node); } return { success: true, message: nodes }; diff --git a/backend/sensei/admin.js b/backend/sensei/admin.js index f8510e9..f321241 100644 --- a/backend/sensei/admin.js +++ b/backend/sensei/admin.js @@ -1,205 +1,73 @@ -const fetch = require('node-fetch'); -const BASE_URL = process.env.BASE_URL; -const MACAROON = process.env.MACAROON; -const TOKEN = process.env.TOKEN; +const { apiCall } = require('../utils/apiCall'); const initSensei = async (username, passphrase, alias, electrum_url, start) => { - const res = await fetch(`${BASE_URL}/v1/init`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - body: { - username, - passphrase, - alias, - electrum_url, - start, - }, + const res = await apiCall('/v1/init', 'POST', { + username, + passphrase, + alias, + electrum_url, + start, }); - - console.log(res); return await res.json(); }; const listNodes = async (page, take, query) => { - const res = await fetch(`${BASE_URL}/v1/nodes?page=${0}&take=${10}`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - } - }); - - console.log(res); + const res = await apiCall('/v1/nodes?page=${0}&take=${10}', 'GET'); return await res.json(); }; const createNode = async (username, passphrase, alias, start) => { - const res = await fetch(`${BASE_URL}/v1/nodes`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - body: { - username, - passphrase, - alias, - start, - }, + const res = await apiCall('/v1/nodes', 'POST', { + username, + passphrase, + alias, + start, }); - - console.log(res); return await res.json(); }; const startNode = async (pubkey, passphrase) => { - const res = await fetch(`${BASE_URL}/v1/nodes/start`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - body: { - pubkey, - passphrase, - }, - }); - - console.log(res); + const res = await apiCall('/v1/nodes/start', 'POST', { pubkey, passphrase }); return await res.json(); }; const stopNode = async (pubkey) => { - const res = await fetch(`${BASE_URL}/v1/nodes/stop`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - body: { - pubkey, - }, - }); - - console.log(res); + const res = await apiCall('/v1/nodes/stop', 'POST', { pubkey }); return await res.json(); }; const deleteNode = async (pubkey) => { - const res = await fetch(`${BASE_URL}/v1/nodes/delete`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - body: { - pubkey, - }, - }); - - console.log(res); + const res = await apiCall('/v1/nodes/delete', 'POST', { pubkey }); return await res.json(); }; const nodeStatus = async () => { - const res = await fetch(`${BASE_URL}/v1/status`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - }); - - console.log(res); + const res = await apiCall('/v1/status', 'GET'); return await res.json(); }; const startSensi = async (passphrase) => { - const res = await fetch(`${BASE_URL}/v1/start`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - body: { - passphrase, - }, - }); - - console.log(res); + const res = await apiCall('/v1/start', 'POST', { passphrase }); return await res.json(); }; const login = async (username, passphrase) => { - const res = await fetch(`${BASE_URL}/v1/login`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - body: { - username, - passphrase, - }, - }); - - console.log(res); + const res = await apiCall('/v1/login', 'POST', { username, passphrase }); return await res.json(); }; const logout = async () => { - const res = await fetch(`${BASE_URL}/v1/logout`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - }); - - console.log(res); + const res = await apiCall('/v1/logout', 'POST'); return await res.json(); }; const getConfig = async () => { - const res = await fetch(`${BASE_URL}/v1/config`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - }); - - console.log(res); + const res = await apiCall('/v1/config', 'GET'); return await res.json(); }; const updateConfig = async (electrum_url) => { - const res = await fetch(`${BASE_URL}/v1/config`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': `macaroon=${MACAROON}; token=${TOKEN}` - }, - credentials: 'include', - body: { - electrum_url, - }, - }); - - console.log(res); + const res = await apiCall('/v1/config', 'POST', { electrum_url }); return await res.json(); };