Skip to content

Commit

Permalink
Email template for Productivity Report (#8532)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek authored Oct 25, 2024
1 parent 3076bb4 commit 7c37de3
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 327 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 11 additions & 6 deletions src/lib/services/email-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
'[email protected]',
customerId,
'customerId',
{
flagsCreated: 1,
productionUpdates: 2,
health: 99,
},
);
console.log(content);
expect(content.from).toBe('[email protected]');
expect(content.subject).toBe('Unleash - productivity report');
expect(
content.html.includes(`<b>Productivity report for customer133</b>`),
).toBe(true);
expect(content.html.includes(`Productivity Report`)).toBe(true);
});
53 changes: 48 additions & 5 deletions src/lib/services/email-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -522,32 +527,48 @@ export class EmailService {
}

async sendProductivityReportEmail(
userName: string,
userEmail: string,
customerId: string,
metrics: {
health: number;
flagsCreated: number;
productionUpdates: number;
},
): Promise<IEmailEnvelope> {
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(
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 7c37de3

Please sign in to comment.