-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not hardcode SendGrid requirements
- Loading branch information
1 parent
5ddb1e8
commit 7da684e
Showing
6 changed files
with
52 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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]' | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters