From faaf54ca998433186b29d481de5fbfae0e4db49d Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Wed, 23 Oct 2024 11:53:43 +0200 Subject: [PATCH] feat: email service for productivity report (#8517) Co-authored-by: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> --- src/lib/services/email-service.test.ts | 26 ++ src/lib/services/email-service.ts | 59 ++++ src/lib/types/experimental.ts | 7 +- .../productivity-report.html.mustache | 320 ++++++++++++++++++ .../productivity-report.plain.mustache | 5 + 5 files changed, 416 insertions(+), 1 deletion(-) create mode 100644 src/mailtemplates/productivity-report/productivity-report.html.mustache create mode 100644 src/mailtemplates/productivity-report/productivity-report.plain.mustache diff --git a/src/lib/services/email-service.test.ts b/src/lib/services/email-service.test.ts index bc80bf148d..af53768cd4 100644 --- a/src/lib/services/email-service.test.ts +++ b/src/lib/services/email-service.test.ts @@ -143,3 +143,29 @@ test('Can send order environments email', async () => { expect(content.html.includes(customerId)).toBe(true); expect(content.bcc).toBe('bcc@bcc.com'); }); + +test('Can send productivity report email', async () => { + const emailService = new EmailService({ + email: { + host: 'test', + port: 587, + secure: false, + smtpuser: '', + smtppass: '', + sender: 'noreply@getunleash.ai', + }, + getLogger: noLoggerProvider, + } as unknown as IUnleashConfig); + + const customerId = 'customer133'; + + const content = await emailService.sendProductivityReportEmail( + 'user@user.com', + customerId, + ); + expect(content.from).toBe('noreply@getunleash.ai'); + expect(content.subject).toBe('Unleash - productivity report'); + expect( + content.html.includes(`Productivity report for customer133`), + ).toBe(true); +}); diff --git a/src/lib/services/email-service.ts b/src/lib/services/email-service.ts index 94e60a0c9f..54c1e917cd 100644 --- a/src/lib/services/email-service.ts +++ b/src/lib/services/email-service.ts @@ -34,6 +34,7 @@ const RESET_MAIL_SUBJECT = 'Unleash - Reset your password'; const GETTING_STARTED_SUBJECT = 'Welcome to Unleash'; const ORDER_ENVIRONMENTS_SUBJECT = 'Unleash - ordered environments successfully'; +const PRODUCTIVITY_REPORT = 'Unleash - productivity report'; const SCHEDULED_CHANGE_CONFLICT_SUBJECT = 'Unleash - Scheduled changes can no longer be applied'; const SCHEDULED_EXECUTION_FAILED_SUBJECT = @@ -520,6 +521,64 @@ export class EmailService { }); } + async sendProductivityReportEmail( + userEmail: string, + customerId: string, + ): Promise { + if (this.configured()) { + const context = { + userEmail, + customerId, + }; + + const bodyHtml = await this.compileTemplate( + 'productivity-report', + TemplateFormat.HTML, + context, + ); + const bodyText = await this.compileTemplate( + 'productivity-report', + TemplateFormat.PLAIN, + context, + ); + const email = { + from: this.sender, + to: userEmail, + bcc: '', + subject: PRODUCTIVITY_REPORT, + html: bodyHtml, + text: bodyText, + }; + process.nextTick(() => { + this.mailer!.sendMail(email).then( + () => + this.logger.info( + 'Successfully sent productivity report email', + ), + (e) => + this.logger.warn( + 'Failed to send productivity report 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', + ); + res({ + from: this.sender, + to: userEmail, + bcc: '', + subject: PRODUCTIVITY_REPORT, + html: '', + text: '', + }); + }); + } + isEnabled(): boolean { return this.mailer !== undefined; } diff --git a/src/lib/types/experimental.ts b/src/lib/types/experimental.ts index defd86a4a6..b7441a4d97 100644 --- a/src/lib/types/experimental.ts +++ b/src/lib/types/experimental.ts @@ -63,7 +63,8 @@ export type IFlagKey = | 'webhookDomainLogging' | 'addonUsageMetrics' | 'releasePlans' - | 'navigationSidebar'; + | 'navigationSidebar' + | 'productivityReportEmail'; export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>; @@ -316,6 +317,10 @@ const flags: IFlags = { process.env.UNLEASH_EXPERIMENTAL_SIDEBAR_NAVIGATION, true, ), + productivityReportEmail: parseEnvVarBoolean( + process.env.UNLEASH_EXPERIMENTAL_PRODUCTIVITY_REPORT_EMAIL, + false, + ), }; export const defaultExperimentalOptions: IExperimentalOptions = { diff --git a/src/mailtemplates/productivity-report/productivity-report.html.mustache b/src/mailtemplates/productivity-report/productivity-report.html.mustache new file mode 100644 index 0000000000..d8c23d4dd1 --- /dev/null +++ b/src/mailtemplates/productivity-report/productivity-report.html.mustache @@ -0,0 +1,320 @@ + + + + + *|MC:SUBJECT|* + + + + Productivity report for {{customerId}} + + diff --git a/src/mailtemplates/productivity-report/productivity-report.plain.mustache b/src/mailtemplates/productivity-report/productivity-report.plain.mustache new file mode 100644 index 0000000000..6d9094ab55 --- /dev/null +++ b/src/mailtemplates/productivity-report/productivity-report.plain.mustache @@ -0,0 +1,5 @@ +Subject: Unleash productivity report + +Hello, + +Productivity report