Commit a12ccdd9 authored by Benjamin Bouvier's avatar Benjamin Bouvier

[server] Don't dynamically create a formatCurrency function on Accounts;

parent dfa2b2ec
Pipeline #210934 passed with stages
in 17 minutes and 58 seconds
......@@ -219,3 +219,12 @@ export function checkWeboobMinimalVersion(version: string | null): boolean {
export function makeUrlPrefixRegExp(urlPrefix: string): RegExp {
return new RegExp(`^${urlPrefix}/?`);
}
const currencyFormatterCache: { [key: string]: Function } = {};
export function currencyFormatter(someCurrency: string): Function {
if (typeof currencyFormatterCache[someCurrency] === 'undefined') {
currencyFormatterCache[someCurrency] = currency.makeFormat(someCurrency);
}
return currencyFormatterCache[someCurrency];
}
......@@ -76,7 +76,6 @@ class ReportManager {
let operationsByAccount = new Map();
for (let a of accounts) {
a.formatCurrency = await a.getCurrencyFormatter();
operationsByAccount.set(a.id, {
account: a,
operations: []
......@@ -162,10 +161,12 @@ class ReportManager {
accountsNameMap.set(account.id, `${access.getLabel()}${displayLabel(account)}`);
}
let formatCurrency = await account.getCurrencyFormatter();
let lastCheckDate = formatDate.toShortString(account.lastCheckDate);
let balance = await account.computeBalance();
content += `\t* ${accountsNameMap.get(account.id)} : `;
content += `${account.formatCurrency(balance)} (`;
content += `${formatCurrency(balance)} (`;
content += $t('server.email.report.last_sync');
content += ` ${lastCheckDate})\n`;
}
......@@ -188,11 +189,13 @@ class ReportManager {
return 1;
});
let formatCurrency = await pair.account.getCurrencyFormatter();
content += `\n${accountsNameMap.get(pair.account.id)}:\n`;
for (let op of operations) {
let date = formatDate.toShortString(op.date);
content += `\t* ${date} - ${op.label} : `;
content += `${pair.account.formatCurrency(op.amount)}\n`;
content += `${formatCurrency(op.amount)}\n`;
}
}
} else {
......
......@@ -17,6 +17,7 @@ import Setting from './settings';
import {
currency,
currencyFormatter,
UNKNOWN_ACCOUNT_TYPE,
shouldIncludeInBalance,
shouldIncludeInOutstandingSum,
......@@ -122,10 +123,14 @@ export default class Account {
};
getCurrencyFormatter = async (): Promise<Function> => {
const curr = currency.isKnown(this.currency)
? this.currency
: (await Setting.findOrCreateDefault(await this.userId, 'default-currency')).value;
return currency.makeFormat(curr);
let checkedCurrency;
if (currency.isKnown(this.currency)) {
checkedCurrency = this.currency;
} else {
checkedCurrency = (await Setting.findOrCreateDefault(this.userId, 'default-currency'))
.value;
}
return currencyFormatter(checkedCurrency);
};
// Static methods
......
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