Skip to content

Commit 9dc8684

Browse files
author
Fawad Ali
committed
Merge branch 'main' into feature/CCM-4893_email-automated-tested
2 parents e9f1202 + 6ec04b5 commit 9dc8684

File tree

13 files changed

+166
-138
lines changed

13 files changed

+166
-138
lines changed

amplify/__tests__/functions/send-email/handler.test.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ type HandlerCallbackType = Parameters<
1515
Schema['sendEmail']['functionHandler']
1616
>[2];
1717

18+
const notifyDomainName = process.env.NOTIFY_DOMAIN_NAME;
19+
1820
beforeAll(() => {
1921
jest.useFakeTimers();
2022
jest.setSystemTime(new Date('2022-01-01 10:00'));
2123
process.env.NOTIFY_DOMAIN_NAME = 'test.notify.nhs.uk';
2224
});
2325

26+
afterAll(() => {
27+
process.env.NOTIFY_DOMAIN_NAME = notifyDomainName;
28+
});
29+
2430
test('sends email', async () => {
2531
const mockSESClient = mockDeep<SESClient>({
2632
send: jest.fn().mockReturnValue({
@@ -52,29 +58,16 @@ test('sends email', async () => {
5258

5359
const messageId = rawMimeMessage?.match(/Message-ID: <([^@]+)@/)?.[1];
5460

55-
const messageBoundary = rawMimeMessage?.match(/boundary=([\dA-z]+)/)?.[1];
56-
5761
const expectedMessage = `Date: Sat, 01 Jan 2022 10:00:00 +0000
5862
From: =?utf-8?B?TkhTIE5vdGlmeQ==?= <[email protected]>
5963
To: <recipient-email>
6064
Message-ID: <${messageId}@test.notify.nhs.uk>
6165
Subject: =?utf-8?B?VGVtcGxhdGUgc3VibWl0dGVkIC0gdGVtcGxhdGUtbmFtZQ==?=
6266
MIME-Version: 1.0
63-
Content-Type: multipart/mixed; boundary=${messageBoundary}
64-
65-
--${messageBoundary}
6667
Content-Type: text/html; charset=UTF-8
6768
Content-Transfer-Encoding: 7bit
6869
69-
${emailTemplate('template-id', 'template-name', 'template-message')}
70-
71-
--${messageBoundary}
72-
Content-Type: text/markdown; name="template-content.md"
73-
Content-Transfer-Encoding: base64
74-
Content-Disposition: attachment; filename="template-content.md"
75-
76-
dGVtcGxhdGUtbWVzc2FnZQ==
77-
--${messageBoundary}--`;
70+
${emailTemplate('template-id', 'template-name', 'template-message')}`;
7871

7972
expect(rawMimeMessage?.toString()).toEqual(expectedMessage);
8073
});

amplify/data/resource.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const schema = a.schema({
4343
templateId: a.string().required(),
4444
templateName: a.string().required(),
4545
templateMessage: a.string().required(),
46+
templateSubjectLine: a.string(),
4647
})
4748
.returns(a.string())
4849
.handler(a.handler.function(sendEmail))

amplify/functions/send-email/email-template.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@
209209
<p class="dataValueShort">{{templateId}}</p>
210210
</td>
211211
</tr>
212+
{{#if templateSubjectLine}}
213+
<tr>
214+
<td colspan="2">
215+
<p class="dataLabel">Subject line</p>
216+
<p class="dataValueShort">{{templateSubjectLine}}</p>
217+
</td>
218+
</tr>
219+
{{/if}}
212220
<tr>
213221
<td colspan="2">
214222
<p class="dataLabel">Template content</p>

amplify/functions/send-email/email-template.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ const encoder = (rawText: string) => encode(rawText).replaceAll('\n', '<br />');
88
export const emailTemplate = (
99
templateId: string,
1010
templateName: string,
11-
templateMessage: string
11+
templateMessage: string,
12+
templateSubjectLine?: string | null
1213
): string => {
1314
const parameters = {
1415
templateId,
1516
templateName,
1617
templateMessage: encoder(templateMessage),
18+
templateSubjectLine,
1719
};
1820
return htmlTemplate(parameters);
1921
};

amplify/functions/send-email/handler.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ export const handler: Schema['sendEmail']['functionHandler'] = async (
1414
const client = new SESClient({ region: 'eu-west-2' });
1515

1616
const { senderEmail } = config();
17-
const { recipientEmail, templateId, templateName, templateMessage } =
18-
event.arguments;
17+
const {
18+
recipientEmail,
19+
templateId,
20+
templateName,
21+
templateMessage,
22+
templateSubjectLine,
23+
} = event.arguments;
1924

2025
logger.info(
2126
`Sending email for template ${templateName} with ID ${templateId}`
@@ -27,12 +32,12 @@ export const handler: Schema['sendEmail']['functionHandler'] = async (
2732
msg.setSubject(`Template submitted - ${templateName}`);
2833
msg.addMessage({
2934
contentType: 'text/html',
30-
data: emailTemplate(templateId, templateName, templateMessage),
31-
});
32-
msg.addAttachment({
33-
filename: 'template-content.md',
34-
contentType: 'text/markdown',
35-
data: Buffer.from(templateMessage).toString('base64'),
35+
data: emailTemplate(
36+
templateId,
37+
templateName,
38+
templateMessage,
39+
templateSubjectLine
40+
),
3641
});
3742

3843
const command = new SendRawEmailCommand({

0 commit comments

Comments
 (0)