-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Infra for mail service * Cleaned up code for registration * Registration fix * fixing docs * fix formatter --------- Co-authored-by: Lasya <[email protected]>
- Loading branch information
1 parent
7cc95ed
commit 1b6f9e9
Showing
11 changed files
with
372 additions
and
116 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
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,15 @@ | ||
export function isBoolean(value: unknown): boolean { | ||
return typeof value === "boolean"; | ||
} | ||
|
||
export function isNumber(value: unknown): boolean { | ||
return typeof value === "number"; | ||
} | ||
|
||
export function isString(value: unknown): boolean { | ||
return typeof value === "string"; | ||
} | ||
|
||
export function isArrayOfType(arr: unknown[], typeChecker: (value: unknown) => boolean): boolean { | ||
return Array.isArray(arr) && arr.every(typeChecker); | ||
} |
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,27 @@ | ||
import { isString, isArrayOfType } from "../../formatTools.js"; | ||
|
||
export interface MailInfoFormat { | ||
templateId: string; | ||
recipients: string[]; | ||
scheduleTime?: string; | ||
} | ||
|
||
export function isValidMailInfo(mailInfo: MailInfoFormat): boolean { | ||
if (!mailInfo) { | ||
return false; | ||
} | ||
|
||
if (!isString(mailInfo.templateId)) { | ||
return false; | ||
} | ||
|
||
if (!isArrayOfType(mailInfo.recipients, isString)) { | ||
return false; | ||
} | ||
|
||
if (mailInfo.scheduleTime && !isString(mailInfo.scheduleTime)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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,48 @@ | ||
import Config from "../../config.js"; | ||
import axios, { AxiosResponse } from "axios"; | ||
import { Response, NextFunction } from "express"; | ||
import { StatusCode } from "status-code-enum"; | ||
import { RouterError } from "../../middleware/error-handler.js"; | ||
import { MailInfoFormat } from "./mail-formats.js"; | ||
|
||
export async function sendMailWrapper(res: Response, next: NextFunction, mailInfo: MailInfoFormat): Promise<void | Response> { | ||
try { | ||
const result = await sendMail(mailInfo.templateId, mailInfo.recipients, mailInfo.scheduleTime); | ||
return res.status(StatusCode.SuccessOK).send(result.data); | ||
} catch (error) { | ||
return next( | ||
new RouterError(StatusCode.ClientErrorBadRequest, "EmailNotSent", { | ||
status: error.response?.status, | ||
code: error.code, | ||
}), | ||
); | ||
} | ||
} | ||
|
||
function sendMail(templateId: string, emails: string[], scheduleTime?: string): Promise<AxiosResponse> { | ||
const options = scheduleTime ? { start_time: scheduleTime } : {}; | ||
const recipients = emails.map((emailAddress: string) => { | ||
return { address: `${emailAddress}` }; | ||
}); | ||
|
||
const data = { | ||
options: options, | ||
recipients: recipients, | ||
content: { | ||
template_id: templateId, | ||
}, | ||
}; | ||
|
||
const config = { | ||
method: "post", | ||
maxBodyLength: Infinity, | ||
headers: { | ||
"Content-Type": "application/json", | ||
Accept: "application/json", | ||
Authorization: Config.SPARKPOST_KEY, | ||
}, | ||
data: data, | ||
}; | ||
|
||
return axios.post(Config.SPARKPOST_URL, data, config); | ||
} |
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,31 @@ | ||
// POST /registration/ | ||
// ➡️ send confirmation email to the email provided in application | ||
|
||
import { NextFunction, Request, Response, Router } from "express"; | ||
import { strongJwtVerification } from "../../middleware/verify-jwt.js"; | ||
import { RouterError } from "../../middleware/error-handler.js"; | ||
import { StatusCode } from "status-code-enum"; | ||
import { hasElevatedPerms } from "../auth/auth-lib.js"; | ||
import { JwtPayload } from "../auth/auth-models.js"; | ||
import { MailInfoFormat, isValidMailInfo } from "./mail-formats.js"; | ||
import { sendMailWrapper } from "./mail-lib.js"; | ||
|
||
const mailRouter: Router = Router(); | ||
|
||
mailRouter.post("/send/", strongJwtVerification, async (req: Request, res: Response, next: NextFunction) => { | ||
const payload: JwtPayload = res.locals.payload as JwtPayload; | ||
|
||
if (!hasElevatedPerms(payload)) { | ||
return next(new RouterError(StatusCode.ClientErrorForbidden, "Forbidden")); | ||
} | ||
|
||
const mailInfo = req.body as MailInfoFormat; | ||
|
||
if (!isValidMailInfo(mailInfo)) { | ||
return next(new RouterError(StatusCode.ClientErrorBadRequest, "BadRequest")); | ||
} | ||
|
||
return sendMailWrapper(res, next, mailInfo); | ||
}); | ||
|
||
export default mailRouter; |
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
Oops, something went wrong.