Skip to content

Commit

Permalink
Do not hardcode SendGrid requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
BambanzaJuniorThe2nd committed Aug 27, 2020
1 parent 5ddb1e8 commit 7da684e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 42 deletions.
6 changes: 4 additions & 2 deletions server/src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export interface AppConfig {
* Google API Client ID
*/
googleClientId: string;
sgApiKey: string;
sendgridApiKey: string;
socialReliefEmail: string;
};

export function loadAppConfigFromEnv(env: { [key: string]: string }): AppConfig {
Expand Down Expand Up @@ -135,6 +136,7 @@ export function loadAppConfigFromEnv(env: { [key: string]: string }): AppConfig
distributionInterval: (env.DISTRIBUTION_INTERVAL && Number(env.DISTRIBUTION_INTERVAL)) || 1,
statsComputationInterval: (env.STATS_COMPUTATION_INTERVAL && Number(env.STATS_COMPUTATION_INTERVAL)) || 1,
googleClientId: env.GOOGLE_CLIENT_ID,
sgApiKey: env.SENDGRID_API_KEY
sendgridApiKey: env.SENDGRID_API_KEY || '',
socialReliefEmail: env.SOCIAL_RELIEF_EMAIL || '[email protected]'
};
}
7 changes: 4 additions & 3 deletions server/src/core/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createDbConnectionFailedError } from './error';
import { DonationDistributions } from './distribution';
import { SystemLocks } from './system-lock';
import { AtSmsProvider } from './sms';
import { sgEmailProvider } from './email';
import { sendgridEmailProvider } from './email';
import { Invitations } from './invitation';
import { EventBus } from './event';
import { UserNotifications } from './user-notification';
Expand Down Expand Up @@ -62,8 +62,9 @@ export async function bootstrap(config: AppConfig): Promise<App> {
apiKey: config.atApiKey,
sender: config.atSmsSender
});
const emailProvider = new sgEmailProvider({
apiKey: config.sgApiKey
const emailProvider = new sendgridEmailProvider({
apiKey: config.sendgridApiKey,
socialReliefEmail: config.socialReliefEmail
});

// starts listening to events when instantiated
Expand Down
2 changes: 1 addition & 1 deletion server/src/core/email/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './types';
export { sgEmailProvider } from './sg-email-provider';
export { sendgridEmailProvider } from './sendgrid-email-provider';
38 changes: 38 additions & 0 deletions server/src/core/email/sendgrid-email-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { EmailProvider } from './types';
import { rethrowIfAppError, createEmailDeliveryFailedError, createSendGridApiError } from '../error';
import sgMail = require('@sendgrid/mail');

export interface sendgridEmailProviderArgs {
apiKey: string,
socialReliefEmail: string
};

export class sendgridEmailProvider implements EmailProvider{
private socialReliefEmail: string;

constructor(args: sendgridEmailProviderArgs) {
sgMail.setApiKey(args.apiKey);
this.socialReliefEmail = args.socialReliefEmail;
}

async sendEmail(to: string, message: string): Promise<void> {
try {
const res = await sgMail.send({
to,
from: this.socialReliefEmail,
subject: 'Social Relief Notification',
text: message,
});

if (res[0].statusCode !== 202) {
throw createEmailDeliveryFailedError('Failed to send email');
}
}
catch (e) {
console.log(e);
rethrowIfAppError(e);
throw createSendGridApiError(e.message);
}
}
}

36 changes: 0 additions & 36 deletions server/src/core/email/sg-email-provider.ts

This file was deleted.

5 changes: 5 additions & 0 deletions server/src/core/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type ErrorCode =
| 'b2cRequestFailed'
| 'serverError'
| 'atApiError'
| 'sendgridApiError'
| 'manualPayApiError'
| 'flutterwaveApiError'
| 'serverError'
Expand Down Expand Up @@ -108,6 +109,10 @@ export function createAtApiError(message: string = messages.ERROR_AT_API_ERROR)
return createAppError(message, 'atApiError');
}

export function createSendGridApiError(message: string) {
return createAppError(message, 'sendgridApiError');
}

export function createManualPayApiError(message: string) {
return createAppError(message, 'manualPayApiError');
}
Expand Down

0 comments on commit 7da684e

Please sign in to comment.