Skip to content

Commit

Permalink
fixes for sendgrid registeration methods (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmthecoder authored Oct 8, 2024
1 parent 99837bd commit c50db5c
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 31 deletions.
50 changes: 49 additions & 1 deletion packages/api-gateway/src/models/email.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,54 @@ export class RegisterEmailModel {
@IsOptional()
@IsEmail()
replyTo?: string;

@ApiProperty({
type: 'string',
description:
'The nickname to assosicate with the sender (only visible in sendgrid)',
})
@IsOptional()
nickname: string;

@ApiProperty({
type: 'string',
example: '123 Main St.',
description: 'The address to assosicate with the sender',
})
@IsNotEmpty()
address: string;

@ApiProperty({
type: 'string',
example: 'Atlanta',
description: 'The city to assosicate with the sender',
})
@IsNotEmpty()
city: string;

@ApiProperty({
type: 'string',
example: 'GA',
description: 'The state to assosicate with the sender',
})
@IsNotEmpty()
state: string;

@ApiProperty({
type: 'string',
example: '30332',
description: 'The zip code to assosicate with the sender',
})
@IsNotEmpty()
zip: string;

@ApiProperty({
type: 'string',
example: 'USA',
description: 'The country to assosicate with the sender',
})
@IsNotEmpty()
country: string;
}

export class RegisterEmailResponse {
Expand Down Expand Up @@ -249,7 +297,7 @@ export class RegisterDomainResponse {
| EmailProto.AuthenticateDomainResponse
| EmailProto.VerifyDomainResponse,
) {
this.id = res.id;
this.id = Number(res.id);
this.valid = `${res.valid}`;
this.records = new SendGridDNSResponse(res.records);
this.statusCode = res.statusCode;
Expand Down
22 changes: 15 additions & 7 deletions packages/api-gateway/src/modules/email/email.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export class EmailController implements OnModuleInit {
replyTo: params.replyTo ?? params.email,
configId: apiKey.project.id,
configEnvironment: apiKey.environment,
nickname: params.nickname ?? params.name,
address: params.address,
city: params.city,
state: params.state,
zip: params.zip,
country: params.country,
}),
);

Expand Down Expand Up @@ -121,14 +127,16 @@ export class EmailController implements OnModuleInit {
);
}

const res = this.emailService.authenticateDomain({
domain: req.domain,
subdomain: req.subdomain,
configId: apiKey.project.id,
configEnvironment: apiKey.environment,
});
const res = await lastValueFrom(
this.emailService.authenticateDomain({
domain: req.domain,
subdomain: req.subdomain,
configId: apiKey.project.id,
configEnvironment: apiKey.environment,
}),
);

return new RegisterDomainResponse(await lastValueFrom(res));
return new RegisterDomainResponse(res);
}

@ApiOperation({
Expand Down
20 changes: 20 additions & 0 deletions packages/api-gateway/test/email.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ describe('Email Registration Routes', () => {
.send({
email: 'invalidemail', // Malformed email
name: 'name',
address: 'address',
city: 'city',
state: 'state',
country: 'country',
zip: 'zip',
})
.expect(400);
});
Expand All @@ -148,6 +153,11 @@ describe('Email Registration Routes', () => {
.send({
email: 'validemail@example.com',
name: 'name',
address: 'address',
city: 'city',
state: 'state',
country: 'country',
zip: 'zip',
})
.expect(401);
});
Expand All @@ -158,6 +168,11 @@ describe('Email Registration Routes', () => {
.send({
email: 'validemail@example.com',
name: 'name',
address: 'address',
city: 'city',
state: 'state',
country: 'country',
zip: 'zip',
})
.expect(401);
});
Expand All @@ -169,6 +184,11 @@ describe('Email Registration Routes', () => {
.send({
email: 'validemail@example.com',
name: 'name',
address: 'address',
city: 'city',
state: 'state',
country: 'country',
zip: 'zip',
})
.expect(201); // Assuming the server responds with 201 Created on successful registration
});
Expand Down
1 change: 0 additions & 1 deletion packages/auth-service/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ describe('User authentication tests', () => {

const res = await promise;
res['id'] = Number(res['id']);
console.log(`res: ${JSON.stringify(res)}`);
expect(res).toStrictEqual(correctUserResponse);
});
});
1 change: 1 addition & 0 deletions packages/db-service/src/modules/email/email.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export class EmailController implements EmailDbServiceController {
},
},
});

return {
domain: emailDomain.domain,
subdomain: emailDomain.subdomain,
Expand Down
67 changes: 45 additions & 22 deletions packages/email-service/src/modules/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import axios from 'axios';
import { ClientGrpc, RpcException } from '@nestjs/microservices';
import { lastValueFrom } from 'rxjs';
import { MailDataRequired } from '@sendgrid/mail';
import { status } from '@grpc/grpc-js';

const { EMAIL_DB_SERVICE_NAME } = EmailProto;

@Injectable()
export class EmailService implements OnModuleInit {
private emailService: EmailProto.EmailDbServiceClient;
constructor(
private sendgrid: SendGridService,
@Inject(EMAIL_DB_SERVICE_NAME) private emailClient: ClientGrpc,
) {}
constructor(@Inject(EMAIL_DB_SERVICE_NAME) private emailClient: ClientGrpc) {}

onModuleInit() {
this.emailService =
Expand All @@ -30,7 +28,10 @@ export class EmailService implements OnModuleInit {
this.emailService.createEmailServiceConfig(request),
);
if (!config) {
throw new RpcException('Failed to create email service config');
throw new RpcException({
code: status.INVALID_ARGUMENT,
message: 'Failed to create email service config',
});
}
return {
success: true,
Expand Down Expand Up @@ -93,15 +94,21 @@ export class EmailService implements OnModuleInit {
},
);

const records: EmailProto.SendGridDnsRecords = response.data.dns;
const records: EmailProto.SendGridDnsRecords = {
dkim1: response.data.dns.dkim1,
dkim2: response.data.dns.dkim2,
mailCname: response.data.dns.mail_cname,
};

this.emailService.createEmailDomain({
domain: req.domain,
subdomain: req.subdomain,
sendgridId: response.data.id,
configId: req.configId,
configEnvironment: req.configEnvironment,
});
// await lastValueFrom(
// this.emailService.createEmailDomain({
// domain: req.domain,
// subdomain: req.subdomain,
// sendgridId: response.data.id,
// configId: req.configId,
// configEnvironment: req.configEnvironment,
// }),
// );

return {
statusCode: response.status,
Expand All @@ -110,8 +117,13 @@ export class EmailService implements OnModuleInit {
records,
};
} catch (error) {
console.error('Error registering domain:', error);
throw new RpcException('Failed to register domain');
if (error instanceof RpcException) {
throw error;
}
throw new RpcException({
code: status.INVALID_ARGUMENT,
message: JSON.stringify(error),
});
}
}

Expand Down Expand Up @@ -143,7 +155,10 @@ export class EmailService implements OnModuleInit {
};
await sendgrid.send(data);
} catch (err) {
throw new RpcException(JSON.stringify(err));
throw new RpcException({
code: status.INVALID_ARGUMENT,
message: JSON.stringify(err),
});
}
}
}
Expand Down Expand Up @@ -183,13 +198,20 @@ export class EmailService implements OnModuleInit {
message: 'test register success',
};
}

try {
const res = await axios.post(
sendgridUrl,
{
fromEmail: req.fromEmail,
fromName: req.fromName,
replyTo: req.replyTo,
nickname: req.nickname ?? req.fromName,
from_email: req.fromEmail,
from_name: req.fromName,
reply_to: req.replyTo,
address: req.address,
city: req.city,
state: req.state,
zip: req.zip,
country: req.country,
},
{
headers: {
Expand All @@ -204,8 +226,10 @@ export class EmailService implements OnModuleInit {
message: 'Sender registered successfully',
};
} catch (err) {
console.error('error registering sender:', err);
throw new RpcException('Unable to register sender');
throw new RpcException({
code: status.INVALID_ARGUMENT,
message: JSON.stringify(err),
});
}
}

Expand Down Expand Up @@ -263,7 +287,6 @@ export class EmailService implements OnModuleInit {
records,
};
} catch (error) {
console.error('Error registering domain:', error);
throw new RpcException('Failed to register domain');
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/email-service/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ describe('Email Service Sender Registration Tests', () => {
replyTo: 'example@example.com',
configId: 0,
configEnvironment: 'prod',
address: 'address',
city: 'city',
state: 'state',
country: 'country',
zip: 'zip',
},
(err: any, response: EmailProto.RegisterSenderResponse) => {
if (err) {
Expand All @@ -102,6 +107,11 @@ describe('Email Service Sender Registration Tests', () => {
replyTo: '',
configId: 0,
configEnvironment: 'prod',
address: 'address',
city: 'city',
state: 'state',
country: 'country',
zip: 'zip',
},
(err: any, response: EmailProto.RegisterSenderResponse) => {
if (err) {
Expand Down
6 changes: 6 additions & 0 deletions packages/proto/definitions/email.proto
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ message RegisterSenderRequest {
string reply_to = 3;
int64 configId = 4;
string configEnvironment = 5;
string nickname = 6;
string address = 7;
string city = 8;
string state = 9;
string country = 10;
string zip = 11;
}

message RegisterSenderResponse {
Expand Down
6 changes: 6 additions & 0 deletions packages/proto/src/gen/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export interface RegisterSenderRequest {
replyTo: string;
configId: number;
configEnvironment: string;
nickname: string;
address: string;
city: string;
state: string;
country: string;
zip: string;
}

export interface RegisterSenderResponse {
Expand Down

0 comments on commit c50db5c

Please sign in to comment.