diff --git a/backend/api/admin/adminController.js b/backend/api/admin/adminController.js index e728fc9..d28b4b0 100644 --- a/backend/api/admin/adminController.js +++ b/backend/api/admin/adminController.js @@ -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 }; diff --git a/backend/api/admin/adminService.js b/backend/api/admin/adminService.js index 4d78bb2..25251c3 100644 --- a/backend/api/admin/adminService.js +++ b/backend/api/admin/adminService.js @@ -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(); @@ -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`; @@ -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 }; diff --git a/backend/api/admin/index.js b/backend/api/admin/index.js index dc9ce00..660491b 100644 --- a/backend/api/admin/index.js +++ b/backend/api/admin/index.js @@ -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;