Skip to content

Commit

Permalink
pushing to clear
Browse files Browse the repository at this point in the history
  • Loading branch information
bnonni committed May 7, 2022
1 parent 1bda121 commit e92c329
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 108 deletions.
11 changes: 5 additions & 6 deletions backend/api/account/accountController.js
Original file line number Diff line number Diff line change
@@ -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 };
4 changes: 2 additions & 2 deletions backend/api/account/index.js
Original file line number Diff line number Diff line change
@@ -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;
46 changes: 25 additions & 21 deletions backend/api/admin/adminController.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -55,4 +59,4 @@ const shouldBeEmpty = async (req, res) => {
}
}

module.exports = { getParkingSpotDetails, reserveParkingSpot, shouldBeEmpty };
module.exports = { getAllBalances, addNewAccount, keysend };
77 changes: 2 additions & 75 deletions backend/api/admin/adminService.js
Original file line number Diff line number Diff line change
@@ -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 = { };
6 changes: 4 additions & 2 deletions backend/api/admin/index.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion backend/sensei/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

Expand Down
12 changes: 11 additions & 1 deletion backend/sensei/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ const getPeers = async () => {
};
}),
};
}
}


export {
getUnusedAddress,
getBalance,
getInfo,
getPeers,
getChannels,
getPayments
};

0 comments on commit e92c329

Please sign in to comment.