Skip to content

Commit

Permalink
update email service send to include subject (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmthecoder authored Oct 7, 2024
1 parent 8def261 commit 99837bd
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 23 deletions.
8 changes: 8 additions & 0 deletions packages/api-gateway/src/models/email.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ export class SendEmailModel {
@Type(() => EmailSender)
sender: EmailSender | undefined;

@ApiProperty({
type: 'string',
example: 'Email subject',
description: 'The subject of the email',
})
@IsNotEmpty()
subject: string;

@ApiProperty({
type: [EmailContent],
description: 'List of MIME content to send in the email',
Expand Down
1 change: 1 addition & 0 deletions packages/api-gateway/src/modules/email/email.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export class EmailController implements OnModuleInit {
email: req.sender.email,
name: req.sender.name,
},
subject: req.subject,
content: req.content,
configId: apiKey.project.id,
configEnvironment: apiKey.environment,
Expand Down
18 changes: 18 additions & 0 deletions packages/api-gateway/test/email.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ describe('Email Sending Route', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
})
.expect(401);
});
Expand All @@ -194,6 +195,7 @@ describe('Email Sending Route', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
})
.expect(401);
});
Expand All @@ -205,6 +207,7 @@ describe('Email Sending Route', () => {
.send({
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
subject: 'Test email',
content: [{ type: 'text/plain', value: 'Test email' }],
})
.expect(201);
Expand All @@ -219,6 +222,7 @@ describe('Email Sending Route', () => {
recipients: [
{ email: '[email protected]', name: 'RecipientName' },
],
subject: 'Test email',
content: [{ type: 'text/plain', value: 'Test email' }],
})
.expect(201);
Expand All @@ -234,6 +238,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [{ type: 'text/plain', value: 'Test email' }],
})
.expect(201);
Expand All @@ -249,6 +254,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: 'text/plain', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -268,6 +274,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName2' },
],
cc: [{ email: '[email protected]', name: 'RecipientName1' }],
subject: 'Test email',
content: [
{ type: 'text/plain', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -287,6 +294,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName2' },
],
bcc: [{ email: '[email protected]', name: 'RecipientName1' }],
subject: 'Test email',
content: [
{ type: 'text/plain', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -313,6 +321,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: 'text/plain', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -331,6 +340,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: 'text/plain', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -349,6 +359,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: 'text/plain', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -367,6 +378,7 @@ describe('Email Sending Route', () => {
{ email: '', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: 'text/plain', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -385,6 +397,7 @@ describe('Email Sending Route', () => {
{ email: 'invalid-email', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: 'text/plain', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -403,6 +416,7 @@ describe('Email Sending Route', () => {
{ email: null, name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: 'text/plain', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -421,6 +435,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: '', value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -439,6 +454,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: null, value: 'Test email' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -457,6 +473,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: 'text/plain', value: '' },
{ type: 'text/plain', value: 'Test email' },
Expand All @@ -475,6 +492,7 @@ describe('Email Sending Route', () => {
{ email: '[email protected]', name: 'RecipientName1' },
{ email: '[email protected]', name: 'RecipientName2' },
],
subject: 'Test email',
content: [
{ type: 'text/plain', value: null },
{ type: 'text/plain', value: 'Test email' },
Expand Down
35 changes: 18 additions & 17 deletions packages/email-service/src/modules/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SendGridService } from 'src/sendgrid.service';
import axios from 'axios';
import { ClientGrpc, RpcException } from '@nestjs/microservices';
import { lastValueFrom } from 'rxjs';
import { MailDataRequired } from '@sendgrid/mail';

const { EMAIL_DB_SERVICE_NAME } = EmailProto;

Expand Down Expand Up @@ -126,24 +127,24 @@ export class EmailService implements OnModuleInit {
// SendGrid Client for future integration with API
// Conditional statement used for testing without actually calling Sendgrid. Remove when perform actual integration
if (process.env.NODE_ENV != 'test') {
this.sendgrid.setApiKey(sendGridApiKey);
await this.sendgrid.send({
personalizations: [
{
to: request.recipients,
cc: request.cc,
bcc: request.bcc,
const sendgrid = new SendGridService();
sendgrid.setApiKey(sendGridApiKey);
try {
const data: MailDataRequired = {
to: request.recipients,
cc: request.cc,
bcc: request.bcc,
from: {
email: request.sender.email,
name: request.sender.name,
},
],
from: {
email: request.sender.email,
name: request.sender.name,
},
content: [
request.content[0],
...request.content.slice(1, request.content.length),
],
});
subject: request.subject,
content: [request.content[0], ...request.content.splice(1)],
};
await sendgrid.send(data);
} catch (err) {
throw new RpcException(JSON.stringify(err));
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions packages/email-service/test/email.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -163,6 +164,7 @@ describe('Email Service Send Email Tests', () => {
sender: null,
recipients: [{ email: '[email protected]' }],
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -182,6 +184,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: null },
recipients: [{ email: '[email protected]' }],
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -201,6 +204,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '' },
recipients: [{ email: '[email protected]' }],
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -220,6 +224,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: null,
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -239,6 +244,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [],
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -258,6 +264,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [{ email: null }],
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -277,6 +284,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '' }],
content: [{ type: 'text/plain', value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -296,6 +304,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
content: null,
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -315,6 +324,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
content: [],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -334,6 +344,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
content: [{ type: null, value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -353,6 +364,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
content: [{ type: 'text/plain', value: null }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -372,6 +384,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
content: [{ type: '', value: 'Test email' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand All @@ -391,6 +404,7 @@ describe('Email Service Send Email Tests', () => {
sender: { email: '[email protected]' },
recipients: [{ email: '[email protected]' }],
content: [{ type: 'text/plain', value: '' }],
subject: 'Test email',
configId: 0,
configEnvironment: 'prod',
},
Expand Down
14 changes: 8 additions & 6 deletions packages/proto/definitions/email.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ service EmailDbService {
rpc createEmailDomain(CreateEmailDomainRequest) returns (EmailDomain);
rpc createEmailServiceConfig(CreateEmailServiceConfigRequest)
returns (EmailServiceConfig);
rpc getEmailServiceConfig(GetEmailServiceConfigRequest) returns (EmailServiceConfig);
rpc getEmailServiceConfig(GetEmailServiceConfigRequest)
returns (EmailServiceConfig);
}

message EmailSender {
Expand All @@ -37,9 +38,10 @@ message SendEmailRequest {
repeated EmailRecipient cc = 2;
repeated EmailRecipient bcc = 3;
SenderInfo sender = 4;
repeated EmailContent content = 5;
int64 configId = 6;
string configEnvironment = 7;
string subject = 5;
repeated EmailContent content = 6;
int64 configId = 7;
string configEnvironment = 8;
}

message SenderInfo {
Expand Down Expand Up @@ -139,8 +141,8 @@ message CreateEmailDomainRequest {
string configEnvironment = 5;
}

message VerifyDomainRequest {
string domain = 1;
message VerifyDomainRequest {
string domain = 1;
int64 configId = 2;
string configEnvironment = 3;
}
Expand Down
1 change: 1 addition & 0 deletions packages/proto/src/gen/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface SendEmailRequest {
cc: EmailRecipient[];
bcc: EmailRecipient[];
sender: SenderInfo | undefined;
subject: string;
content: EmailContent[];
configId: number;
configEnvironment: string;
Expand Down

0 comments on commit 99837bd

Please sign in to comment.