refactor: move repetitive code into a new method in mailQueue

This commit is contained in:
Jovan Krunić
2020-10-23 10:12:21 +02:00
committed by Rainer Killinger
parent af305aa196
commit dd6ea1c6f3

View File

@@ -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<string>(() => 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<string>(() => (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<string>(() => (this.transport as SMTP).sendMail(mail));
await this.addToQueue(mail);
}
}
}