From e92c329840acf347c5bc97420d96cd5b6763051f Mon Sep 17 00:00:00 2001 From: bnonni Date: Sat, 7 May 2022 16:13:14 -0400 Subject: [PATCH] pushing to clear --- backend/api/account/accountController.js | 11 ++-- backend/api/account/index.js | 4 +- backend/api/admin/adminController.js | 46 +++++++------- backend/api/admin/adminService.js | 77 +----------------------- backend/api/admin/index.js | 6 +- backend/sensei/admin.js | 2 +- backend/sensei/nodes.js | 12 +++- 7 files changed, 50 insertions(+), 108 deletions(-) diff --git a/backend/api/account/accountController.js b/backend/api/account/accountController.js index 7be5c4d..94ba7c6 100644 --- a/backend/api/account/accountController.js +++ b/backend/api/account/accountController.js @@ -1,20 +1,19 @@ const debug = require('../../utils/debug'); const accountService = require('./accountService'); -const getAccountBalance = async (req, res) => { +const getAccountAllowance = async (req, res) => { try { - const username = req._parsedUrl.query.split('=')[1]; - username, passphrase, alias, start - const response = await accountService.getAccountBalance(username); + const response = await accountService.getAccountAllowance(); - debug.info(`Invoice Creation Response: ${JSON.stringify(response)}`); + debug.info(`Account Allowance 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 }); } }; -module.exports = { getAccountBalance }; +module.exports = { getAccountAllowance }; diff --git a/backend/api/account/index.js b/backend/api/account/index.js index 9b3d7e7..d468042 100644 --- a/backend/api/account/index.js +++ b/backend/api/account/index.js @@ -1,8 +1,8 @@ const express = require('express'); const router = express(); -const { getAccountBalance } = require('./accountController'); +const { getAccountAllowance } = require('./accountController'); -router.get('/allowance', getAccountBalance); +router.get('/allowance', getAccountAllowance); module.exports = router; diff --git a/backend/api/admin/adminController.js b/backend/api/admin/adminController.js index 6518af2..d1045ad 100644 --- a/backend/api/admin/adminController.js +++ b/backend/api/admin/adminController.js @@ -1,12 +1,18 @@ const debug = require('../../utils/debug'); const adminService = require('./adminService'); +const senseiAdmin = require('../../sensei/admin'); +const senseiNodes = require('../../sensei/nodes'); -const getParkingSpotDetails = async (req, res) => { +const getAllBalances = async (req, res) => { try { - const uuid = req._parsedUrl.query.split('=')[1]; - const response = await adminService.getParkingSpotDetails(uuid); + const response = await senseiAdmin.listNodes(); + let nodesBalances = [] + for(let node of response.nodes){ + node.balance = await senseiNodes.getBalance(node.username); + nodesBalances.push(node) + } - debug.info(`Spot Details Response: ${JSON.stringify(response)}`); + debug.info(`Admin All Balances Response: ${nodesBalances}`); if (!response.success) res.status(500).json(response); else res.status(200).json(response); @@ -17,19 +23,16 @@ const getParkingSpotDetails = async (req, res) => { } }; -const reserveParkingSpot = async (req, res) => { +const addNewAccount = async (req, res) => { try { - const uuid = req.body.uuid; - const licensePlate = req.body.licensePlate; - const duration = req.body.duration; + const username = req.body.username; + const passphrase = req.body.passphrase; + // add to DB? + const alias = req.body.alias; + const start = req.body.start; + const response = await senseiAdmin.createNode(username, passphrase, alias, start) - const response = await adminService.reserveParkingSpot( - uuid, - licensePlate, - duration - ); - - debug.info(`Spot Reservation Response: ${JSON.stringify(response)}`); + debug.info(`Add Account / Create Node Response: ${JSON.stringify(response)}`); if (!response.success) res.status(500).json(response); else res.status(200).json(response); @@ -38,13 +41,14 @@ const reserveParkingSpot = async (req, res) => { debug.error(error.stack); res.status(500).json({ message: error.message, error: error.stack }); } -}; +} -const shouldBeEmpty = async (req, res) => { +const keysend = async (req, res) => { try { - const response = await adminService.shouldBeEmpty(); - - debug.info(`Spot Details Response: ${JSON.stringify(response)}`); + const destPubkey = req.body.destPubkey; + const amtMsat = req.body.amtMsat; + const response = await senseiNodes.keysend(destPubkey, amtMsat); + debug.info(`Admin Keysend Response: ${JSON.stringify(response)}`); if (!response.success) res.status(500).json(response); else res.status(200).json(response); @@ -55,4 +59,4 @@ const shouldBeEmpty = async (req, res) => { } } -module.exports = { getParkingSpotDetails, reserveParkingSpot, shouldBeEmpty }; +module.exports = { getAllBalances, addNewAccount, keysend }; diff --git a/backend/api/admin/adminService.js b/backend/api/admin/adminService.js index e145e90..1fc8db7 100644 --- a/backend/api/admin/adminService.js +++ b/backend/api/admin/adminService.js @@ -1,77 +1,4 @@ const debug = require('../../utils/debug'); +const accounts = require('../../db/collection'); -// replace spots with accounts -const spots = require('../../db/collection'); - - -// replace all functions -const getParkingSpotDetails = async (uuid) => { - try { - return { success: true, message: (await spots.doc(uuid).get()).data() }; - } catch (error) { - debug.error(error.stack); - throw new Error(error); - } -}; - -const reserveParkingSpot = async (uuid, licensePlate, duration) => { - try { - const spot = spots.doc(uuid); - const startTime = Math.floor(new Date().getTime() / 1000); - if (!spot.occupied) { - var response = await spot.update({ - duration: duration, - expirationTime: startTime + duration, - expired: false, - licensePlate: licensePlate, - occupied: true, - startTime: startTime, - }); - - return { success: true, message: response }; - } - } catch (error) { - debug.error(error.stack); - throw new Error(error); - } -}; - -const shouldBeEmpty = async () => { - try { - const shouldBeEmptySpots = {} - const expiredSpots = []; - const emptySpots = []; - const now = Math.floor(new Date().getTime() / 1000); - const unoccupiedSpots = await spots - .where('occupied', '==', false) - .get(); - - unoccupiedSpots.forEach(spot => { - emptySpots.push(spot.data()) - }) - shouldBeEmptySpots.unoccupiedSpots = emptySpots; - - const occupiedSpots = await spots - .where('occupied', '==', true) - .get(); - - occupiedSpots.forEach((spot) => { - const spotData = spot.data() - if((spotData.expirationTime - now) <= 0){ - spots.doc(spot.id).update({ - expired: true - }) - spotData.expired = true; - expiredSpots.push(spotData) - } - }) - shouldBeEmptySpots.expiredSpots = expiredSpots; - - return { success: true, message: shouldBeEmptySpots }; - } catch (error) { - debug.error(error.stack); - throw new Error(error); - } -}; - -module.exports = { getParkingSpotDetails, reserveParkingSpot, shouldBeEmpty }; +module.exports = { }; diff --git a/backend/api/admin/index.js b/backend/api/admin/index.js index 7c82cfd..1c6b6a1 100644 --- a/backend/api/admin/index.js +++ b/backend/api/admin/index.js @@ -1,8 +1,10 @@ const express = require('express'); const router = express(); -const { getAllBalances } = require('./adminController'); +const { getAllBalances, addNewAccount, keysend } = require('./adminController'); -router.get('/details', getAllBalances); +router.get('/balances', getAllBalances); +router.post('/add', addNewAccount); +router.post('/transfer', keysend); module.exports = router; diff --git a/backend/sensei/admin.js b/backend/sensei/admin.js index 83d6a99..dac0238 100644 --- a/backend/sensei/admin.js +++ b/backend/sensei/admin.js @@ -18,7 +18,7 @@ const initSensei = async (username, passphrase, alias, electrum_url, start) => { }; const listNodes = async (page, take, query) => { - const res = await fetch(`${BASE_URL}/v1/nodes`, { + const res = await fetch(`${BASE_URL}/v1/nodes?page=${page || 0}&take=${take || 10}`, { method: 'GET', }); diff --git a/backend/sensei/nodes.js b/backend/sensei/nodes.js index 34a6633..9a1525c 100644 --- a/backend/sensei/nodes.js +++ b/backend/sensei/nodes.js @@ -76,4 +76,14 @@ const getPeers = async () => { }; }), }; -} \ No newline at end of file +} + + +export { + getUnusedAddress, + getBalance, + getInfo, + getPeers, + getChannels, + getPayments +};