Skip to content

Commit

Permalink
feat: new email template for scheduled-execution-failed email (#5519)
Browse files Browse the repository at this point in the history
What it says on the box.

Relates to #
[1-1687](https://linear.app/unleash/issue/1-1687/send-an-email-when-the-scheduling-fails)


<img width="782" alt="Screenshot 2023-12-01 at 15 33 08"
src="https://github.com/Unleash/unleash/assets/104830839/513a0a71-5ca5-4ea7-8482-7903c8485737">

---------

Signed-off-by: andreas-unleash <[email protected]>
Co-authored-by: Thomas Heartman <[email protected]>
  • Loading branch information
andreas-unleash and thomasheartman authored Dec 1, 2023
1 parent fba8022 commit be17b7f
Show file tree
Hide file tree
Showing 3 changed files with 600 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/lib/services/email-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface IEmailEnvelope {

const RESET_MAIL_SUBJECT = 'Unleash - Reset your password';
const GETTING_STARTED_SUBJECT = 'Welcome to Unleash';
const SCHEDULED_EXECUTION_FAILED_SUBJECT =
'Unleash - Scheduled change request could not be applied';

export const MAIL_ACCEPTED = '250 Accepted';

Expand Down Expand Up @@ -68,6 +70,76 @@ export class EmailService {
}
}

async sendScheduledExecutionFailedEmail(
name: string,
recipient: string,
changeRequestLink: string,
changeRequestTitle: string,
scheduledAt: string,
errorMessage: string,
): Promise<IEmailEnvelope> {
if (this.configured()) {
const year = new Date().getFullYear();
const bodyHtml = await this.compileTemplate(
'scheduled-execution-failed',
TemplateFormat.HTML,
{
changeRequestLink,
scheduledAt,
errorMessage,
name,
year,
},
);
const bodyText = await this.compileTemplate(
'scheduled-execution-failed',
TemplateFormat.PLAIN,
{
changeRequestLink,
changeRequestTitle,
scheduledAt,
errorMessage,
name,
year,
},
);
const email = {
from: this.sender,
to: recipient,
subject: SCHEDULED_EXECUTION_FAILED_SUBJECT,
html: bodyHtml,
text: bodyText,
};
process.nextTick(() => {
this.mailer!.sendMail(email).then(
() =>
this.logger.info(
'Successfully sent scheduled-execution-failed email',
),
(e) =>
this.logger.warn(
'Failed to send scheduled-execution-failed email',
e,
),
);
});
return Promise.resolve(email);
}
return new Promise((res) => {
this.logger.warn(
'No mailer is configured. Please read the docs on how to configure an email service',
);
this.logger.debug('Change request link: ', changeRequestLink);
res({
from: this.sender,
to: recipient,
subject: SCHEDULED_EXECUTION_FAILED_SUBJECT,
html: '',
text: '',
});
});
}

async sendResetMail(
name: string,
recipient: string,
Expand Down
Loading

0 comments on commit be17b7f

Please sign in to comment.