diff --git a/backend/api/admin/adminController.js b/backend/api/admin/adminController.js index c463efe..e728fc9 100644 --- a/backend/api/admin/adminController.js +++ b/backend/api/admin/adminController.js @@ -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 }; diff --git a/backend/api/admin/adminService.js b/backend/api/admin/adminService.js index dfc852a..4d78bb2 100644 --- a/backend/api/admin/adminService.js +++ b/backend/api/admin/adminService.js @@ -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'); @@ -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 }; diff --git a/backend/api/admin/index.js b/backend/api/admin/index.js index 1c6b6a1..dc9ce00 100644 --- a/backend/api/admin/index.js +++ b/backend/api/admin/index.js @@ -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;