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(productivity-report): additional email headers #8540

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/lib/__snapshots__/create-config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ exports[`should create default config 1`] = `
"disableScheduler": undefined,
"email": {
"host": undefined,
"optionalEmailHeaders": {},
"port": 587,
"secure": false,
"sender": "Unleash <[email protected]>",
Expand Down
9 changes: 9 additions & 0 deletions src/lib/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,22 @@ const defaultImport: WithOptional<IImportOption, 'file'> = {
environment: process.env.IMPORT_ENVIRONMENT ?? 'development',
};

const emailHeaderName = process.env.OPTIONAL_EMAIL_HEADER_NAME;
const emailHeaderValue = process.env.OPTIONAL_EMAIL_HEADER_VALUE;

const defaultEmail: IEmailOption = {
host: process.env.EMAIL_HOST,
secure: parseEnvVarBoolean(process.env.EMAIL_SECURE, false),
port: parseEnvVarNumber(process.env.EMAIL_PORT, 587),
sender: process.env.EMAIL_SENDER || 'Unleash <[email protected]>',
smtpuser: process.env.EMAIL_USER,
smtppass: process.env.EMAIL_PASSWORD,
optionalEmailHeaders:
emailHeaderName && emailHeaderValue
? {
[emailHeaderName]: emailHeaderValue,
}
: {},
};

const dbPort = (dbConfig: Partial<IDBOption>): Partial<IDBOption> => {
Expand Down
44 changes: 43 additions & 1 deletion src/lib/services/email-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,50 @@ test('Can send productivity report email', async () => {
health: 99,
},
);
console.log(content);

expect(content.from).toBe('[email protected]');
expect(content.subject).toBe('Unleash - productivity report');
expect(content.html.includes(`Productivity Report`)).toBe(true);
});

test('Should add optional headers to productivity email', async () => {
const emailService = new EmailService({
server: {
unleashUrl: 'http://localhost',
},
email: {
host: 'test',
port: 587,
secure: false,
smtpuser: '',
smtppass: '',
sender: '[email protected]',
optionalEmailHeaders: {
'x-header-name': 'value',
},
},
getLogger: noLoggerProvider,
} as unknown as IUnleashConfig);

const passwordResetMail = await emailService.sendResetMail(
'name',
'[email protected]',
'http://exempla.com',
);

const productivityMail = await emailService.sendProductivityReportEmail(
'[email protected]',
'customerId',
{
flagsCreated: 1,
productionUpdates: 2,
health: 99,
},
);

expect(passwordResetMail.headers).toBeFalsy();

expect(productivityMail.headers).toStrictEqual({
'x-header-name': 'value',
});
});
11 changes: 9 additions & 2 deletions src/lib/services/email-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface IEmailEnvelope {
path: string;
cid: string;
}[];
headers?: Record<string, string>;
}

const RESET_MAIL_SUBJECT = 'Unleash - Reset your password';
Expand Down Expand Up @@ -555,7 +556,8 @@ export class EmailService {
TemplateFormat.PLAIN,
context,
);
const email: IEmailEnvelope = {

const email = {
from: this.sender,
to: userEmail,
bcc: '',
Expand All @@ -569,7 +571,11 @@ export class EmailService {
'unleashLogo',
),
],
};
headers: {
...this.config.email.optionalEmailHeaders,
},
} satisfies IEmailEnvelope;

process.nextTick(() => {
this.mailer!.sendMail(email).then(
() =>
Expand All @@ -585,6 +591,7 @@ export class EmailService {
});
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',
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export interface IEmailOption {
smtpuser?: string;
smtppass?: string;
transportOptions?: SMTPTransport.Options;
optionalEmailHeaders?: Record<string, string>;
}

export interface IListeningPipe {
Expand Down