Skip to content

Commit

Permalink
Add GET .../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 c6304d4 commit 242a1a8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
9 changes: 5 additions & 4 deletions backend/api/account/accountController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const accountService = require('./accountService');

const getAccountAllowance = async (req, res) => {
try {
const username = req.body.username;
const response = await accountService.getAccountAllowance(username);
debug.info(`Account Allowance Response: ${JSON.stringify(response)}`);
// TODO: Input verification logic?
const accountName = req.params.username;
const response = await accountService.getAccountAllowance(accountName);
debug.info(`Response for getting an account'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 });
Expand Down
19 changes: 12 additions & 7 deletions backend/api/account/accountService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ const collection = require('../../db/collection');
const DOC_NAME = 'nicks-family';
const MEMBERS_SUBCOLLECTION_NAME = 'members';

const getAccountAllowance = async (username) => {
try {
// get allowance from firestore
return { success: true, message: nodes };
} catch (error) {
debug.error(error.stack);
throw new Error(error);
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?
const allowance = doc.data().allowance;
return { success: true, allowance };

};

const getAccountBalance = async () => {
Expand Down
2 changes: 1 addition & 1 deletion backend/api/account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { getAccountAllowance, payInvoice, createInvoice, getAccountBalance, getAc

router.get('/account/balance', getAccountBalance);
router.get('/:username/permissions', getAccountPermissions);
router.get('/allowance', getAccountAllowance);
router.get('/:username/allowance', getAccountAllowance);
router.get('/payment/send', payInvoice);
router.get('/payment/receive', createInvoice);

Expand Down

0 comments on commit 242a1a8

Please sign in to comment.