Skip to content

Commit

Permalink
Add POST .../admin/permissions endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaloult committed May 8, 2022
1 parent d458f4b commit aac0ae3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
29 changes: 28 additions & 1 deletion backend/api/admin/adminController.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,31 @@ const keysend = async (req, res) => {
}
}

module.exports = { getAllBalances, addNewAccount, keysend };
// Request body:
// ```
// {
// accountName: string,
// permissions: {
// isAdmin: boolean,
// hasAllowance: boolean,
// canSpend: boolean,
// }
// }
// ```
const updatePermissions = async (req, res) => {
try {
// TODO: Input verification logic?
const accountName = req.body.accountName;
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 });
}
};

module.exports = { getAllBalances, addNewAccount, keysend, updatePermissions };
20 changes: 18 additions & 2 deletions backend/api/admin/adminService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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');

Expand All @@ -18,4 +18,20 @@ const getAllBalances = async (req, res) => {
}
};

module.exports = { getAllBalances };
const updatePermissions = async (accountName, newPermissions) => {
const docName = 'nicks-family';
const subcollectionName = 'members';
const docRef = await collection.doc(docName).collection(subcollectionName).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 };
};

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

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

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

module.exports = router;

0 comments on commit aac0ae3

Please sign in to comment.