Commit fdb760dd authored by Benjamin Bouvier's avatar Benjamin Bouvier
Browse files

Clean Pouch metadata on the /all request too;

parent fb337543
......@@ -28,10 +28,18 @@ let log = makeLogger('controllers/all');
const ERR_MSG_LOADING_ALL = 'Error when loading all Kresus data';
// Strip away Couchdb/pouchdb metadata.
function cleanMeta(obj) {
delete obj._id;
delete obj._rev;
delete obj.docType;
return obj;
}
async function getAllData(userId, isExport = false, cleanPassword = true) {
let ret = {};
ret.accounts = await Accounts.all(userId);
ret.accesses = await Accesses.all(userId);
ret.accounts = (await Accounts.all(userId)).map(cleanMeta);
ret.accesses = (await Accesses.all(userId)).map(cleanMeta);
for (let access of ret.accesses) {
// Process enabled status only for the /all request.
......@@ -44,18 +52,21 @@ async function getAllData(userId, isExport = false, cleanPassword = true) {
}
}
ret.categories = await Categories.all(userId);
ret.operations = await Transactions.all(userId);
ret.settings = isExport ? await Settings.allWithoutGhost(userId) : await Settings.all(userId);
ret.categories = (await Categories.all(userId)).map(cleanMeta);
ret.operations = (await Transactions.all(userId)).map(cleanMeta);
ret.settings = (isExport
? await Settings.allWithoutGhost(userId)
: await Settings.all(userId)
).map(cleanMeta);
if (isExport) {
ret.budgets = await Budgets.all(userId);
ret.budgets = (await Budgets.all(userId)).map(cleanMeta);
}
// Return alerts only if there is an email recipient.
let emailRecipient = ret.settings.find(s => s.name === 'email-recipient');
if (emailRecipient && emailRecipient.value !== DefaultSettings.get('email-recipient')) {
ret.alerts = await Alerts.all(userId);
ret.alerts = (await Alerts.all(userId)).map(cleanMeta);
} else {
ret.alerts = [];
}
......
......@@ -6,13 +6,6 @@ import DefaultSettings from '../../shared/default-settings';
let log = makeLogger('controllers/helpers');
// Strip away Couchdb/pouchdb metadata.
function cleanMeta(obj) {
delete obj._id;
delete obj._rev;
delete obj.docType;
}
// Sync function
export function cleanData(world) {
let accessMap = {};
......@@ -22,7 +15,6 @@ export function cleanData(world) {
for (let a of world.accesses) {
accessMap[a.id] = nextAccessId;
a.id = nextAccessId++;
cleanMeta(a);
}
let accountMap = {};
......@@ -32,7 +24,6 @@ export function cleanData(world) {
a.bankAccess = accessMap[a.bankAccess];
accountMap[a.id] = nextAccountId;
a.id = nextAccountId++;
cleanMeta(a);
}
let categoryMap = {};
......@@ -41,7 +32,6 @@ export function cleanData(world) {
for (let c of world.categories) {
categoryMap[c.id] = nextCatId;
c.id = nextCatId++;
cleanMeta(c);
}
world.budgets = world.budgets || [];
......@@ -51,7 +41,6 @@ export function cleanData(world) {
} else {
b.categoryId = categoryMap[b.categoryId];
}
cleanMeta(b);
}
world.operations = world.operations || [];
......@@ -69,7 +58,6 @@ export function cleanData(world) {
// Strip away id.
delete o.id;
cleanMeta(o);
// Remove attachments, if there are any.
delete o.attachments;
......@@ -90,7 +78,6 @@ export function cleanData(world) {
}
delete s.id;
cleanMeta(s);
// Properly save the default account id if it exists.
if (
......@@ -114,7 +101,6 @@ export function cleanData(world) {
for (let a of world.alerts) {
a.accountId = accountMap[a.accountId];
delete a.id;
cleanMeta(a);
}
return world;
......
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