Skip to content

Commit

Permalink
fix(email): sync templates/SendGrid provider eu domain/Email provider…
Browse files Browse the repository at this point in the history
…s missing config fields error handling/GetExternalTemplates error handling (#1146)

* fix(email): soft syncing external templates

* fix(email): external templates pagination fallback

* chore(email): remove redundant config field

* feat(email): sendgrid provider set region host

* fix(email): provider connection error handling before data handling

* fix(email): provider config missing fields checks

* fix(email): soft syncing external templates

* fix(email): external templates pagination fallback

* chore(email): remove redundant config field

* feat(email): sendgrid provider set region host

* fix(email): provider connection error handling before data handling

* fix(email): provider config missing fields checks

* fix(email): delete deprecated apiUser field on preConfig
  • Loading branch information
Renc17 authored Sep 10, 2024
1 parent 5a37d02 commit 4e17224
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
5 changes: 5 additions & 0 deletions modules/email/src/Email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export default class Email extends ManagedModule<Config> {
}

async preConfig(config: Config) {
if (config.transportSettings?.sendgrid.hasOwnProperty('apiUser')) {
delete (
config as Config & { transportSettings: { sendgrid: { apiUser?: string } } }
).transportSettings.sendgrid.apiUser;
}
if (
isNil(config.active) ||
isNil(config.transport) ||
Expand Down
23 changes: 13 additions & 10 deletions modules/email/src/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,23 @@ export class AdminHandlers {
}

async getExternalTemplates(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
const { skip } = call.request.params ?? 0;
const { limit } = call.request.params ?? 25;
const { sortByName } = call.request.params;
const {
skip = 0,
limit = 25,
sortByName = false,
} = call.request.params as {
skip?: number;
limit?: number;
sortByName?: boolean;
};
const [err, externalTemplates] = await to(this.emailService.getExternalTemplates()!);
if (!isNil(err)) {
throw new GrpcError(status.INTERNAL, err.message);
}
if (!isNil(sortByName)) {
if (sortByName) externalTemplates!.sort((a, b) => a.name.localeCompare(b.name));
else externalTemplates!.sort((a, b) => b.name.localeCompare(a.name));
}

if (!isNil(err)) {
throw new GrpcError(status.INTERNAL, err.message);
}
if (isNil(externalTemplates)) {
throw new GrpcError(status.NOT_FOUND, 'No external templates could be retrieved');
}
Expand Down Expand Up @@ -313,11 +318,9 @@ export class AdminHandlers {
const templateDocument = await EmailTemplate.getInstance().findOne({
externalId: element.id,
});

if (isNil(templateDocument)) {
throw new GrpcError(status.NOT_FOUND, 'Template does not exist');
continue;
}

const synchronized = {
name: element.name,
subject: element.versions[0].subject,
Expand Down
8 changes: 4 additions & 4 deletions modules/email/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ export default {
},
},
sendgrid: {
apiKey: {
doc: 'The SendGrid API key',
residency: {
doc: 'Sets the host for sendgrid provider. SendGrid Default: global',
format: 'String',
default: '',
},
apiUser: {
doc: 'The SendGrid API username',
apiKey: {
doc: 'The SendGrid API key',
format: 'String',
default: '',
},
Expand Down
24 changes: 18 additions & 6 deletions modules/email/src/email-provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Mail from 'nodemailer/lib/mailer';
import { SentMessageInfo } from 'nodemailer';
import { MailgunConfig } from './transports/mailgun/mailgun.config.js';
import { isEmpty, isNil } from 'lodash-es';
import { isEmpty } from 'lodash-es';
import { MandrillConfig } from './transports/mandrill/mandrill.config.js';
import { EmailBuilderClass } from './models/EmailBuilderClass.js';
import { SendGridConfig } from './transports/sendgrid/sendgrid.config.js';
Expand All @@ -19,12 +19,13 @@ export class EmailProvider {

constructor(transport: string, transportSettings: any) {
if (transport === 'mailgun') {
this._transportName = 'mailgun';
const { apiKey, proxy, host } = transportSettings.mailgun;
let domain = ConfigController.getInstance().config.sendingDomain;
if (!isEmpty(transportSettings.mailgun.domain)) {
domain = transportSettings.mailgun.domain;
}
if (isNil(apiKey) || isNil(domain) || isNil(host)) {
if (isEmpty(apiKey) || isEmpty(domain) || isEmpty(host)) {
throw new Error('Mailgun transport settings are missing');
}
const mailgunSettings: MailgunConfig = {
Expand All @@ -40,6 +41,10 @@ export class EmailProvider {
this._transportName = 'smtp';

const { smtp } = transportSettings;
const { port, host } = smtp;
if (isEmpty(port) || isEmpty(host)) {
throw new Error('Smtp transport settings are missing');
}
smtp.auth.user = smtp.auth.username;
smtp.auth.pass = smtp.auth.password;
if (smtp.ignoreTls) {
Expand All @@ -57,16 +62,23 @@ export class EmailProvider {
});
} else if (transport === 'mandrill') {
this._transportName = 'mandrill';
const { apiKey } = transportSettings.mandrill.apiKey;
if (isEmpty(apiKey)) {
throw new Error('Mandrill transport settings are missing');
}
const mandrillSettings: MandrillConfig = {
auth: {
apiKey: transportSettings.mandrill.apiKey,
},
auth: { apiKey },
};
this._transport = new MandrillProvider(mandrillSettings);
} else if (transport === 'sendgrid') {
this._transportName = 'sendgrid';
const { residency, apiKey } = transportSettings.sendgrid;
if (isEmpty(apiKey) || isEmpty(residency)) {
throw new Error('Sendgrid transport settings are missing');
}
const sgSettings: SendGridConfig = {
apiKey: transportSettings['sendgrid'].apiKey,
apiKey,
residency,
};
this._transport = new SendgridProvider(sgSettings);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class SendgridProvider extends EmailProviderClass {
constructor(sgSettings: SendGridConfig) {
super(createTransport(sgTransport(sgSettings)));
this._sgClient = new Client();
this._sgClient.setDataResidency(sgSettings.residency);
this._sgClient.setApiKey(sgSettings.apiKey);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface SendGridConfig {
apiKey: string;
residency: string;
}

0 comments on commit 4e17224

Please sign in to comment.