From 3a2206d22847e3b79be22189004587b01237c15a Mon Sep 17 00:00:00 2001 From: Jaanus Sellin Date: Thu, 10 Oct 2024 11:33:21 +0300 Subject: [PATCH] feat: order environments service implementation (#8415) This can be consumed in enterprise repo. --- src/lib/services/email-service.test.ts | 29 +++++++++ src/lib/services/email-service.ts | 64 +++++++++++++++++++ .../order-environments.html.mustache | 4 +- .../order-environments.plain.mustache | 4 +- 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/lib/services/email-service.test.ts b/src/lib/services/email-service.test.ts index 6c67d75235..8f97838342 100644 --- a/src/lib/services/email-service.test.ts +++ b/src/lib/services/email-service.test.ts @@ -102,3 +102,32 @@ test('should strip special characters from email subject', async () => { ); expect(emailService.stripSpecialCharacters('tom-jones')).toBe('tom-jones'); }); + +test('Can send order environments email', async () => { + process.env.ORDER_ENVIRONMENTS_BCC = 'bcc@bcc.com'; + 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 environments = ['development', 'production']; + const content = await emailService.sendOrderEnvironmentEmail( + 'user@user.com', + customerId, + environments, + ); + expect(content.from).toBe('noreply@getunleash.ai'); + expect(content.subject).toBe('Unleash - ordered environments successfully'); + expect(content.html.includes(`
  • ${environments[0]}
  • `)).toBe(true); + expect(content.html.includes(`
  • ${environments[1]}
  • `)).toBe(true); + expect(content.html.includes(customerId)).toBe(true); + expect(content.bcc).toBe('bcc@bcc.com'); +}); diff --git a/src/lib/services/email-service.ts b/src/lib/services/email-service.ts index 24646e985c..c16cf4e765 100644 --- a/src/lib/services/email-service.ts +++ b/src/lib/services/email-service.ts @@ -24,6 +24,7 @@ export enum TransporterType { export interface IEmailEnvelope { from: string; to: string; + bcc?: string; subject: string; html: string; text: string; @@ -31,6 +32,8 @@ export interface IEmailEnvelope { const RESET_MAIL_SUBJECT = 'Unleash - Reset your password'; const GETTING_STARTED_SUBJECT = 'Welcome to Unleash'; +const ORDER_ENVIRONMENTS_SUBJECT = + 'Unleash - ordered environments successfully'; const SCHEDULED_CHANGE_CONFLICT_SUBJECT = 'Unleash - Scheduled changes can no longer be applied'; const SCHEDULED_EXECUTION_FAILED_SUBJECT = @@ -447,6 +450,67 @@ export class EmailService { }); } + async sendOrderEnvironmentEmail( + userEmail: string, + customerId: string, + environmentNames: string[], + ): Promise { + if (this.configured()) { + const context = { + userEmail, + customerId, + environments: environmentNames.map((name) => + this.stripSpecialCharacters(name), + ), + }; + const bodyHtml = await this.compileTemplate( + 'order-environments', + TemplateFormat.HTML, + context, + ); + const bodyText = await this.compileTemplate( + 'order-environments', + TemplateFormat.PLAIN, + context, + ); + const email = { + from: this.sender, + to: userEmail, + bcc: process.env.ORDER_ENVIRONMENTS_BCC || 'ivar@getunleash.io', + subject: ORDER_ENVIRONMENTS_SUBJECT, + html: bodyHtml, + text: bodyText, + }; + process.nextTick(() => { + this.mailer!.sendMail(email).then( + () => + this.logger.info( + 'Successfully sent order environments email', + ), + (e) => + this.logger.warn( + 'Failed to send order environments 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: ORDER_ENVIRONMENTS_SUBJECT, + html: '', + text: '', + }); + }); + } + isEnabled(): boolean { return this.mailer !== undefined; } diff --git a/src/mailtemplates/order-environments/order-environments.html.mustache b/src/mailtemplates/order-environments/order-environments.html.mustache index b1dde723e4..e2971d2ce9 100644 --- a/src/mailtemplates/order-environments/order-environments.html.mustache +++ b/src/mailtemplates/order-environments/order-environments.html.mustache @@ -338,10 +338,10 @@

    Order for additional environments submitted

    Hello,

    -

    An order for additional environments has been successfully submitted by {{{ userEmail }}} for customer ID {{{ instanceId }}}. Below are the details of the environments requested:

    +

    An order for additional environments has been successfully submitted by {{{ userEmail }}} for customer ID {{{ customerId }}}. Below are the details of the environments requested:

    Please note that it may take up to 24 hours for these changes to come into effect.

    diff --git a/src/mailtemplates/order-environments/order-environments.plain.mustache b/src/mailtemplates/order-environments/order-environments.plain.mustache index 2075f983a5..e12da06c27 100644 --- a/src/mailtemplates/order-environments/order-environments.plain.mustache +++ b/src/mailtemplates/order-environments/order-environments.plain.mustache @@ -2,10 +2,10 @@ Subject: Order for additional environments submitted Hello, -An order for additional environments has been successfully submitted by {{ userEmail }} for customer ID {{ instanceId }}. Below are the details of the environments requested: +An order for additional environments has been successfully submitted by {{ userEmail }} for customer ID {{ customerId }}. Below are the details of the environments requested: {{#environments}} - - {{ name }} + - {{.}} {{/environments}} Please note that it may take up to 24 hours for these changes to come into effect.