From 2ddcb347290ad4088614dbdf2f756b5c3b4a2f64 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Fri, 25 Oct 2024 13:41:26 +0200 Subject: [PATCH 1/2] feat(productivity-report): additional email headers --- src/lib/create-config.ts | 9 ++++++ src/lib/services/email-service.test.ts | 44 +++++++++++++++++++++++++- src/lib/services/email-service.ts | 11 +++++-- src/lib/types/option.ts | 1 + 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/lib/create-config.ts b/src/lib/create-config.ts index a3156fb9b0b4..3c60999d3775 100644 --- a/src/lib/create-config.ts +++ b/src/lib/create-config.ts @@ -341,6 +341,9 @@ const defaultImport: WithOptional = { environment: process.env.IMPORT_ENVIRONMENT ?? 'development', }; +const emailHeaderName = process.env.OPTIONAL_EMAIL_HEADER_NAME; +const emailHeaderValue = process.env.OPTIONAL_EMAIL_HEADER_VALUE; + const defaultEmail: IEmailOption = { host: process.env.EMAIL_HOST, secure: parseEnvVarBoolean(process.env.EMAIL_SECURE, false), @@ -348,6 +351,12 @@ const defaultEmail: IEmailOption = { sender: process.env.EMAIL_SENDER || 'Unleash ', smtpuser: process.env.EMAIL_USER, smtppass: process.env.EMAIL_PASSWORD, + optionalEmailHeaders: + emailHeaderName && emailHeaderValue + ? { + [emailHeaderName]: emailHeaderValue, + } + : {}, }; const dbPort = (dbConfig: Partial): Partial => { diff --git a/src/lib/services/email-service.test.ts b/src/lib/services/email-service.test.ts index a8a914a37eb0..5575b6be5f4f 100644 --- a/src/lib/services/email-service.test.ts +++ b/src/lib/services/email-service.test.ts @@ -169,8 +169,50 @@ test('Can send productivity report email', async () => { health: 99, }, ); - console.log(content); + expect(content.from).toBe('noreply@getunleash.ai'); expect(content.subject).toBe('Unleash - productivity report'); expect(content.html.includes(`Productivity Report`)).toBe(true); }); + +test('Should add optional headers to productivity email', async () => { + const emailService = new EmailService({ + server: { + unleashUrl: 'http://localhost', + }, + email: { + host: 'test', + port: 587, + secure: false, + smtpuser: '', + smtppass: '', + sender: 'noreply@getunleash.ai', + optionalEmailHeaders: { + 'x-header-name': 'value', + }, + }, + getLogger: noLoggerProvider, + } as unknown as IUnleashConfig); + + const passwordResetMail = await emailService.sendResetMail( + 'name', + 'user@example.com', + 'http://exempla.com', + ); + + const productivityMail = await emailService.sendProductivityReportEmail( + 'user@user.com', + 'customerId', + { + flagsCreated: 1, + productionUpdates: 2, + health: 99, + }, + ); + + expect(passwordResetMail.headers).toBeFalsy(); + + expect(productivityMail.headers).toStrictEqual({ + 'x-header-name': 'value', + }); +}); diff --git a/src/lib/services/email-service.ts b/src/lib/services/email-service.ts index b46846c593a7..64ee11e18973 100644 --- a/src/lib/services/email-service.ts +++ b/src/lib/services/email-service.ts @@ -33,6 +33,7 @@ export interface IEmailEnvelope { path: string; cid: string; }[]; + headers?: Record; } const RESET_MAIL_SUBJECT = 'Unleash - Reset your password'; @@ -555,7 +556,8 @@ export class EmailService { TemplateFormat.PLAIN, context, ); - const email: IEmailEnvelope = { + + const email = { from: this.sender, to: userEmail, bcc: '', @@ -569,7 +571,11 @@ export class EmailService { 'unleashLogo', ), ], - }; + headers: { + ...this.config.email.optionalEmailHeaders, + }, + } satisfies IEmailEnvelope; + process.nextTick(() => { this.mailer!.sendMail(email).then( () => @@ -585,6 +591,7 @@ export class EmailService { }); 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', diff --git a/src/lib/types/option.ts b/src/lib/types/option.ts index b9cb4957fe66..2df0eebbf710 100644 --- a/src/lib/types/option.ts +++ b/src/lib/types/option.ts @@ -167,6 +167,7 @@ export interface IEmailOption { smtpuser?: string; smtppass?: string; transportOptions?: SMTPTransport.Options; + optionalEmailHeaders?: Record; } export interface IListeningPipe { From 341a44cd963d5a5cbbc8b9a1fd0bf248fea6a5ca Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Fri, 25 Oct 2024 13:48:50 +0200 Subject: [PATCH 2/2] add config snapshot --- src/lib/__snapshots__/create-config.test.ts.snap | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/__snapshots__/create-config.test.ts.snap b/src/lib/__snapshots__/create-config.test.ts.snap index 74bdf34609d2..0c099bcee7db 100644 --- a/src/lib/__snapshots__/create-config.test.ts.snap +++ b/src/lib/__snapshots__/create-config.test.ts.snap @@ -53,6 +53,7 @@ exports[`should create default config 1`] = ` "disableScheduler": undefined, "email": { "host": undefined, + "optionalEmailHeaders": {}, "port": 587, "secure": false, "sender": "Unleash ",