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

feat: email service for productivity report #8517

Merged
merged 1 commit into from
Oct 23, 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
26 changes: 26 additions & 0 deletions src/lib/services/email-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,29 @@ test('Can send order environments email', async () => {
expect(content.html.includes(customerId)).toBe(true);
expect(content.bcc).toBe('[email protected]');
});

test('Can send productivity report email', async () => {
const emailService = new EmailService({
email: {
host: 'test',
port: 587,
secure: false,
smtpuser: '',
smtppass: '',
sender: '[email protected]',
},
getLogger: noLoggerProvider,
} as unknown as IUnleashConfig);

const customerId = 'customer133';

const content = await emailService.sendProductivityReportEmail(
'[email protected]',
customerId,
);
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);
});
59 changes: 59 additions & 0 deletions src/lib/services/email-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const RESET_MAIL_SUBJECT = 'Unleash - Reset your password';
const GETTING_STARTED_SUBJECT = 'Welcome to Unleash';
const ORDER_ENVIRONMENTS_SUBJECT =
'Unleash - ordered environments successfully';
const PRODUCTIVITY_REPORT = 'Unleash - productivity report';
const SCHEDULED_CHANGE_CONFLICT_SUBJECT =
'Unleash - Scheduled changes can no longer be applied';
const SCHEDULED_EXECUTION_FAILED_SUBJECT =
Expand Down Expand Up @@ -520,6 +521,64 @@ export class EmailService {
});
}

async sendProductivityReportEmail(
userEmail: string,
customerId: string,
): Promise<IEmailEnvelope> {
if (this.configured()) {
const context = {
userEmail,
customerId,
};

const bodyHtml = await this.compileTemplate(
'productivity-report',
TemplateFormat.HTML,
context,
);
const bodyText = await this.compileTemplate(
'productivity-report',
TemplateFormat.PLAIN,
context,
);
const email = {
from: this.sender,
to: userEmail,
bcc: '',
subject: PRODUCTIVITY_REPORT,
html: bodyHtml,
text: bodyText,
};
process.nextTick(() => {
this.mailer!.sendMail(email).then(
() =>
this.logger.info(
'Successfully sent productivity report email',
),
(e) =>
this.logger.warn(
'Failed to send productivity report email',
e,
),
);
});
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',
);
res({
from: this.sender,
to: userEmail,
bcc: '',
subject: PRODUCTIVITY_REPORT,
html: '',
text: '',
});
});
}

isEnabled(): boolean {
return this.mailer !== undefined;
}
Expand Down
7 changes: 6 additions & 1 deletion src/lib/types/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export type IFlagKey =
| 'webhookDomainLogging'
| 'addonUsageMetrics'
| 'releasePlans'
| 'navigationSidebar';
| 'navigationSidebar'
| 'productivityReportEmail';

export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>;

Expand Down Expand Up @@ -316,6 +317,10 @@ const flags: IFlags = {
process.env.UNLEASH_EXPERIMENTAL_SIDEBAR_NAVIGATION,
true,
),
productivityReportEmail: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_PRODUCTIVITY_REPORT_EMAIL,
false,
),
};

export const defaultExperimentalOptions: IExperimentalOptions = {
Expand Down
Loading
Loading