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

Implementation of sendgrid sender registration #59

Merged
merged 9 commits into from
Mar 30, 2024
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
2 changes: 2 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ services:
DB_SERVICE_ADDR: db-service:5000
EMAIL_SERVICE_ADDR: email-service:5000
LOGGING_SERVICE_ADDR: logging-service:5000
NODE_ENV: test
SENDGRID_API_KEY: test-key
networks:
- bog-api-net
depends_on:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
},
"dependencies": {
"@sendgrid/mail": "^8.1.1",
"axios": "^1.6.7",
"bcrypt": "^5.1.1",
"fs-extra": "^11.2.0",
"jsonwebtoken": "^9.0.2"
Expand Down
5 changes: 5 additions & 0 deletions packages/email-service/src/modules/email/email.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ export class EmailController implements EmailProto.EmailServiceController {
throw new RpcException(error.message);
}
}
async registerSender(
req: EmailProto.RegisterSenderRequest,
): Promise<EmailProto.RegisterSenderResponse> {
return await this.emailService.registerSender(req);
}
}
58 changes: 57 additions & 1 deletion packages/email-service/src/modules/email/email.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Injectable } from '@nestjs/common';
import { EmailProto } from 'juno-proto';
import { SendGridService } from 'src/sendgrid.service';
import axios from 'axios';
import { RpcException } from '@nestjs/microservices';

@Injectable()
export class EmailService {
Expand All @@ -9,7 +11,7 @@ export class EmailService {
async sendEmail(request: EmailProto.SendEmailRequest): Promise<void> {
// SendGrid Client for future integration with API
// Conditional statement used for testing without actually calling Sendgrid. Remove when perform actual integration
if (process.env.SENDGRID_API_KEY) {
if (process.env.SENDGRID_API_KEY && process.env.NODE_ENV != 'test') {
await this.sendgrid.send({
personalizations: [
{
Expand All @@ -24,4 +26,58 @@ export class EmailService {
});
}
}

async registerSender(
req: EmailProto.RegisterSenderRequest,
): Promise<EmailProto.RegisterSenderResponse> {
if (!req.fromEmail) {
throw new RpcException('Cannot register sender (no email supplied)');
}
if (!req.fromName) {
throw new RpcException('Cannot register sender (no name supplied)');
}
if (!req.replyTo) {
throw new RpcException('Cannot register sender (no reply to specified)');
}

const sendgridApiKey = process.env.SENDGRID_API_KEY;
const sendgridUrl = 'https://api.sendgrid.com/v3/verified_senders';

if (!sendgridApiKey) {
throw new RpcException(
'Cannot register sender (sendgrid API key is missing)',
);
}

if (process.env['NODE_ENV'] == 'test') {
return {
statusCode: 201,
message: 'test register success',
};
}
try {
const res = await axios.post(
sendgridUrl,
{
fromEmail: req.fromEmail,
fromName: req.fromName,
replyTo: req.replyTo,
},
{
headers: {
Authorization: `Bearer ${sendgridApiKey}`,
'Content-Type': 'application/json',
},
},
);

return {
statusCode: res.status,
message: 'Sender registered successfully',
};
} catch (err) {
console.error('error registering sender:', err);
throw new RpcException('Unable to register sender');
}
}
}
2 changes: 1 addition & 1 deletion packages/email-service/src/sendgrid.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class SendGridService
implements OnModuleInit
{
async onModuleInit() {
if (process.env.SENDGRID_API_KEY) {
if (process.env.SENDGRID_API_KEY && process.env.NODE_ENV != 'test') {
this.setApiKey(process.env.SENDGRID_API_KEY);
}
}
Expand Down
68 changes: 62 additions & 6 deletions packages/email-service/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { AppModule } from './../src/app.module';
import * as ProtoLoader from '@grpc/proto-loader';
import * as GRPC from '@grpc/grpc-js';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { ResetProtoFile } from 'juno-proto';
import { EmailProtoFile, EmailProto, ResetProtoFile } from 'juno-proto';
import { JUNO_EMAIL_PACKAGE_NAME } from 'juno-proto/dist/gen/email';

let app: INestMicroservice;

Expand All @@ -18,8 +19,8 @@ async function initApp() {
const app = moduleFixture.createNestMicroservice<MicroserviceOptions>({
transport: Transport.GRPC,
options: {
package: [],
protoPath: [],
package: [JUNO_EMAIL_PACKAGE_NAME],
protoPath: [EmailProtoFile],
url: process.env.EMAIL_SERVICE_ADDR,
},
});
Expand All @@ -33,7 +34,7 @@ async function initApp() {
beforeAll(async () => {
app = await initApp();

const proto = ProtoLoader.loadSync([ResetProtoFile]) as any;
const proto = ProtoLoader.loadSync([EmailProtoFile, ResetProtoFile]) as any;

const protoGRPC = GRPC.loadPackageDefinition(proto) as any;

Expand All @@ -53,6 +54,61 @@ afterAll(() => {
app.close();
});

it('Dummy test', () => {
expect('1').toEqual('1');
describe('Email Service Sender Registration Tests', () => {
let emailClient: any;
beforeEach(async () => {
const proto = ProtoLoader.loadSync([EmailProtoFile]) as any;

const protoGRPC = GRPC.loadPackageDefinition(proto) as any;

emailClient = new protoGRPC.juno.email.EmailService(
process.env.EMAIL_SERVICE_ADDR,
GRPC.credentials.createInsecure(),
);
});

it('should successfully register a sender', async () => {
const response: EmailProto.RegisterSenderResponse = await new Promise(
(resolve, reject) => {
emailClient.registerSender(
{
fromEmail: '[email protected]',
fromName: 'example',
replyTo: '[email protected]',
},
(err: any, response: EmailProto.RegisterSenderResponse) => {
if (err) {
reject(err);
} else {
resolve(response);
}
},
);
},
);

expect(response).toBeDefined();
expect(response.statusCode).toEqual(201);
});

it('should fail to register a sender', async () => {
const response: any = await new Promise((resolve, reject) => {
emailClient.registerSender(
{
fromEmail: '',
fromName: '',
replyTo: '',
},
(err: any, response: EmailProto.RegisterSenderResponse) => {
if (err) {
resolve(err);
} else {
reject(response);
}
},
);
});

expect(response).toBeDefined();
});
});
14 changes: 14 additions & 0 deletions packages/proto/definitions/email.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "identifiers.proto";

service EmailService {
rpc sendEmail(SendEmailRequest) returns (SendEmailResponse);
rpc registerSender(RegisterSenderRequest) returns (RegisterSenderResponse);
}

service EmailDbService {
Expand Down Expand Up @@ -56,3 +57,16 @@ message UpdateEmailRequest {
identifiers.EmailIdentifier emailIdentifier = 1;
EmailUpdateParams updateParams = 2;
}

message SendEmailRequestResponse { bool success = 1; }

message RegisterSenderRequest {
string from_email = 1;
string from_name = 2;
string reply_to = 3;
}

message RegisterSenderResponse {
int32 status_code = 1;
string message = 2;
}
28 changes: 27 additions & 1 deletion packages/proto/src/gen/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,29 @@ export interface UpdateEmailRequest {
updateParams: EmailUpdateParams | undefined;
}

export interface SendEmailRequestResponse {
success: boolean;
}

export interface RegisterSenderRequest {
fromEmail: string;
fromName: string;
replyTo: string;
}

export interface RegisterSenderResponse {
statusCode: number;
message: string;
}

export const JUNO_EMAIL_PACKAGE_NAME = 'juno.email';

export interface EmailServiceClient {
sendEmail(request: SendEmailRequest): Observable<SendEmailResponse>;

registerSender(
request: RegisterSenderRequest,
): Observable<RegisterSenderResponse>;
}

export interface EmailServiceController {
Expand All @@ -64,11 +83,18 @@ export interface EmailServiceController {
| Promise<SendEmailResponse>
| Observable<SendEmailResponse>
| SendEmailResponse;

registerSender(
request: RegisterSenderRequest,
):
| Promise<RegisterSenderResponse>
| Observable<RegisterSenderResponse>
| RegisterSenderResponse;
}

export function EmailServiceControllerMethods() {
return function (constructor: Function) {
const grpcMethods: string[] = ['sendEmail'];
const grpcMethods: string[] = ['sendEmail', 'registerSender'];
for (const method of grpcMethods) {
const descriptor: any = Reflect.getOwnPropertyDescriptor(
constructor.prototype,
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,7 @@ [email protected]:
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axios@^1.6.0, axios@^1.6.4:
axios@^1.6.0, axios@^1.6.4, axios@^1.6.7:
version "1.6.8"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.8.tgz#66d294951f5d988a00e87a0ffb955316a619ea66"
integrity sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==
Expand Down
Loading