1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

feat: order environments service implementation (#8415)

This can be consumed in enterprise repo.
This commit is contained in:
Jaanus Sellin 2024-10-10 11:33:21 +03:00 committed by GitHub
parent 534dd093d2
commit 3a2206d228
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 97 additions and 4 deletions

View File

@ -102,3 +102,32 @@ test('should strip special characters from email subject', async () => {
);
expect(emailService.stripSpecialCharacters('tom-jones')).toBe('tom-jones');
});
test('Can send order environments email', async () => {
process.env.ORDER_ENVIRONMENTS_BCC = 'bcc@bcc.com';
const emailService = new EmailService({
email: {
host: 'test',
port: 587,
secure: false,
smtpuser: '',
smtppass: '',
sender: 'noreply@getunleash.ai',
},
getLogger: noLoggerProvider,
} as unknown as IUnleashConfig);
const customerId = 'customer133';
const environments = ['development', 'production'];
const content = await emailService.sendOrderEnvironmentEmail(
'user@user.com',
customerId,
environments,
);
expect(content.from).toBe('noreply@getunleash.ai');
expect(content.subject).toBe('Unleash - ordered environments successfully');
expect(content.html.includes(`<li>${environments[0]}</li>`)).toBe(true);
expect(content.html.includes(`<li>${environments[1]}</li>`)).toBe(true);
expect(content.html.includes(customerId)).toBe(true);
expect(content.bcc).toBe('bcc@bcc.com');
});

View File

@ -24,6 +24,7 @@ export enum TransporterType {
export interface IEmailEnvelope {
from: string;
to: string;
bcc?: string;
subject: string;
html: string;
text: string;
@ -31,6 +32,8 @@ export interface IEmailEnvelope {
const RESET_MAIL_SUBJECT = 'Unleash - Reset your password';
const GETTING_STARTED_SUBJECT = 'Welcome to Unleash';
const ORDER_ENVIRONMENTS_SUBJECT =
'Unleash - ordered environments successfully';
const SCHEDULED_CHANGE_CONFLICT_SUBJECT =
'Unleash - Scheduled changes can no longer be applied';
const SCHEDULED_EXECUTION_FAILED_SUBJECT =
@ -447,6 +450,67 @@ export class EmailService {
});
}
async sendOrderEnvironmentEmail(
userEmail: string,
customerId: string,
environmentNames: string[],
): Promise<IEmailEnvelope> {
if (this.configured()) {
const context = {
userEmail,
customerId,
environments: environmentNames.map((name) =>
this.stripSpecialCharacters(name),
),
};
const bodyHtml = await this.compileTemplate(
'order-environments',
TemplateFormat.HTML,
context,
);
const bodyText = await this.compileTemplate(
'order-environments',
TemplateFormat.PLAIN,
context,
);
const email = {
from: this.sender,
to: userEmail,
bcc: process.env.ORDER_ENVIRONMENTS_BCC || 'ivar@getunleash.io',
subject: ORDER_ENVIRONMENTS_SUBJECT,
html: bodyHtml,
text: bodyText,
};
process.nextTick(() => {
this.mailer!.sendMail(email).then(
() =>
this.logger.info(
'Successfully sent order environments email',
),
(e) =>
this.logger.warn(
'Failed to send order environments 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: ORDER_ENVIRONMENTS_SUBJECT,
html: '',
text: '',
});
});
}
isEnabled(): boolean {
return this.mailer !== undefined;
}

View File

@ -338,10 +338,10 @@
<td valign="top" class="bodyContent" mc:edit="body_content">
<h1>Order for additional environments submitted</h1>
<p>Hello,</p>
<p>An order for additional environments has been successfully submitted by <strong>{{{ userEmail }}}</strong> for customer ID <strong>{{{ instanceId }}}</strong>. Below are the details of the environments requested:</p>
<p>An order for additional environments has been successfully submitted by <strong>{{{ userEmail }}}</strong> for customer ID <strong>{{{ customerId }}}</strong>. Below are the details of the environments requested:</p>
<ul>
{{#environments}}
<li>{{{ name }}}</li>
<li>{{.}}</li>
{{/environments}}
</ul>
<p>Please note that it may take up to 24 hours for these changes to come into effect.</p>

View File

@ -2,10 +2,10 @@ Subject: Order for additional environments submitted
Hello,
An order for additional environments has been successfully submitted by {{ userEmail }} for customer ID {{ instanceId }}. Below are the details of the environments requested:
An order for additional environments has been successfully submitted by {{ userEmail }} for customer ID {{ customerId }}. Below are the details of the environments requested:
{{#environments}}
- {{ name }}
- {{.}}
{{/environments}}
Please note that it may take up to 24 hours for these changes to come into effect.