Skip to content

Commit

Permalink
Add POST .../admin/allowance endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaloult committed May 8, 2022
1 parent 8d45384 commit 15276e9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
25 changes: 24 additions & 1 deletion backend/api/admin/adminController.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,27 @@ const updatePermissions = async (req, res) => {
}
};

module.exports = { getAllBalances, addNewAccount, keysend, updatePermissions };
// Request body:
// ```
// {
// accountName: string,
// allowance: number,
// }
// ```
const setAccountAllowance = async (req, res) => {
try {
// TODO: Input verification logic?
const accountName = req.body.accountName;
const newAllowance = req.body.allowance;
const response = await adminService.setAccountAllowance(accountName, newAllowance);
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 });
}
};

module.exports = { getAllBalances, addNewAccount, keysend, updatePermissions, setAccountAllowance };
23 changes: 19 additions & 4 deletions backend/api/admin/adminService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ 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();
Expand All @@ -19,9 +22,7 @@ const getAllBalances = async (req, res) => {
};

const updatePermissions = async (accountName, newPermissions) => {
const docName = 'nicks-family';
const subcollectionName = 'members';
const docRef = await collection.doc(docName).collection(subcollectionName).doc(accountName);
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`;
Expand All @@ -34,4 +35,18 @@ const updatePermissions = async (accountName, newPermissions) => {
return { success: true };
};

module.exports = { getAllBalances, updatePermissions };
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 };
3 changes: 2 additions & 1 deletion backend/api/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const express = require('express');
const router = express();

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

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

module.exports = router;

0 comments on commit 15276e9

Please sign in to comment.