-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
326 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Success, Error } from "./simple-comment-types" | ||
|
||
export abstract class AbstractNotificationService { | ||
/** | ||
* Send notification to moderators | ||
*/ | ||
abstract notifyModerators: ( | ||
message: string | ||
) => Promise<Success | Error> | 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { AbstractNotificationService } from "./AbstractNotificationService" | ||
|
||
export class NoOpNotificationService extends AbstractNotificationService { | ||
notifyModerators = (message: string): void => { | ||
console.info(`NoOpNotificationService: ${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,62 @@ | ||
import { AbstractNotificationService } from "./AbstractNotificationService" | ||
import { Error, Success } from "./simple-comment-types" | ||
import { MailService } from "@sendgrid/mail" | ||
import { config as dotEnvConfig } from "dotenv" | ||
dotEnvConfig() | ||
|
||
const _sendGridApiKey = process.env.NOTIFICATION_SERVICE_API_KEY | ||
|
||
const _moderatorContactEmails = process.env | ||
.SIMPLE_COMMENT_MODERATOR_CONTACT_EMAIL | ||
? process.env.SIMPLE_COMMENT_MODERATOR_CONTACT_EMAIL.split(",") | ||
: undefined | ||
|
||
export class SendGridNotificationService extends AbstractNotificationService { | ||
private readonly _mailService: MailService | ||
private readonly _moderatorContactEmails: string[] | ||
private readonly _sendGridApiKey: string | ||
|
||
constructor( | ||
mailService: MailService, | ||
sendGridApiKey?: string, | ||
moderatorContactEmails?: string[] | ||
) { | ||
super() | ||
|
||
const apiKey = sendGridApiKey ?? _sendGridApiKey | ||
if (apiKey === undefined) | ||
throw "NOTIFICATION_SERVICE_API_KEY is not set in environmental variables" | ||
this._sendGridApiKey = apiKey | ||
|
||
const emails = moderatorContactEmails ?? _moderatorContactEmails | ||
|
||
if (emails === undefined || emails.length === 0) | ||
throw `SIMPLE_COMMENT_MODERATOR_CONTACT_EMAIL is not set in environmental variables` | ||
|
||
this._moderatorContactEmails = emails | ||
mailService.setApiKey(this._sendGridApiKey) | ||
this._mailService = mailService | ||
} | ||
|
||
notifyModerators = async (body: string): Promise<Error | Success> => { | ||
const messages = this._moderatorContactEmails.map(email => ({ | ||
to: email, | ||
from: "[email protected]", // Sender's email address | ||
subject: "Simple Comment Notification", | ||
text: `${body}`, | ||
})) | ||
|
||
const sendPromises = messages.map(message => | ||
this._mailService.send(message) | ||
) | ||
const sendResults = await Promise.race(sendPromises) | ||
|
||
if (sendResults[0]?.statusCode !== 202) { | ||
const { statusCode, body } = sendResults[0] | ||
const resultsBody = JSON.stringify(body) | ||
return { statusCode, body: resultsBody } | ||
} else { | ||
return { statusCode: 202, body: "Emails sent successfully" } | ||
} | ||
} | ||
} |
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,129 @@ | ||
import { SendGridNotificationService } from "../../lib/SendGridNotificationService" | ||
import { ClientResponse, MailService } from "@sendgrid/mail" | ||
|
||
jest.mock("dotenv", () => ({ | ||
config: jest.fn(() => { | ||
process.env.SIMPLE_COMMENT_MODERATOR_CONTACT_EMAIL = | ||
"[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]" | ||
|
||
process.env.NOTIFICATION_SERVICE_API_KEY = "SG.from.test.env" | ||
}), | ||
})) | ||
|
||
describe("SendGridNotificationService", () => { | ||
let sendGridNotificationService: SendGridNotificationService | ||
let mailServiceMock: jest.Mocked<MailService> | ||
|
||
const moderatorContactEmails = ["a1@example,com", "[email protected]"] | ||
const sendGridTestApiKey = "SG.test" | ||
|
||
const originalEnv = process.env | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
mailServiceMock = { | ||
setApiKey: jest.fn(), | ||
send: jest.fn(), | ||
} as unknown as jest.Mocked<MailService> | ||
|
||
sendGridNotificationService = new SendGridNotificationService( | ||
mailServiceMock, | ||
sendGridTestApiKey, | ||
moderatorContactEmails | ||
) | ||
}) | ||
|
||
afterEach(() => { | ||
process.env = originalEnv | ||
}) | ||
|
||
it("should set API key", async () => { | ||
expect(mailServiceMock.setApiKey).toHaveBeenCalledWith(sendGridTestApiKey) | ||
}) | ||
|
||
it("should use environmental variable for API key", async () => { | ||
new SendGridNotificationService(mailServiceMock) | ||
expect(mailServiceMock.setApiKey).toHaveBeenCalledWith("SG.from.test.env") | ||
}) | ||
|
||
it("should handle email sending failure", async () => { | ||
const body = "Test message" | ||
const mockErrorResponse: ClientResponse = { | ||
statusCode: 500, | ||
body: {}, | ||
headers: undefined, | ||
} | ||
|
||
mailServiceMock.send.mockResolvedValueOnce([mockErrorResponse, {}]) | ||
|
||
const result = await sendGridNotificationService.notifyModerators(body) | ||
|
||
expect(mailServiceMock.setApiKey).toHaveBeenCalledWith(sendGridTestApiKey) | ||
expect(mailServiceMock.send).toHaveBeenCalledTimes( | ||
moderatorContactEmails.length | ||
) | ||
expect(result.statusCode).toEqual(mockErrorResponse.statusCode) | ||
}) | ||
|
||
it("should throw given empty moderator contact emails", async () => { | ||
expect( | ||
() => | ||
new SendGridNotificationService(mailServiceMock, sendGridTestApiKey, []) | ||
).toThrowError( | ||
"SIMPLE_COMMENT_MODERATOR_CONTACT_EMAIL is not set in environmental variables" | ||
) | ||
}) | ||
|
||
it("should throw given undefined moderator contact emails", async () => { | ||
expect( | ||
() => | ||
new SendGridNotificationService(mailServiceMock, sendGridTestApiKey, []) | ||
).toThrowError( | ||
"SIMPLE_COMMENT_MODERATOR_CONTACT_EMAIL is not set in environmental variables" | ||
) | ||
}) | ||
|
||
it("should send notification to moderators", async () => { | ||
const body = "Test message" | ||
const clientResponse = { | ||
statusCode: 202, | ||
body: {}, | ||
headers: undefined, | ||
} | ||
const mockResponse: [ClientResponse, object] = [clientResponse, {}] | ||
|
||
mailServiceMock.send.mockResolvedValueOnce(mockResponse) | ||
|
||
const result = await sendGridNotificationService.notifyModerators(body) | ||
|
||
expect(mailServiceMock.setApiKey).toHaveBeenCalledWith(sendGridTestApiKey) | ||
expect(mailServiceMock.send).toHaveBeenCalledTimes( | ||
moderatorContactEmails.length | ||
) | ||
expect(result.statusCode).toEqual(202) | ||
}) | ||
|
||
it("should send mulitple emails given comma-separated SIMPLE_COMMENT_MODERATOR_CONTACT_EMAIL", async () => { | ||
const body = "Test message" | ||
const clientResponse = { | ||
statusCode: 202, | ||
body: {}, | ||
headers: undefined, | ||
} | ||
const mockResponse: [ClientResponse, object] = [clientResponse, {}] | ||
|
||
mailServiceMock.send.mockResolvedValue(mockResponse) | ||
|
||
const service = new SendGridNotificationService( | ||
mailServiceMock, | ||
sendGridTestApiKey | ||
) | ||
const result = await service.notifyModerators(body) | ||
|
||
expect(mailServiceMock.setApiKey).toHaveBeenCalledWith(sendGridTestApiKey) | ||
expect(mailServiceMock.send).toHaveBeenCalledTimes(10) | ||
expect(result.statusCode).toEqual(202) | ||
}) | ||
|
||
// Add more test cases as needed | ||
}) |
Oops, something went wrong.