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

Issue 2406/add email template s3 logic #2407

Merged
merged 17 commits into from
Dec 11, 2023
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
40 changes: 20 additions & 20 deletions backend/src/api/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as nodemailer from 'nodemailer';
import * as fs from 'fs';
import * as handlebars from 'handlebars';
import * as util from 'util';
import S3Client from '../tasks/s3-client';

export const validateBody = async <T>(
obj: ClassType<T>,
Expand Down Expand Up @@ -111,8 +112,8 @@ export const sendUserNotificationEmail = async (
SES: new SES({ region: 'us-east-1' })
});

const fs = require('fs').promises;
const html = await fs.readFile(template_file, 'utf8');
const client = new S3Client();
const html = await client.getEmailAsset(template_file);
const template = handlebars.compile(html);
const data = {
first_name: p_firstName,
Expand All @@ -129,37 +130,37 @@ export const sendUserNotificationEmail = async (
attachments: [
{
filename: 'banner.png',
path: '/app/src/email_templates/banner.png',
content: await client.getEmailAsset('banner.png'),
cid: 'CISA Banner'
},
{
filename: 'web.png',
path: '/app/src/email_templates/banner.png',
content: await client.getEmailAsset('banner.png'),
cid: 'CISA Web'
},
{
filename: 'email.png',
path: '/app/src/email_templates/email.png',
content: await client.getEmailAsset('email.png'),
cid: 'CISA Email'
},
{
filename: 'linkedin.png',
path: '/app/src/email_templates/linkedin.png',
content: await client.getEmailAsset('linkedin.png'),
cid: 'CISA LinkedIn'
},
{
filename: 'twitter.png',
path: '/app/src/email_templates/twitter.png',
content: await client.getEmailAsset('twitter.png'),
cid: 'CISA Twitter'
},
{
filename: 'facebook.png',
path: '/app/src/email_templates/facebooK.png',
content: await client.getEmailAsset('facebooK.png'),
cid: 'CISA Facebook'
},
{
filename: 'instagram.png',
path: '/app/src/email_templates/instagram.png',
content: await client.getEmailAsset('instagram.png'),
cid: 'CISA Instagram'
}
]
Expand All @@ -179,10 +180,9 @@ export const sendRegionalAdminNotificationEmail = async (
SES: new SES({ region: 'us-east-1' })
});

const fs = require('fs').promises;
const html = await fs.readFile(
'/app/src/email_templates/crossfeed_regional_admin_notification.html',
'utf8'
const client = new S3Client();
const html = await client.getEmailAsset(
'crossfeed_regional_admin_notification.html'
);
const template = handlebars.compile(html);
const data = {
Expand All @@ -200,37 +200,37 @@ export const sendRegionalAdminNotificationEmail = async (
attachments: [
{
filename: 'banner.png',
path: '/app/src/email_templates/banner.png',
content: await client.getEmailAsset('banner.png'),
cid: 'CISA Banner'
},
{
filename: 'web.png',
path: '/app/src/email_templates/banner.png',
content: await client.getEmailAsset('banner.png'),
cid: 'CISA Web'
},
{
filename: 'email.png',
path: '/app/src/email_templates/email.png',
content: await client.getEmailAsset('email.png'),
cid: 'CISA Email'
},
{
filename: 'linkedin.png',
path: '/app/src/email_templates/linkedin.png',
content: await client.getEmailAsset('linkedin.png'),
cid: 'CISA LinkedIn'
},
{
filename: 'twitter.png',
path: '/app/src/email_templates/twitter.png',
content: await client.getEmailAsset('twitter.png'),
cid: 'CISA Twitter'
},
{
filename: 'facebook.png',
path: '/app/src/email_templates/facebooK.png',
content: await client.getEmailAsset('facebooK.png'),
cid: 'CISA Facebook'
},
{
filename: 'instagram.png',
path: '/app/src/email_templates/instagram.png',
content: await client.getEmailAsset('instagram.png'),
cid: 'CISA Instagram'
}
]
Expand Down
22 changes: 22 additions & 0 deletions backend/src/tasks/s3-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class S3Client {
throw e;
}
}

async listReports(orgId: string) {
try {
const params = {
Expand All @@ -93,6 +94,27 @@ class S3Client {
throw e;
}
}

async getEmailAsset(fileName: string) {
try {
const params = {
Bucket: process.env.EMAIL_BUCKET_NAME!,
Key: fileName
};

const data = await this.s3
.getObject(params, function (err, data) {
if (err) throw err;
})
.promise();
if (data && data.Body) {
return data.Body.toString('utf-8');
}
} catch (e) {
console.error(e);
throw e;
}
}
}

export default S3Client;
2 changes: 2 additions & 0 deletions dev.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ EXPORT_BUCKET_NAME=crossfeed-local-exports

REPORTS_BUCKET_NAME=crossfeed-local-reports

EMAIL_BUCKET_NAME=cisa-crossfeed-staging-html-email

IS_LOCAL=1
Loading