-
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.
Merge pull request #113 from alphamanuscript/86-email-notifications
Implement email service
- Loading branch information
Showing
12 changed files
with
111 additions
and
2 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
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
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,2 @@ | ||
export * from './types'; | ||
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, | ||
emailSender: string | ||
}; | ||
|
||
export class SendGridEmailProvider implements EmailProvider{ | ||
private emailSender: string; | ||
|
||
constructor(args: SendGridEmailProviderArgs) { | ||
sgMail.setApiKey(args.apiKey); | ||
this.emailSender = args.emailSender; | ||
} | ||
|
||
async sendEmail(to: string, message: string): Promise<void> { | ||
try { | ||
const res = await sgMail.send({ | ||
to, | ||
from: this.emailSender, | ||
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 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,3 @@ | ||
export interface EmailProvider { | ||
sendEmail(to: string, message: string): Promise<void>; | ||
} |
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
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
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