Commit 7e3d8b08 authored by Benjamin Bouvier's avatar Benjamin Bouvier
Browse files

Add validation checks for Account update;

parent 6c3a0e5b
......@@ -99,22 +99,28 @@ export function deleteOperation(opId) {
});
}
export function resyncBalance(accountId) {
return buildFetchPromise(`api/${API_VERSION}/accounts/${accountId}/resync-balance`).then(
data => data.initialBalance
);
}
export function updateAccount(accountId, newFields) {
let error = checkAllowedFields(newFields, ['excludeFromBalance', 'customLabel']);
if (error) {
alert(`Developer error when updating an account: ${error}`);
return;
}
export function updateAccount(accountId, attributes) {
return buildFetchPromise(`api/${API_VERSION}/accounts/${accountId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(attributes)
body: JSON.stringify(newFields)
});
}
export function resyncBalance(accountId) {
return buildFetchPromise(`api/${API_VERSION}/accounts/${accountId}/resync-balance`).then(
data => data.initialBalance
);
}
export function deleteAccount(accountId) {
return buildFetchPromise(`api/${API_VERSION}/accounts/${accountId}`, {
method: 'DELETE'
......
......@@ -7,6 +7,7 @@ import Transactions from '../../models/transactions';
import accountManager from '../../lib/accounts-manager';
import { makeLogger, KError, asyncErr } from '../../helpers';
import { checkAllowedFields } from '../../shared/validators';
let log = makeLogger('controllers/accounts');
......@@ -53,23 +54,18 @@ export async function destroyWithOperations(userId, account) {
}
}
// Update an account.
export async function update(req, res) {
try {
let { id: userId } = req.user;
let attr = req.body;
// We can only update the flag excludeFromBalance
// and the custom label of an account.
if (
typeof attr.excludeFromBalance === 'undefined' &&
typeof attr.customLabel === 'undefined'
) {
throw new KError('Missing parameter', 400);
let newFields = req.body;
let error = checkAllowedFields(newFields, ['excludeFromBalance', 'customLabel']);
if (error) {
throw new KError(`when updating an account: ${error}`, 400);
}
let account = req.preloaded.account;
let newAccount = await Accounts.update(userId, account.id, attr);
let newAccount = await Accounts.update(userId, account.id, newFields);
res.status(200).json(newAccount);
} catch (err) {
return asyncErr(res, err, 'when updating an account');
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment