Skip to content

Commit

Permalink
Add GET .../admin/account/:username/allowance endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaloult committed May 8, 2022
1 parent 5a81264 commit 17435f9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
17 changes: 16 additions & 1 deletion backend/api/admin/adminController.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ const updatePermissions = async (req, res) => {
}
};

const getAccountAllowance = async (req, res) => {
try {
// TODO: Input verification logic?
const accountName = req.params.username;
const response = await adminService.getAccountAllowance(accountName);
debug.info(`Response for getting 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 });
}
};

// Request body:
// ```
// {
Expand All @@ -99,4 +114,4 @@ const setAccountAllowance = async (req, res) => {
}
};

module.exports = { getAllBalances, addNewAccount, keysend, updatePermissions, setAccountAllowance };
module.exports = { getAllBalances, addNewAccount, keysend, updatePermissions, getAccountAllowance, setAccountAllowance };
24 changes: 23 additions & 1 deletion backend/api/admin/adminService.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ const updatePermissions = async (accountName, newPermissions) => {
return { success: true };
};

const getAccountAllowance = async (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`;
debug.error(errMsg);
throw new Error(errMsg);
}

// TODO: Error handling?
let data = doc.data();
// If permissions indicate that we don't have an allowance, then ignore
// whatever value is in that allowance field.
if (!data.permissions.hasAllowance) {
return { success: true, allowance: -1 };
}

// TODO: Error handling?
let allowance = data.allowance;
return { success: true, allowance };
};

const setAccountAllowance = async (accountName, newAllowance) => {
const docRef = await collection.doc(DOC_NAME).collection(MEMBERS_SUBCOLLECTION_NAME).doc(accountName);
const doc = await docRef.get();
Expand All @@ -49,4 +71,4 @@ const setAccountAllowance = async (accountName, newAllowance) => {
return { success: true };
};

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

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

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

module.exports = router;

0 comments on commit 17435f9

Please sign in to comment.