diff --git a/backend/api/admin/adminController.js b/backend/api/admin/adminController.js index d9e9da5..a5adb1a 100644 --- a/backend/api/admin/adminController.js +++ b/backend/api/admin/adminController.js @@ -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: // ``` // { @@ -99,4 +114,4 @@ const setAccountAllowance = async (req, res) => { } }; -module.exports = { getAllBalances, addNewAccount, keysend, updatePermissions, setAccountAllowance }; +module.exports = { getAllBalances, addNewAccount, keysend, updatePermissions, getAccountAllowance, setAccountAllowance }; diff --git a/backend/api/admin/adminService.js b/backend/api/admin/adminService.js index 25251c3..a60e5e5 100644 --- a/backend/api/admin/adminService.js +++ b/backend/api/admin/adminService.js @@ -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(); @@ -49,4 +71,4 @@ const setAccountAllowance = async (accountName, newAllowance) => { return { success: true }; }; -module.exports = { getAllBalances, updatePermissions, setAccountAllowance }; +module.exports = { getAllBalances, updatePermissions, getAccountAllowance, setAccountAllowance }; diff --git a/backend/api/admin/index.js b/backend/api/admin/index.js index a73d5ed..2f64de7 100644 --- a/backend/api/admin/index.js +++ b/backend/api/admin/index.js @@ -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;