Skip to content

Commit

Permalink
feat: email service for productivity report
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Oct 23, 2024
1 parent 7ac33af commit 45e56a1
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 1 deletion.
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

0 comments on commit 45e56a1

Please sign in to comment.