From dd6ea1c6f391d6ce200d1f782b08ebfa07d7da6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Kruni=C4=87?= Date: Fri, 23 Oct 2020 10:12:21 +0200 Subject: [PATCH] refactor: move repetitive code into a new method in mailQueue --- src/notification/mail-queue.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/notification/mail-queue.ts b/src/notification/mail-queue.ts index 6eae69cb..b54f39a4 100644 --- a/src/notification/mail-queue.ts +++ b/src/notification/mail-queue.ts @@ -67,18 +67,27 @@ export class MailQueue { this.checkForVerification(); } + /** + * Adds a mail into the queue so it gets send when the queue is ready + * + * @param mail Information required for sending a mail + */ + private async addToQueue(mail: MailOptions) { + return this.queue.add(() => this.transport.sendMail(mail)); + } + /** * Verify the given transport */ private checkForVerification() { - if (this.verificationCounter > MailQueue.MAX_VERIFICATION_ATTEMPTS) { + if (this.verificationCounter >= MailQueue.MAX_VERIFICATION_ATTEMPTS) { throw new Error('Failed to initialize the SMTP transport for the mail queue'); } if (!this.transport.isVerified()) { this.verificationCounter++; - setTimeout(async () => { + setTimeout(() => { Logger.warn('Transport not verified yet. Trying to send mails here...'); this.checkForVerification(); }, MailQueue.VERIFICATION_TIMEOUT); @@ -86,7 +95,7 @@ export class MailQueue { Logger.ok('Transport for mail queue was verified. We can send mails now'); // if the transport finally was verified send all our mails from the dry queue this.dryQueue.forEach(async (mail) => { - await this.queue.add(() => (this.transport as SMTP).sendMail(mail)); + await this.addToQueue(mail); }); } } @@ -101,7 +110,7 @@ export class MailQueue { // push to a dry queue which gets pushed to the real queue when the transport is verified this.dryQueue.push(mail); } else { - await this.queue.add(() => (this.transport as SMTP).sendMail(mail)); + await this.addToQueue(mail); } } }