Commit 94e2696f authored by Antoine's avatar Antoine
Browse files

Ensure the currency formatter has always a value + fix 'default-currency' calls. Fix #839.

parent a75d9b6d
......@@ -165,7 +165,7 @@ async function notifyNewOperations(access, newOperations, accountMap) {
if (ops.length === 1) {
// Send a notification with the operation content
let formatCurrency = currency.makeFormat(account.currency);
let formatCurrency = await account.getCurrencyFormatter();
params.operation_details = `${ops[0].title} ${formatCurrency(ops[0].amount)}`;
}
......
......@@ -3,9 +3,8 @@ import Notifications from './notifications';
import Accounts from '../models/accounts';
import Alerts from '../models/alerts';
import Settings from '../models/settings';
import { makeLogger, translate as $t, currency, displayLabel } from '../helpers';
import { makeLogger, translate as $t, displayLabel } from '../helpers';
let log = makeLogger('alert-manager');
......@@ -37,15 +36,13 @@ ${$t('server.email.signature')}
async checkAlertsForOperations(userId, access, operations) {
try {
let defaultCurrency = await Settings.byName(userId, 'default-currency').value;
// Map account to names
let accounts = await Accounts.byAccess(userId, access);
let accountsMap = new Map();
for (let a of accounts) {
accountsMap.set(a.id, {
title: a.title,
formatCurrency: currency.makeFormat(a.currency || defaultCurrency)
formatCurrency: await a.getCurrencyFormatter()
});
}
......@@ -93,8 +90,6 @@ ${$t('server.email.signature')}
async checkAlertsForAccounts(userId, access) {
try {
let defaultCurrency = await Settings.byName(userId, 'default-currency').value;
let accounts = await Accounts.byAccess(userId, access);
for (let account of accounts) {
let alerts = await Alerts.byAccountAndType(userId, account.id, 'balance');
......@@ -109,8 +104,7 @@ ${$t('server.email.signature')}
}
// Set the currency formatter
let curr = account.currency || defaultCurrency;
let formatCurrency = currency.makeFormat(curr);
let formatCurrency = await account.getCurrencyFormatter();
let text = alert.formatAccountMessage(
displayLabel(account),
balance,
......
......@@ -2,7 +2,6 @@ import {
makeLogger,
KError,
translate as $t,
currency,
formatDate,
POLLER_START_LOW_HOUR,
POLLER_START_HIGH_HOUR,
......@@ -13,7 +12,6 @@ import Emailer from './emailer';
import Accounts from '../models/accounts';
import Alerts from '../models/alerts';
import Settings from '../models/settings';
import Transactions from '../models/transactions';
import moment from 'moment';
......@@ -79,12 +77,9 @@ class ReportManager {
throw new KError("report's account does not exist");
}
let defaultCurrency = await Settings.byName(userId, 'default-currency').value;
let operationsByAccount = new Map();
for (let a of accounts) {
let curr = a.currency ? a.currency : defaultCurrency;
a.formatCurrency = currency.makeFormat(curr);
a.formatCurrency = await a.getCurrencyFormatter();
operationsByAccount.set(a.id, {
account: a,
operations: []
......
......@@ -3,6 +3,7 @@ import moment from 'moment';
import {
assert,
currency,
makeLogger,
promisify,
promisifyModel,
......@@ -11,6 +12,7 @@ import {
shouldIncludeInOutstandingSum
} from '../helpers';
import Settings from './settings';
import Transactions from './transactions';
let log = makeLogger('models/accounts');
......@@ -172,4 +174,12 @@ Account.prototype.getUserId = async function getUserId() {
return process.kresus.user.id;
};
Account.prototype.getCurrencyFormatter = async function getCurrencyFormatter() {
let curr = currency.isKnown(this.currency)
? this.currency
: (await Settings.findOrCreateDefault(await this.getUserId(), 'default-currency')).value;
return currency.makeFormat(curr);
};
module.exports = 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