Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Brookman authored and Jordan Brookman committed May 8, 2022
2 parents 6b38fb5 + 6273027 commit ad8b7f1
Show file tree
Hide file tree
Showing 15 changed files with 648 additions and 197 deletions.
34 changes: 23 additions & 11 deletions backend/api/account/accountController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -31,12 +30,11 @@ const payInvoice = async (req, res) => {
}
};

const createInvoice = async (req, res) => {
const getAccountBalance = async (req, res) => {
try {
const response = await accountService.getAccountAllowance();

debug.info(`Account Allowance Response: ${JSON.stringify(response)}`);
const response = await accountService.getAccountBalance();

debug.info(`Account Balance Response: ${JSON.stringify(response)}`);
if (!response.success) res.status(500).json(response);
else res.status(200).json(response);

Expand All @@ -46,4 +44,18 @@ const createInvoice = async (req, res) => {
}
};

module.exports = { getAccountAllowance, payInvoice, createInvoice };
const createInvoice = async (req, res) => {
try {
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 });
}
};

module.exports = { getAccountAllowance, payInvoice, createInvoice, getAccountBalance };
34 changes: 13 additions & 21 deletions backend/api/account/accountService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
6 changes: 2 additions & 4 deletions backend/api/account/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const express = require('express');
const router = express();

const { getAccountAllowance, payInvoice, createInvoice } = require('./accountController');
const { getAccountAllowance, payInvoice, createInvoice, getAccountBalance } = require('./accountController');

const { getBalance } = require('../../sensei/nodes');

router.get('/account/balance', getBalance);
router.get('/account/balance', getAccountBalance);
router.get('/allowance', getAccountAllowance);
router.get('/payment/send', payInvoice);
router.get('/payment/receive', createInvoice);
Expand Down
50 changes: 49 additions & 1 deletion backend/api/admin/adminController.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,52 @@ const keysend = async (req, res) => {
}
}

module.exports = { getAllBalances, addNewAccount, keysend };
// Request body:
// ```
// {
// permissions: {
// isAdmin: boolean,
// hasAllowance: boolean,
// canSpend: boolean,
// }
// }
// ```
const updatePermissions = async (req, res) => {
try {
// TODO: Input verification logic?
const accountName = req.params.username;
const newPermissions = req.body.permissions;
const response = await adminService.updatePermissions(accountName, newPermissions);
debug.info(`Response for updating a family member's permissions: ${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 });
}
};

// Request body:
// ```
// {
// allowance: number,
// }
// ```
const setAccountAllowance = async (req, res) => {
try {
// TODO: Input verification logic?
const accountName = req.params.username;
const newAllowance = req.body.allowance;
const response = await adminService.setAccountAllowance(accountName, newAllowance);
debug.info(`Response for setting a family member's allowance: ${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 = { getAllBalances, addNewAccount, keysend, updatePermissions, setAccountAllowance };
37 changes: 34 additions & 3 deletions backend/api/admin/adminService.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const debug = require('../../utils/debug');
const accounts = require('../../db/collection');
const collection = require('../../db/collection');
const senseiAdmin = require('../../sensei/admin');
const senseiNodes = require('../../sensei/nodes');

const DOC_NAME = 'nicks-family';
const MEMBERS_SUBCOLLECTION_NAME = 'members';

const getAllBalances = async (req, res) => {
try {
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 };
Expand All @@ -18,4 +21,32 @@ const getAllBalances = async (req, res) => {
}
};

module.exports = { getAllBalances };
const updatePermissions = async (accountName, newPermissions) => {
const docRef = await collection.doc(DOC_NAME).collection(MEMBERS_SUBCOLLECTION_NAME).doc(accountName);
const doc = await docRef.get();
if (!doc.exists) {
const errMsg = `Firestore document "${docName}/${subcollectionName}/${accountName}" does not exist in the families collection`;
debug.error(errMsg);
throw new Error(errMsg);
}

// TODO: Error handling?
await docRef.update({permissions: newPermissions});
return { success: true };
};

const setAccountAllowance = async (accountName, newAllowance) => {
const docRef = await collection.doc(DOC_NAME).collection(MEMBERS_SUBCOLLECTION_NAME).doc(accountName);
const doc = await docRef.get();
if (!doc.exists) {
const errMsg = `Firestore document "${docName}/${subcollectionName}/${accountName}" does not exist in the families collection`;
debug.error(errMsg);
throw new Error(errMsg);
}

// TODO: Error handling?
await docRef.update({ allowance: newAllowance, "permissions.hasAllowance": true });
return { success: true };
};

module.exports = { getAllBalances, updatePermissions, setAccountAllowance };
4 changes: 3 additions & 1 deletion backend/api/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const express = require('express');
const router = express();

const { getAllBalances, addNewAccount, keysend } = require('./adminController');
const { getAllBalances, addNewAccount, keysend, updatePermissions, setAccountAllowance } = require('./adminController');

router.get('/balances', getAllBalances);
router.post('/add', addNewAccount);
router.post('/transfer', keysend);
router.post('/account/:username/permissions', updatePermissions)
router.post('/account/:username/allowance', setAccountAllowance)

module.exports = router;
Loading

0 comments on commit ad8b7f1

Please sign in to comment.