Commit 0ba27260 authored by ZeHiro's avatar ZeHiro
Browse files

Add the date of operations to operation alerts. Fixes #277.

Currency format alerts.
Use locale date format for reports.
parent 2d806014
......@@ -6,6 +6,8 @@ import { maybeHas as maybeHas_,
currency as currency_ } from './shared/helpers.js';
import errors from './shared/errors.json';
import moment from 'moment';
export let has = maybeHas_;
export let setupTranslator = setupTranslator_;
export let translate = translate_;
......@@ -119,3 +121,15 @@ export function isCredentialError(err) {
err.errCode === getErrorCode('INVALID_PARAMETERS') ||
err.errCode === getErrorCode('NO_PASSWORD');
}
export function setupMoment(locale) {
if (locale) {
moment.locale(locale);
} else {
moment.locale('en');
}
}
export function formatDateToLocaleString(date) {
return moment(date).format('L');
}
import { makeLogger, setupTranslator } from './helpers';
import { makeLogger, setupTranslator, setupMoment } from './helpers';
import * as Migrations from './models/migrations';
import * as Bank from './models/bank';
......@@ -15,7 +15,10 @@ let log = makeLogger('init');
// See comment in index.js.
module.exports = async function (app, server, callback) {
try {
setupTranslator(await Settings.getLocale());
// Localize Kresus
let locale = await Settings.getLocale();
setupTranslator(locale);
setupMoment(locale);
// Do data migrations first
log.info('Applying data migrations...');
......
......@@ -3,8 +3,9 @@ import Notifications from './notifications';
import Account from '../models/account';
import Alert from '../models/alert';
import Config from '../models/config';
import { makeLogger, translate as $t } from '../helpers';
import { makeLogger, translate as $t, currency } from '../helpers';
let log = makeLogger('alert-manager');
......@@ -32,7 +33,6 @@ ${$t('server.email.signature')}
// Send email notification
let content = this.wrapContent(text);
let fullSubject = `Kresus - ${subject}`;
await Emailer.sendToUser({
......@@ -45,11 +45,16 @@ ${$t('server.email.signature')}
async checkAlertsForOperations(operations) {
try {
let defaultCurrency = await Config.byName('defaultCurrency').value;
// Map account to names
let accounts = await Account.all();
let accountNames = new Map;
let accountsMap = new Map;
for (let a of accounts) {
accountNames.set(a.accountNumber, a.title);
accountsMap.set(a.accountNumber, {
title: a.title,
formatter: currency.makeFormat(a.currency || defaultCurrency)
});
}
// Map accounts to alerts
......@@ -72,14 +77,15 @@ ${$t('server.email.signature')}
continue;
}
let accountName = accountNames.get(operation.bankAccount);
// Set the account information
let { title: accountName, formatter } = accountsMap.get(operation.bankAccount);
for (let alert of alerts) {
if (!alert.testTransaction(operation))
continue;
let text =
alert.formatOperationMessage(operation, accountName);
alert.formatOperationMessage(operation, accountName, formatter);
await this.send({
subject: $t('server.alert.operation.title'),
text
......@@ -93,6 +99,8 @@ ${$t('server.email.signature')}
async checkAlertsForAccounts() {
try {
let defaultCurrency = await Config.byName('defaultCurrency').value;
let accounts = await Account.all();
for (let account of accounts) {
let alerts = await Alert.byAccountAndType(account.accountNumber,
......@@ -105,9 +113,11 @@ ${$t('server.email.signature')}
if (!alert.testBalance(balance))
continue;
// Set the currency formatter
let curr = account.currency || defaultCurrency;
let currencyFormatter = currency.makeFormat(curr);
let text =
alert.formatAccountMessage(account.title, balance);
alert.formatAccountMessage(account.title, balance, currencyFormatter);
await this.send({
subject: $t('server.alert.balance.title'),
text
......
import * as americano from 'cozydb';
import { makeLogger, promisify, promisifyModel,
translate as $t } from '../helpers';
import { makeLogger, promisify, promisifyModel, translate as $t,
formatDateToLocaleString } from '../helpers';
let log = makeLogger('models/alert');
......@@ -89,29 +89,32 @@ Alert.prototype.testBalance = function(balance) {
(this.order === 'gt' && balance >= alertLimit);
};
Alert.prototype.formatOperationMessage = function(operation, accountName) {
Alert.prototype.formatOperationMessage = function(operation, accountName, currencyFormatter) {
let cmp = this.order === 'lt' ? $t('server.alert.operation.lessThan') :
$t('server.alert.operation.greaterThan');
let amount = operation.amount;
let amount = currencyFormatter(operation.amount);
let account = accountName;
let title = operation.title;
let date = formatDateToLocaleString(operation.date);
return $t('server.alert.operation.content', {
title,
account,
amount,
cmp,
limit: this.limit
date,
limit: currencyFormatter(this.limit)
});
};
Alert.prototype.formatAccountMessage = function(title, balance) {
Alert.prototype.formatAccountMessage = function(title, balance, currencyFormatter) {
let cmp = this.order === 'lt' ? $t('server.alert.balance.lessThan') :
$t('server.alert.balance.greaterThan');
return $t('server.alert.balance.content', {
title,
cmp,
limit: this.limit,
balance
limit: currencyFormatter(this.limit),
balance: currencyFormatter(balance)
});
};
......
......@@ -360,13 +360,13 @@ module.exports = {
title: 'Alert on transaction amount',
lessThan: 'less than',
greaterThan: 'greater than',
content: `Alert: the transaction "%{title}" on the account "%{account}" has an amount of %{amount}, %{cmp} %{limit}.`
content: `Alert: the transaction "%{title}" from %{date} on the account "%{account}" has an amount of %{amount}, %{cmp} %{limit}.`
},
balance: {
title: 'Alert on balance amount',
lessThan: 'below the',
greaterThan: 'above the',
content: 'Alert: the balance on the account %{title} is %{cmp} alert threshold of %{limit}, with a balance of %{balance}.'
content: 'Alert: the balance on the account %{title} is %{cmp} alert threshold of %{limit}, with a balance of %{balance}.'
}
},
......
......@@ -360,13 +360,13 @@ module.exports = {
title: 'Alerte sur transaction',
lessThan: 'inférieur',
greaterThan: 'supérieur',
content: `Alerte : transaction "%{title}" (compte %{account}) d'un montant de %{amount}, %{cmp} à %{limit}.`
content: `Alerte : transaction "%{title}" du %{date} (compte %{account}) d'un montant de %{amount}, %{cmp} à %{limit}.`
},
balance: {
title: 'Alerte sur solde de compte',
lessThan: 'sous le',
greaterThan: 'au dessus du',
content: `Alerte : le solde sur le compte %{title} est %{cmp} seuil d'alerte de %{limit}, avec un solde de %{balance}.`
content: `Alerte : le solde sur le compte %{title} est %{cmp} seuil d'alerte de %{limit}, avec un solde de %{balance}.`
}
},
......
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