-
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.
- Loading branch information
1 parent
6a1008a
commit 2b2862c
Showing
8 changed files
with
85 additions
and
81 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 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
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,31 +1,49 @@ | ||
// POST /registration/ | ||
// ➡️ send confirmation email to the email provided in application | ||
|
||
import { NextFunction, Request, Response, Router } from "express"; | ||
import { strongJwtVerification } from "../../middleware/verify-jwt"; | ||
import { Router } from "express"; | ||
import { RouterError } from "../../middleware/error-handler"; | ||
import { StatusCode } from "status-code-enum"; | ||
import { hasElevatedPerms } from "../../common/auth"; | ||
import { JwtPayload } from "../auth/auth-schemas"; | ||
import { MailInfoFormat, isValidMailInfo } from "./mail-formats"; | ||
import { sendMailWrapper } from "./mail-lib"; | ||
import { Role } from "../auth/auth-schemas"; | ||
import { sendMail } from "./mail-lib"; | ||
import specification, { Tag } from "../../middleware/specification"; | ||
import { MailInfoSchema, MailSendResultsSchema } from "./mail-schemas"; | ||
|
||
const mailRouter = Router(); | ||
|
||
mailRouter.post("/send/", strongJwtVerification, async (req: Request, res: Response, next: NextFunction) => { | ||
const payload = 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); | ||
}); | ||
mailRouter.post( | ||
"/send/", | ||
specification({ | ||
method: "post", | ||
path: "/mail/send/", | ||
tag: Tag.MAIL, | ||
role: Role.ADMIN, | ||
summary: "Sends an email", | ||
description: | ||
"**WARNING**: This endpoint is not very well documented, so make sure you know what you're doing before you use it directly.", | ||
body: MailInfoSchema, | ||
responses: { | ||
[StatusCode.SuccessOK]: { | ||
description: "The upload url", | ||
schema: MailSendResultsSchema, | ||
}, | ||
}, | ||
}), | ||
async (req, res, next) => { | ||
const mailInfo = req.body; | ||
|
||
try { | ||
const result = await sendMail(mailInfo); | ||
return res.status(StatusCode.SuccessOK).json(result.data); | ||
} catch (error) { | ||
return next( | ||
new RouterError(StatusCode.ClientErrorBadRequest, "EmailNotSent", { | ||
status: error.response?.status, | ||
code: error.code, | ||
}), | ||
); | ||
} | ||
}, | ||
); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { z } from "zod"; | ||
|
||
export const MailInfoSchema = z | ||
.object({ | ||
templateId: z.string(), | ||
recipients: z.array(z.string()), | ||
scheduleTime: z.optional(z.string()), | ||
subs: z.optional(z.record(z.unknown())), | ||
}) | ||
.openapi("MailInfo"); | ||
|
||
export type MailInfo = z.infer<typeof MailInfoSchema>; | ||
|
||
export const MailSendResultsSchema = z | ||
.object({ | ||
results: z.object({ | ||
total_rejected_recipients: z.number(), | ||
total_accepted_recipients: z.number(), | ||
id: z.string(), | ||
}), | ||
}) | ||
.openapi("MailSendResults", { | ||
example: { | ||
results: { | ||
total_rejected_recipients: 0, | ||
total_accepted_recipients: 1, | ||
id: "11668787493850529", | ||
}, | ||
}, | ||
}); | ||
export type MailSendResults = z.infer<typeof MailSendResultsSchema>; |
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