diff --git a/package.json b/package.json index a3f8232a6d..4cd96cd7aa 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "main": "./dist/lib/server-impl.js", "scripts": { "start": "TZ=UTC node ./dist/server.js", - "copy-templates": "copyfiles -u 1 src/mailtemplates/**/*.mustache dist/", + "copy-templates": "copyfiles -u 1 src/mailtemplates/**/* dist/", "build:backend": "tsc --pretty --strictNullChecks false", "build:frontend": "yarn --cwd ./frontend run build", "build:frontend:if-needed": "./scripts/build-frontend-if-needed.sh", diff --git a/src/lib/services/email-service.test.ts b/src/lib/services/email-service.test.ts index af53768cd4..a8a914a37e 100644 --- a/src/lib/services/email-service.test.ts +++ b/src/lib/services/email-service.test.ts @@ -146,6 +146,9 @@ test('Can send order environments email', async () => { test('Can send productivity report email', async () => { const emailService = new EmailService({ + server: { + unleashUrl: 'http://localhost', + }, email: { host: 'test', port: 587, @@ -157,15 +160,17 @@ test('Can send productivity report email', async () => { getLogger: noLoggerProvider, } as unknown as IUnleashConfig); - const customerId = 'customer133'; - const content = await emailService.sendProductivityReportEmail( 'user@user.com', - customerId, + 'customerId', + { + flagsCreated: 1, + productionUpdates: 2, + 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 for customer133`), - ).toBe(true); + expect(content.html.includes(`Productivity Report`)).toBe(true); }); diff --git a/src/lib/services/email-service.ts b/src/lib/services/email-service.ts index 54c1e917cd..b46846c593 100644 --- a/src/lib/services/email-service.ts +++ b/src/lib/services/email-service.ts @@ -28,6 +28,11 @@ export interface IEmailEnvelope { subject: string; html: string; text: string; + attachments?: { + filename: string; + path: string; + cid: string; + }[]; } const RESET_MAIL_SUBJECT = 'Unleash - Reset your password'; @@ -522,32 +527,48 @@ export class EmailService { } async sendProductivityReportEmail( + userName: string, userEmail: string, - customerId: string, + metrics: { + health: number; + flagsCreated: number; + productionUpdates: number; + }, ): Promise { if (this.configured()) { const context = { + userName, userEmail, - customerId, + ...metrics, + unleashUrl: this.config.server.unleashUrl, }; + const template = 'productivity-report'; + const bodyHtml = await this.compileTemplate( - 'productivity-report', + template, TemplateFormat.HTML, context, ); const bodyText = await this.compileTemplate( - 'productivity-report', + template, TemplateFormat.PLAIN, context, ); - const email = { + const email: IEmailEnvelope = { from: this.sender, to: userEmail, bcc: '', subject: PRODUCTIVITY_REPORT, html: bodyHtml, text: bodyText, + attachments: [ + this.resolveTemplateAttachment( + template, + 'unleash-logo.png', + 'unleashLogo', + ), + ], }; process.nextTick(() => { this.mailer!.sendMail(email).then( @@ -613,6 +634,28 @@ export class EmailService { throw new NotFoundError('Could not find template'); } + private resolveTemplateAttachment( + templateName: string, + filename: string, + cid: string, + ): { + filename: string; + path: string; + cid: string; + } { + const topPath = path.resolve(__dirname, '../../mailtemplates'); + const attachment = path.join(topPath, templateName, filename); + if (existsSync(attachment)) { + return { + filename, + path: attachment, + cid, + }; + } + + throw new NotFoundError('Could not find email attachment'); + } + configured(): boolean { return this.sender !== 'not-configured' && this.mailer !== undefined; } diff --git a/src/mailtemplates/productivity-report/productivity-report.html.mustache b/src/mailtemplates/productivity-report/productivity-report.html.mustache index d8c23d4dd1..5ad6c3260a 100644 --- a/src/mailtemplates/productivity-report/productivity-report.html.mustache +++ b/src/mailtemplates/productivity-report/productivity-report.html.mustache @@ -1,320 +1,82 @@ - + + - - *|MC:SUBJECT|* - + Your Unleash Productivity Report - - Productivity report for {{customerId}} + + +
+
+ Unleash +
Your Monthly Productivity Report
+
+
+
+

Hi {{userName}},

+

We are excited to share the latest insights for your instance. As always if you + have any questions or concerns let us know - we are here for you.

+

Ready to dive into your numbers?

+
+
+
+
+ {{health}}%
+ your instance health +
+
+
+ + + + + + + + + +
+
+ {{flagsCreated}}
+ flags created last month +
+
+
+ {{productionUpdates}}
+ production updates last month +
+
+
+ Go to your Insights +
+
+ +
+ diff --git a/src/mailtemplates/productivity-report/unleash-logo.png b/src/mailtemplates/productivity-report/unleash-logo.png new file mode 100644 index 0000000000..687bf761e8 Binary files /dev/null and b/src/mailtemplates/productivity-report/unleash-logo.png differ