Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/aydan/mailing service #70

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import errorHandler from "./middleware/error-handler";
import attendeeRouter from "./services/attendees/attendee-router";
import authRouter from "./services/auth/auth-router";
import eventsRouter from "./services/events/events-router";
import mailRouter from "./services/mail/mail-router";
import notificationsRouter from "./services/notifications/notifications-router";
import registrationRouter from "./services/registration/registration-router";
import s3Router from "./services/s3/s3-router";
Expand All @@ -36,6 +37,7 @@ app.use("/", bodyParser.json());
app.use("/attendee", attendeeRouter);
app.use("/auth", authRouter);
app.use("/events", eventsRouter);
app.use("/mail", mailRouter);
app.use("/notifications", notificationsRouter);
app.use("/registration", registrationRouter);
app.use("/s3", s3Router);
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export const Config = {
AUTH_CALLBACK_URI_BASE:
"https://api.reflectionsprojections.org/auth/callback/",

AUTH_ADMIN_WHITELIST: new Set([
"[email protected]", // Aydan Pirani (Dev)
"[email protected]", // Divya Koya (Dev)
"[email protected]", // Ritika Vithani (Director)
"[email protected]", // Ojaswee Chaudhary (Director)
]),

JWT_SIGNING_SECRET: getEnv("JWT_SIGNING_SECRET"),
JWT_EXPIRATION_TIME: "1 day",

Expand All @@ -42,6 +49,8 @@ export const Config = {
// QR Scanning
QR_HASH_ITERATIONS: 10000,
QR_HASH_SECRET: getEnv("QR_HASH_SECRET"),

MAIL_TEMPLATE_REGEX: /\${{([^{}]+)}}/g,
};

export const DeviceRedirects: Record<string, string> = {
Expand Down
5 changes: 5 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import {
NotificationsSchema,
NotificationsValidator,
} from "./services/notifications/notifications-schema";
import {
TemplateSchema,
TemplateValidator,
} from "./services/mail/templates/templates-schema";

mongoose.set("toObject", { versionKey: false });

Expand Down Expand Up @@ -61,6 +65,7 @@ export const Database = {
RegistrationSchema,
RegistrationValidator
),
TEMPLATES: initializeModel("templates", TemplateSchema, TemplateValidator),
NOTIFICATIONS: initializeModel(
"notifications",
NotificationsSchema,
Expand Down
9 changes: 8 additions & 1 deletion src/services/auth/auth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
import { Config } from "../../config";
import { Database } from "../../database";
import { Role } from "./auth-models";

export function createGoogleStrategy(device: string) {
return new GoogleStrategy(
Expand All @@ -17,9 +18,15 @@
const name = profile.displayName;
const email = profile._json.email;

let roles = [];

Check failure on line 21 in src/services/auth/auth-utils.ts

View workflow job for this annotation

GitHub Actions / lint

'roles' is never reassigned. Use 'const' instead

Check failure on line 21 in src/services/auth/auth-utils.ts

View workflow job for this annotation

GitHub Actions / lint

'roles' is never reassigned. Use 'const' instead

if (Config.AUTH_ADMIN_WHITELIST.has(email ?? "")) {
roles.push(Role.Values.ADMIN);
}

Database.ROLES.findOneAndUpdate(
{ userId: userId },
{ userId, name, email },
{ userId, name, email, roles },
{ upsert: true }
)
.then(() => cb(null, profile))
Expand Down
5 changes: 5 additions & 0 deletions src/services/mail/drafts/drafts-subrouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Router } from "express";

const draftsSubRouter = Router();

export default draftsSubRouter;
Empty file.
5 changes: 5 additions & 0 deletions src/services/mail/lists/lists-subrouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Router } from "express";

const listsSubRouter = Router();

export default listsSubRouter;
13 changes: 13 additions & 0 deletions src/services/mail/mail-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import draftsSubRouter from "./drafts/drafts-subrouter";
import listsSubRouter from "./lists/lists-subrouter";
import templatesSubRouter from "./templates/templates-subrouter";

import { Router } from "express";

const mailRouter = Router();

mailRouter.use("/drafts", draftsSubRouter);
mailRouter.use("/lists", listsSubRouter);
mailRouter.use("/templates", templatesSubRouter);

export default mailRouter;
31 changes: 31 additions & 0 deletions src/services/mail/templates/templates-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Schema } from "mongoose";
import { z } from "zod";

export const TemplateValidator = z.object({
templateId: z.string().regex(/^\S*$/, {
message: "Spaces Not Allowed",
}),
subject: z.string(),
content: z.string(),
substitutions: z.string().array().default([]),
});

export const TemplateSchema = new Schema({
templateId: {
type: String,
required: true,
unique: true,
},
subject: {
type: String,
required: true,
},
content: {
type: String,
required: true,
},
substitutions: {
type: [String],
required: true,
},
});
111 changes: 111 additions & 0 deletions src/services/mail/templates/templates-subrouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { Router } from "express";
import RoleChecker from "../../../middleware/role-checker";
import { Role } from "../../auth/auth-models";
import { Database } from "../../../database";
import { StatusCodes } from "http-status-codes";
import { TemplateValidator } from "./templates-schema";
import { Config } from "../../../config";

const templatesSubRouter = Router();

templatesSubRouter.get(
"/",
RoleChecker([Role.Values.STAFF]),
async (req, res) => {
const templates = await Database.TEMPLATES.find();
const cleanedTemplates = templates.map((x) => x.toObject());
return res.status(StatusCodes.OK).json(cleanedTemplates);
}
);

templatesSubRouter.post(
"/",
RoleChecker([Role.Values.ADMIN]),
async (req, res) => {
try {
let templateData = TemplateValidator.parse(req.body);

Check failure on line 26 in src/services/mail/templates/templates-subrouter.ts

View workflow job for this annotation

GitHub Actions / lint

'templateData' is never reassigned. Use 'const' instead

Check failure on line 26 in src/services/mail/templates/templates-subrouter.ts

View workflow job for this annotation

GitHub Actions / lint

'templateData' is never reassigned. Use 'const' instead
let substitutions = templateData.content.matchAll(

Check failure on line 27 in src/services/mail/templates/templates-subrouter.ts

View workflow job for this annotation

GitHub Actions / lint

'substitutions' is never reassigned. Use 'const' instead

Check failure on line 27 in src/services/mail/templates/templates-subrouter.ts

View workflow job for this annotation

GitHub Actions / lint

'substitutions' is never reassigned. Use 'const' instead
Config.MAIL_TEMPLATE_REGEX
);
const subVars = Array.from(
substitutions,
(substitutions) => substitutions[1]
);

await Database.TEMPLATES.create({
...templateData,
substitutions: subVars,
});
return res.sendStatus(StatusCodes.CREATED);
} catch (error) {
return res.status(StatusCodes.BAD_REQUEST).send(error);
}
}
);

templatesSubRouter.put(
"/",
RoleChecker([Role.Values.ADMIN]),
async (req, res) => {
try {
let templateData = TemplateValidator.parse(req.body);

Check failure on line 51 in src/services/mail/templates/templates-subrouter.ts

View workflow job for this annotation

GitHub Actions / lint

'templateData' is never reassigned. Use 'const' instead

Check failure on line 51 in src/services/mail/templates/templates-subrouter.ts

View workflow job for this annotation

GitHub Actions / lint

'templateData' is never reassigned. Use 'const' instead
let substitutions = templateData.content.matchAll(

Check failure on line 52 in src/services/mail/templates/templates-subrouter.ts

View workflow job for this annotation

GitHub Actions / lint

'substitutions' is never reassigned. Use 'const' instead

Check failure on line 52 in src/services/mail/templates/templates-subrouter.ts

View workflow job for this annotation

GitHub Actions / lint

'substitutions' is never reassigned. Use 'const' instead
Config.MAIL_TEMPLATE_REGEX
);
const subVars = Array.from(
substitutions,
(substitutions) => substitutions[1]
);

const updateResult = await Database.TEMPLATES.findOneAndUpdate(
{ templateId: templateData.templateId },
{ ...templateData, substitutions: subVars }
);

if (!updateResult) {
return res
.status(StatusCodes.NOT_FOUND)
.send({ error: "NoSuchId" });
}

return res.sendStatus(StatusCodes.OK);
} catch (error) {
return res.status(StatusCodes.BAD_REQUEST).send(error);
}
}
);

templatesSubRouter.get(
"/:TEMPLATEID",
RoleChecker([Role.Values.STAFF]),
async (req, res) => {
const templateId = req.params.TEMPLATEID;
const templateInfo = await Database.TEMPLATES.findOne({
templateId: templateId,
});
if (!templateInfo) {
return res
.status(StatusCodes.NOT_FOUND)
.send({ error: "NoSuchId" });
}
return res.status(StatusCodes.OK).json(templateInfo?.toObject());
}
);

templatesSubRouter.delete(
"/:TEMPLATEID",
RoleChecker([Role.Values.ADMIN]),
async (req, res) => {
try {
const templateId = req.params.TEMPLATEID;
await Database.TEMPLATES.findOneAndDelete({
templateId: templateId,
});
return res.sendStatus(StatusCodes.NO_CONTENT);
} catch (error) {
return res.status(StatusCodes.BAD_REQUEST).send(error);
}
}
);

export default templatesSubRouter;
Loading