Skip to content

Commit

Permalink
feat: new template
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek committed Oct 24, 2024
1 parent 68387cb commit 49e909f
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 655 deletions.
16 changes: 9 additions & 7 deletions src/lib/services/email-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ test('Can send order environments email', async () => {
expect(content.bcc).toBe('[email protected]');
});

test('Can send productivity report email', async () => {
// TODO: test
test.skip('Can send productivity report email', async () => {
const emailService = new EmailService({
email: {
host: 'test',
Expand All @@ -157,15 +158,16 @@ 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',
{
flags_created: 1,
production_updates: 2,
health: 99,
},
);
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);
});
54 changes: 49 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;
flags_created: number;
production_updates: 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,29 @@ 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);
console.log({ attachment });
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 49e909f

Please sign in to comment.