Commit 55fd74f4 authored by Benjamin Bouvier's avatar Benjamin Bouvier

Fixes #843: Forward the right no-category sentinel when creating a new transaction;

parent 22e01593
Pipeline #120678 passed with stages
in 6 minutes and 54 seconds
......@@ -7,7 +7,8 @@ import {
translate as $t,
NONE_CATEGORY_ID,
UNKNOWN_OPERATION_TYPE,
displayLabel
displayLabel,
notify
} from '../../helpers';
import CategorySelect from './category-select';
......@@ -39,7 +40,7 @@ const AddOperationModal = connect(
await actions.createOperation(dispatch, operation);
actions.hideModal(dispatch);
} catch (err) {
// TODO properly report.
notify.error(err.message);
}
}
};
......
......@@ -307,8 +307,7 @@ export function setOperationCategory(operationId, categoryId, formerCatId) {
assert(typeof operationId === 'string', 'SetOperationCategory first arg must have an id');
assert(typeof categoryId === 'string', 'SetOperationCategory 2nd arg must be String id');
// The server expects an empty string for replacing by none
let serverCategoryId = categoryId === NONE_CATEGORY_ID ? '' : categoryId;
let serverCategoryId = categoryId === NONE_CATEGORY_ID ? null : categoryId;
return dispatch => {
dispatch(basic.setOperationCategory(operationId, categoryId, formerCatId));
......@@ -385,10 +384,15 @@ export function mergeOperations(toKeep, toRemove) {
}
export function createOperation(operation) {
let serverOperation = operation;
if (operation.categoryId === NONE_CATEGORY_ID) {
serverOperation = Object.assign({}, operation, { categoryId: null });
}
return dispatch => {
dispatch(basic.createOperation(operation));
return backend
.createOperation(operation)
.createOperation(serverOperation)
.then(created => {
dispatch(success.createOperation(created));
})
......
......@@ -120,8 +120,7 @@ export function destroy(categoryId, replace) {
assert(typeof categoryId === 'string', 'DeleteCategory first arg must be a string id');
assert(typeof replace === 'string', 'DeleteCategory second arg must be a String id');
// The server expects an empty string if there's no replacement category.
let serverReplace = replace === NONE_CATEGORY_ID ? '' : replace;
let serverReplace = replace === NONE_CATEGORY_ID ? null : replace;
return dispatch => {
dispatch(basic.deleteCategory(categoryId, replace));
......
......@@ -75,25 +75,23 @@ export async function destroy(req, res) {
try {
let { id: userId } = req.user;
let replaceby = req.body.replaceByCategoryId;
if (typeof replaceby === 'undefined') {
throw new KError('Missing parameter replaceby', 400);
let replaceBy = req.body.replaceByCategoryId;
if (typeof replaceBy === 'undefined') {
throw new KError('Missing parameter replaceBy', 400);
}
let former = req.preloaded.category;
let categoryId;
if (replaceby.toString() !== '') {
log.debug(`Replacing category ${former.id} by ${replaceby}...`);
let categoryToReplaceBy = await Categories.find(userId, replaceby);
if (replaceBy !== null) {
log.debug(`Replacing category ${former.id} by ${replaceBy}...`);
let categoryToReplaceBy = await Categories.find(userId, replaceBy);
if (!categoryToReplaceBy) {
throw new KError('Replacement category not found', 404);
}
categoryId = replaceby;
} else {
log.debug('No replacement category, replacing by None.');
categoryId = null;
log.debug('No replacement category, replacing by the None category.');
}
let categoryId = replaceBy;
let operations = await Transactions.byCategory(userId, former.id);
for (let op of operations) {
......
......@@ -48,16 +48,13 @@ export async function update(req, res) {
let opUpdate = {};
if (typeof attr.categoryId !== 'undefined') {
if (attr.categoryId === '') {
opUpdate.categoryId = null;
} else {
let newCategory = await Categories.find(userId, attr.categoryId);
if (!newCategory) {
if (attr.categoryId !== null) {
let found = await Categories.find(userId, attr.categoryId);
if (!found) {
throw new KError('Category not found', 404);
} else {
opUpdate.categoryId = attr.categoryId;
}
}
opUpdate.categoryId = attr.categoryId;
}
if (typeof attr.type !== 'undefined') {
......@@ -118,11 +115,20 @@ export async function create(req, res) {
if (!Transaction.isOperation(operation)) {
throw new KError('Not an operation', 400);
}
if (typeof operation.categoryId !== 'undefined' && operation.categoryId !== null) {
let found = await Categories.find(userId, operation.categoryId);
if (!found) {
throw new KError('Category not found', 404);
}
}
// We fill the missing fields
operation.raw = operation.title;
operation.customLabel = operation.title;
operation.dateImport = moment().format('YYYY-MM-DDTHH:mm:ss.000Z');
operation.createdByUser = true;
let op = await Transaction.create(userId, operation);
res.status(201).json(op);
} catch (err) {
......
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