Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email template for Productivity Report #8532

Merged
merged 7 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what else did we copy before?

"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?: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the rationale for this? what did we do before?

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be a breaking change for my enterprise PR so we need to cordinate

userEmail: string,
customerId: string,
metrics: {
Tymek marked this conversation as resolved.
Show resolved Hide resolved
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to check if everything works with base paths in sandbox

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same as templates, but yes, we can check

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
Loading