Commit 98d49fa7 authored by Benjamin Bouvier's avatar Benjamin Bouvier
Browse files

Fixups to !1247

parent ba01a1b1
Pipeline #325817 passed with stages
in 14 minutes and 4 seconds
......@@ -18,20 +18,21 @@ import {
} from '../helpers';
function postSave(userId: number, key: string, value: string) {
let notifier = null;
switch (key) {
case 'email-recipient':
notifier = getEmailer();
if (notifier !== null) {
notifier.forceReinit(value);
case 'email-recipient': {
const emailSender = getEmailer();
if (emailSender !== null) {
emailSender.forceReinit(value);
}
break;
case 'apprise-url':
notifier = getNotifier(userId);
}
case 'apprise-url': {
const notifier = getNotifier(userId);
if (notifier !== null) {
notifier.forceReinit(value);
}
break;
}
case 'locale':
setupTranslator(value);
break;
......
......@@ -22,11 +22,12 @@ ${$t('server.email.signature')}
userId: number,
{ subject, text }: { subject: string; text: string }
): Promise<void> {
let notificationsSent = false;
let sentNotifications = false;
const notifier = getNotifier(userId);
if (notifier !== null) {
await notifier.send(subject, text);
notificationsSent = true;
sentNotifications = true;
}
const emailer = getEmailer();
......@@ -40,13 +41,13 @@ ${$t('server.email.signature')}
content,
});
notificationsSent = true;
sentNotifications = true;
}
if (notificationsSent) {
log.info('Notification sent.');
if (sentNotifications) {
log.info('Notifications have been sent.');
} else {
log.info('No notifier or email found, no notification sent.');
log.info('No notifier or email sender found, no notifications sent.');
}
}
......@@ -56,10 +57,7 @@ ${$t('server.email.signature')}
operations: Transaction[]
): Promise<void> {
try {
const notifier = getNotifier(userId);
const emailer = getEmailer();
if (notifier === null && emailer === null) {
if (getNotifier(userId) === null && getEmailer() === null) {
log.info('No notifier or emailer found, skipping transactions alerts check.');
return;
}
......@@ -123,11 +121,8 @@ ${$t('server.email.signature')}
async checkAlertsForAccounts(userId: number, access: Access): Promise<void> {
try {
const notifier = getNotifier(userId);
const emailer = getEmailer();
if (notifier === null && emailer === null) {
log.info('No notifier or emailer found, accounts alerts check.');
if (getNotifier(userId) === null && getEmailer() === null) {
log.info('No notifier or emailer found, skipping transactions alerts check.');
return;
}
......
......@@ -17,7 +17,7 @@ interface SendOptions {
html?: string;
}
class Emailer {
export class Emailer {
fromEmail: string | null = null;
toEmail: string | null = null;
transport: Mail | null = null;
......@@ -153,7 +153,10 @@ class Emailer {
let EMAILER: Emailer | null = null;
function getEmailer(): Emailer | null {
if (EMAILER === null && isEmailEnabled()) {
if (!isEmailEnabled()) {
return null;
}
if (EMAILER === null) {
EMAILER = new Emailer();
}
return EMAILER;
......
......@@ -67,7 +67,10 @@ class Notifier {
let NOTIFIER: Notifier | null = null;
function _getBaseNotifier(): Notifier | null {
if (NOTIFIER === null && isAppriseApiEnabled()) {
if (!isAppriseApiEnabled()) {
return null;
}
if (NOTIFIER === null) {
NOTIFIER = new Notifier();
}
return NOTIFIER;
......@@ -106,21 +109,21 @@ class UserNotifier {
}
const notifier = _getBaseNotifier();
if (notifier) {
notifier._send({ subject, content, appriseUrl: this.appriseUserUrl });
} else {
assert(false, 'Notifier.send misuse: no notifier available');
}
assert(notifier !== null, 'Notifier.send misuse: no notifier available');
notifier._send({ subject, content, appriseUrl: this.appriseUserUrl });
}
}
const NOTIFIER_PER_USER_ID: { [k: string]: UserNotifier } = {};
function getNotifier(userId: number): UserNotifier | null {
if (isAppriseApiEnabled() && !(userId in NOTIFIER_PER_USER_ID)) {
if (!isAppriseApiEnabled()) {
return null;
}
if (!(userId in NOTIFIER_PER_USER_ID)) {
log.info(`Notifier initialized for user ${userId}`);
NOTIFIER_PER_USER_ID[userId] = new UserNotifier(userId);
}
return NOTIFIER_PER_USER_ID[userId] || null;
return NOTIFIER_PER_USER_ID[userId];
}
export async function sendTestNotification(appriseUrl: string): Promise<void> {
......
......@@ -12,7 +12,7 @@ import {
} from '../helpers';
import { Access, Account, Alert, Transaction } from '../models';
import getEmailer from './emailer';
import getEmailer, { Emailer } from './emailer';
const log = makeLogger('report-manager');
......@@ -23,16 +23,18 @@ const MIN_DURATION_BETWEEN_REPORTS =
(24 + POLLER_START_LOW_HOUR - POLLER_START_HIGH_HOUR) * 60 * 60 * 1000;
class ReportManager {
async sendReport(userId: number, subject: string, content: string): Promise<void> {
const emailer = getEmailer();
if (emailer !== null) {
await emailer.sendToUser(userId, {
subject,
content,
});
async sendReport(
emailer: Emailer,
userId: number,
subject: string,
content: string
): Promise<void> {
await emailer.sendToUser(userId, {
subject,
content,
});
log.info('Report sent.');
}
log.info('Report sent.');
}
async manageReports(userId: number): Promise<void> {
......@@ -44,21 +46,21 @@ class ReportManager {
}
const now = new Date();
await this.prepareReport(userId, 'daily');
await this.prepareReport(emailer, userId, 'daily');
// getDay is indexed from 0, meaning Sunday.
if (now.getDay() === 1) {
await this.prepareReport(userId, 'weekly');
await this.prepareReport(emailer, userId, 'weekly');
}
// getDate starts from 1.
if (now.getDate() === 1) {
await this.prepareReport(userId, 'monthly');
await this.prepareReport(emailer, userId, 'monthly');
}
} catch (err) {
log.warn(`Error when preparing reports: ${err}\n${err.stack}`);
}
}
async prepareReport(userId: number, frequencyKey: string): Promise<void> {
async prepareReport(emailer: Emailer, userId: number, frequencyKey: string): Promise<void> {
log.info(`Checking if user has enabled ${frequencyKey} report...`);
let reports = await Alert.reportsByFrequency(userId, frequencyKey);
......@@ -129,7 +131,7 @@ class ReportManager {
const { subject, content } = email;
await this.sendReport(userId, subject, content);
await this.sendReport(emailer, userId, subject, content);
} else {
log.info('no operations to show in the report.');
}
......
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