Skip to content

Commit

Permalink
refactor: service function, fix attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitij-k-osmosys committed Sep 9, 2024
1 parent cb1f3c4 commit b7ea5d3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion apps/api/docs/channels/aws-ses.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Here's a sample request body:
"attachments": [ // Attachments (optional)
{
"filename": "names.txt",
"content": "John Doe\nJane Doe",
"content": "John Doe\nJane Doe"
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import {

export class AwsSesDataDto {
@IsNotEmpty()
@IsString()
from: string | string[];

@IsNotEmpty()
@IsString()
to: string | string[];

@IsOptional()
@IsString()
cc?: string | string[];

@IsOptional()
@IsString()
bcc?: string | string[];

@IsNotEmpty()
Expand All @@ -31,6 +35,7 @@ export class AwsSesDataDto {
html: string;

@IsOptional()
@IsString()
replyTo?: string | string[];

@IsOptional()
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/modules/notifications/queues/queue.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ export class QueueService {
case `${QueueAction.WEBHOOK}-${ChannelType.SMS_KAPSYSTEM}`:
case `${QueueAction.WEBHOOK}-${ChannelType.PUSH_SNS}`:
case `${QueueAction.WEBHOOK}-${ChannelType.VC_TWILIO}`:
case `${QueueAction.WEBHOOK}-${ChannelType.AWS_SES}`:
case `${QueueAction.WEBHOOK}-${ChannelType.SMS_SNS}`:
await this.webhookService.triggerWebhook(job.data.id);
break;
default:
Expand Down
50 changes: 28 additions & 22 deletions apps/api/src/modules/providers/aws-ses/aws-ses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,26 @@ export class AwsSesService {
private logger: Logger,
) {}

private async getSesClient(providerId: number): Promise<aws.SES> {
const config = await this.providersService.getConfigById(providerId);
return new aws.SES({
apiVersion: '2010-12-01',
region: config.AWS_REGION as string,
credentials: {
accessKeyId: config.AWS_ACCESS_KEY_ID as string,
secretAccessKey: config.AWS_SECRET_ACCESS_KEY as string,
},
});
}

async sendAwsSes(
formattedData: AwsSesData,
providerId: number,
): Promise<SESTransport.SentMessageInfo> {
try {
this.logger.debug('Started assigning AWS SES email client');
const awsSesConfig = await this.providersService.getConfigById(providerId);

const ses = new aws.SES({
apiVersion: '2010-12-01',
region: awsSesConfig.AWS_REGION as string,
credentials: {
accessKeyId: awsSesConfig.AWS_ACCESS_KEY_ID as string,
secretAccessKey: awsSesConfig.AWS_SECRET_ACCESS_KEY as string,
},
});

const ses = await this.getSesClient(providerId);

// create Nodemailer SES transporter
const transporter = nodemailer.createTransport({
Expand Down Expand Up @@ -93,37 +97,39 @@ export class AwsSesService {
formattedNotificationData.attachment = await this.formatAttachments(
notificationData.attachments as CreateNotificationAttachmentDto[],
);
delete formattedNotificationData.attachments;
formattedNotificationData.attachments = undefined;

return formattedNotificationData;
}

return notificationData;
}

private async readFileContent(filepath: string): Promise<Buffer> {
try {
return await fs.readFile(filepath);
} catch (error) {
throw new BadRequestException(`Failed to read file at path: ${filepath}: ${error.message}`);
}
}

private async formatAttachments(
attachments: CreateNotificationAttachmentDto[],
): Promise<{ filename: string; data: Buffer; contentType: string }[]> {
): Promise<{ filename: string; content: Buffer | string; contentType: string }[]> {
this.logger.debug('Formatting attachments for AWS SES');
return Promise.all(
attachments.map(async (attachment) => {
let data: Buffer | string | Stream = attachment.content;
let content: Buffer | string | Stream = attachment.content;

if (attachment.path) {
try {
const filepath = path.resolve(attachment.path);
data = await fs.readFile(filepath);
} catch (error) {
throw new BadRequestException(
`Failed to read file at path: ${attachment.path}: ${error.message}`,
);
}
const filepath = path.resolve(attachment.path);
content = await this.readFileContent(filepath);
}

const contentType = mime.lookup(attachment.filename) || 'application/octet-stream';
return {
filename: attachment.filename,
data: Buffer.isBuffer(data) ? data : Buffer.from(data as string, 'base64'),
content: Buffer.isBuffer(content) ? content : Buffer.from(content as string, 'utf-8'),
contentType,
};
}),
Expand Down

0 comments on commit b7ea5d3

Please sign in to comment.