Commit 04fc8d9d authored by Benjamin Bouvier's avatar Benjamin Bouvier
Browse files

Fixes #667: Reuse the update-access endpoint for disabling accesses;

parent b57e5436
Pipeline #25685 passed with stages
in 10 minutes and 10 seconds
......@@ -260,15 +260,6 @@ export function updateAccess(accessId, access) {
});
}
export function disableAccess(accessId) {
return buildFetchPromise(`api/${API_VERSION}/accesses/${accessId}/disable`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
}
});
}
export function createAccess(bank, login, password, customFields) {
let data = {
bank,
......
......@@ -356,9 +356,8 @@ function handleFirstSyncError(err) {
export function createAccess(get, uuid, login, password, fields) {
return dispatch => {
dispatch(basic.createAccess());
backend.createAccess(uuid, login, password, fields)
dispatch(basic.createAccess(uuid, login, null, fields));
backend.createAccess(uuid, login, fields)
.then(results => {
dispatch(success.createAccess(uuid, login, fields, results));
})
......@@ -754,21 +753,14 @@ function reduceUpdateAccess(state, action) {
return state;
}
assertHas(action, 'results');
let { login, customFields, accessId } = action;
assert(customFields instanceof Array);
let { accessId } = action;
let update = { login, customFields, enabled: true };
let newState = u.updateIn('accesses', updateMapIf('id', accessId, update), state);
return finishSync(newState, action.results);
}
assertHas(action, 'newFields');
let newState = u.updateIn('accesses', updateMapIf('id', accessId, action.newFields), state);
function reduceDisableAccess(state, action) {
let { status, accessId } = action;
if (status === SUCCESS) {
return u.updateIn('accesses', updateMapIf('id', accessId, { enabled: false }), state);
}
return state;
return typeof action.results !== 'undefined' ?
finishSync(newState, action.results) :
newState;
}
function reduceCreateAlert(state, action) {
......@@ -842,7 +834,7 @@ const reducers = {
DELETE_ALERT: reduceDeleteAlert,
DELETE_CATEGORY: reduceDeleteCategory,
DELETE_OPERATION: reduceDeleteOperation,
DISABLE_ACCESS: reduceDisableAccess,
DISABLE_ACCESS: reduceUpdateAccess,
MERGE_OPERATIONS: reduceMergeOperations,
RUN_ACCOUNTS_SYNC: reduceRunAccountsSync,
RUN_BALANCE_RESYNC: reduceResyncBalance,
......
......@@ -33,13 +33,6 @@ const settingsState = u({
// Basic action creators
const basic = {
disableAccess(accessId) {
return {
type: DISABLE_ACCESS,
accessId
};
},
sendTestEmail() {
return {
type: SEND_TEST_EMAIL
......@@ -68,13 +61,20 @@ const basic = {
};
},
updateAccess(accessId, login, customFields, results = {}) {
disableAccess(accessId, newFields = {}) {
return {
type: DISABLE_ACCESS,
accessId,
newFields
};
},
updateAccess(accessId, newFields = {}, results = null) {
return {
type: UPDATE_ACCESS,
results,
accessId,
login,
customFields
newFields,
results
};
},
......@@ -91,11 +91,14 @@ const fail = {}, success = {};
fillOutcomeHandlers(basic, fail, success);
export function disableAccess(accessId) {
let newFields = {
enabled: false
};
return dispatch => {
dispatch(basic.disableAccess(accessId));
backend.disableAccess(accessId)
backend.updateAccess(accessId, newFields)
.then(() => {
dispatch(success.disableAccess(accessId));
dispatch(success.disableAccess(accessId, newFields));
}).catch(err => {
dispatch(fail.disableAccess(err));
});
......@@ -157,11 +160,16 @@ export function resetWeboobVersion() {
}
export function updateAccess(accessId, login, password, customFields) {
let newFields = {
login,
customFields,
enabled: true
};
return dispatch => {
dispatch(basic.updateAccess());
backend.updateAccess(accessId, { login, password, customFields }).then(results => {
dispatch(basic.updateAccess(accessId, newFields));
backend.updateAccess(accessId, { password, ...newFields }).then(results => {
results.accessId = accessId;
dispatch(success.updateAccess(accessId, login, customFields, results));
dispatch(success.updateAccess(accessId, newFields, results));
}).catch(err => {
dispatch(fail.updateAccess(err));
});
......
......@@ -6,7 +6,12 @@ import { fullPoll } from '../../lib/poller';
import * as AccountController from './accounts';
import { makeLogger, KError, getErrorCode, asyncErr } from '../../helpers';
import {
asyncErr,
getErrorCode,
KError,
makeLogger
} from '../../helpers';
let log = makeLogger('controllers/accesses');
......@@ -180,33 +185,28 @@ export async function poll(req, res) {
}
}
// Updates the bank access
// Updates a bank access.
export async function update(req, res) {
try {
let access = req.body;
if (!access.password)
throw new KError('missing password', 400);
// Always enable the access.
access.enabled = true;
await req.preloaded.access.updateAttributes(access);
await fetchAccounts(req, res);
} catch (err) {
return asyncErr(res, err, 'when updating bank access');
}
}
if (typeof access.enabled === 'undefined' || access.enabled) {
await req.preloaded.access.updateAttributes(access);
await fetchAccounts(req, res);
} else {
if (Object.keys(access).length > 1) {
log.warn('Supplementary fields not considered when disabling an access.');
}
// Disable the bank access
export async function disable(req, res) {
try {
let access = req.preloaded.access;
let preloaded = req.preloaded.access;
access.enabled = false;
delete access.password;
delete preloaded.password;
preloaded.enabled = false;
await access.save();
res.status(201).json({ status: 'OK' });
await preloaded.save();
res.status(201).json({ status: 'OK' });
}
} catch (err) {
return asyncErr(res, err, 'when disabling bank access');
return asyncErr(res, err, 'when updating bank access');
}
}
......@@ -41,9 +41,6 @@ const routes = {
'accesses/:accessId/fetch/accounts': {
get: accesses.fetchAccounts
},
'accesses/:accessId/disable': {
put: accesses.disable
},
// Accounts
'accountId': {
......
Markdown is supported
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