Commit 69d226cf authored by Benjamin Bouvier's avatar Benjamin Bouvier
Browse files

Enhance messages of notifications on transactions and refactor code;

parent 28eb41d7
......@@ -13,53 +13,77 @@ class AlertManager
constructor() {
if (process.kresus.standalone) {
log.warn('report manager not implemented yet in standalone mode');
return;
}
}
wrapContent(content) {
return `${$t('server.email.hello')}
${content}
${$t('server.email.seeyoulater.notifications')},
${$t('server.email.signature')}
`;
}
async send({ subject, text }) {
// Send cozy notification
Notifications.send(text);
// Send email notification
let content = this.wrapContent(text);
let fullSubject = `Kresus - ${subject}`;
await Emailer.sendToUser({
subject: fullSubject,
content
});
log.info('Notification sent.');
}
async checkAlertsForOperations(operations) {
try {
// Map account to names
let accounts = await Account.all();
let accountNames = new Map;
for (let a of accounts) {
accountNames.set(a.accountNumber, a.title);
}
// Map accounts to alerts
let alertsByAccount = new Map;
for (let operation of operations) {
// Memoize alerts by account
let alerts = alertsByAccount.get(operation.bankAccount);
if (typeof alerts === 'undefined') {
let alerts;
if (!alertsByAccount.has(operation.bankAccount)) {
alerts = await Alert.byAccountAndType(operation.bankAccount,
'transaction');
alertsByAccount.set(operation.bankAccount, alerts);
} else {
alerts = alertsByAccount.get(operation.bankAccount);
}
// Skip operations for which the account has no alerts
if (!alerts || !alerts.length) {
continue;
}
let accountName = accountNames.get(operation.bankAccount);
for (let alert of alerts) {
if (!alert.testTransaction(operation))
continue;
// Send cozy notification
Notifications.send(alert.formatOperationMessage(operation));
// Send email notification
let content =
`${$t('server.email.hello')}
${alert.formatOperationMessage(operation)}
${$t('server.email.seeyoulater.notifications')},
${$t('server.email.signature')}
`;
let subject = $t('server.alert.operation.title');
subject = `Kresus - ${subject}`;
let text =
alert.formatOperationMessage(operation, accountName);
await Emailer.sendToUser({
subject,
content
await this.send({
subject: $t('server.alert.operation.title'),
text
});
log.info('Notification sent.');
}
}
} catch (err) {
......@@ -81,27 +105,13 @@ ${$t('server.email.signature')}
if (!alert.testBalance(balance))
continue;
// Cozy notification
let message =
let text =
alert.formatAccountMessage(account.title, balance);
Notifications.send(message);
// Send email notification
let content =
`${$t('server.email.hello')}
${alert.formatAccountMessage(account.title, balance)}
${$t('server.email.seeyoulater.notifications')},
${$t('server.email.signature')}
`;
await Emailer.sendToUser({
subject: `Kresus - ${$t('server.alert.balance.title')}`,
content
await this.send({
subject: $t('server.alert.balance.title'),
text
});
log.info('Notification sent.');
}
}
} catch (err) {
......
......@@ -89,11 +89,11 @@ Alert.prototype.testBalance = function(balance) {
(this.order === 'gt' && balance >= alertLimit);
};
Alert.prototype.formatOperationMessage = function(operation) {
Alert.prototype.formatOperationMessage = function(operation, accountName) {
let cmp = this.order === 'lt' ? $t('server.alert.operation.lessThan') :
$t('server.alert.operation.greaterThan');
let amount = operation.amount;
let account = operation.bankAccount;
let account = accountName;
let title = operation.title;
return $t('server.alert.operation.content', {
title,
......
......@@ -359,7 +359,7 @@ module.exports = {
title: 'Alert on transaction amount',
lessThan: 'less than',
greaterThan: 'greater than',
content: `Alert: transaction "%{title}" (account %{account}) has an amount of %{amount}€, %{cmp} %{limit}€.`
content: `Alert: the transaction "%{title}" on the account "%{account}" has an amount of %{amount}€, %{cmp} %{limit}€.`
},
balance: {
title: 'Alert on balance amount',
......@@ -371,7 +371,10 @@ module.exports = {
email: {
hello: 'Dear Kresus user,',
signature: 'Yours truly, Kresus.',
signature: `Yours truly, Kresus.
(if you would like to unsubscribe or change the frequency to which you receive notifications, log into your Kresus and go to Settings > Emails)
`,
seeyoulater: {
notifications: 'See you soon for new notifications',
report: 'See you soon for another report'
......
......@@ -371,7 +371,10 @@ module.exports = {
email: {
hello: 'Bonjour cher.e utilisateur.rice de Kresus,',
signature: 'Votre serviteur, Kresus.',
signature: `Votre serviteur, Kresus.
(si vous souhaitez vous désinscrire de ces notifications ou modifier la fréquence à laquelle celles-ci arrivent, connectez-vous à votre Kresus et visitez l'onglet Préférences, puis Emails)
`,
seeyoulater: {
notifications: 'A bientôt pour de nouvelles notifications',
report: 'A bientôt pour un autre rapport'
......
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