Skip to content

Commit

Permalink
58
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondwusuper committed Feb 24, 2025
1 parent 31bc746 commit 6c75ea0
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 16 deletions.
2 changes: 2 additions & 0 deletions backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import express, { Express, NextFunction, Request, Response } from "express";
import { isHttpError } from "http-errors";

import announcementRoutes from "./src/routes/announcement";
import directoryRoutes from "./src/routes/directory";
import discussionRoutes from "./src/routes/discussion";
import replyRoutes from "./src/routes/reply";

Expand All @@ -27,6 +28,7 @@ app.use('/api/announcement', announcementRoutes)
app.use("/api/users", userRoutes);
app.use("/api/discussions", discussionRoutes);
app.use("/api/replies", replyRoutes);
app.use("/api/directory", directoryRoutes);

// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
app.use((error: unknown, req: Request, res: Response, next: NextFunction) => {
Expand Down
51 changes: 36 additions & 15 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
"express": "^4.21.2",
"express-validator": "^7.2.1",
"mongodb": "^5.7.0",
"mongoose": "^8.9.5"
"mongoose": "^8.9.5",
"nodemailer": "^6.10.0"
},
"devDependencies": {
"@eslint/eslintrc": "^3.2.0",
"@eslint/js": "^9.17.0",
"@types/express": "^5.0.0",
"@types/node": "^22.10.5",
"@types/nodemailer": "^6.4.17",
"@typescript-eslint/eslint-plugin": "^8.19.0",
"@typescript-eslint/parser": "^8.19.0",
"eslint": "^8.57.1",
Expand Down
59 changes: 59 additions & 0 deletions backend/src/controllers/directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// src/controllers/directoryController.ts
import { Request, Response } from "express";

import { sendApprovalEmail, sendDenialEmail } from "../services/emailService";

type ApproveDirectoryRequestBody = {
email: string;
name: string;
};

type DenyDirectoryRequestBody = {
email: string;
name: string;
reason: string;
};

export const approveDirectoryEntry = async (
req: Request<unknown, unknown, ApproveDirectoryRequestBody>,
res: Response,
) => {
try {
const { email, name } = req.body;

if (!email || !name) {
res.status(400).json({ error: "Email and name are required" });
return;
}

// Send the approval email
await sendApprovalEmail(email, name);

res.status(200).json({ message: "Directory entry approved and email sent" });
} catch (error) {
console.error("Error approving directory entry:", error);
res.status(500).json({ error: "Internal server error" });
}
};

export const denyDirectoryEntry = async (
req: Request<unknown, unknown, DenyDirectoryRequestBody>,
res: Response,
) => {
try {
const { email, name, reason } = req.body;

if (!email || !name || !reason) {
res.status(400).json({ error: "Email, name, and reason are required" });
return;
}

// Send the denial email
await sendDenialEmail(email, name, reason);

res.status(200).json({ message: "Directory entry denied and email sent" });
} catch (error) {
console.error("Error denying directory entry:", error);
res.status(500).json({ error: "Internal server error" });
}
};
11 changes: 11 additions & 0 deletions backend/src/routes/directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// src/routes/directoryRoutes.ts
import express from "express";

import * as DirectoryController from "../controllers/directory";

const router = express.Router();

router.post("/approve", DirectoryController.approveDirectoryEntry);
router.post("/deny", DirectoryController.denyDirectoryEntry);

export default router;
46 changes: 46 additions & 0 deletions backend/src/services/emailService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// src/services/sendEmail.ts
import { config } from "dotenv";
import nodemailer from "nodemailer";
config();

const transporter = nodemailer.createTransport({
service: "gmail", // You can change this if you're using another provider
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_APP_PASSWORD,
},
});

const sendApprovalEmail = async (to: string, name: string) => {
const mailOptions = {
from: process.env.EMAIL_USER,
to: process.env.EMAIL_NOTIFICATIONS_RECIPIENT,
subject: "Your Directory Approval",
html: `<p>Dear ${name},</p><p>Your directory entry has been approved!</p><p>Thank you for your submission.</p>`,
};

try {
await transporter.sendMail(mailOptions);
} catch (error) {
console.error("Error sending approval email:", error);
throw new Error("Failed to send approval email");
}
};

const sendDenialEmail = async (to: string, name: string, reason: string) => {
const mailOptions = {
from: process.env.EMAIL_USER,
to: process.env.EMAIL_NOTIFICATIONS_RECIPIENT,
subject: "Your Directory Denial",
html: `<p>Dear ${name},</p><p>Your directory entry has been denied for the following reason: ${reason}</p><p>If you have any questions, feel free to reach out.</p>`,
};

try {
await transporter.sendMail(mailOptions);
} catch (error) {
console.error("Error sending denial email:", error);
throw new Error("Failed to send denial email");
}
};

export { sendApprovalEmail, sendDenialEmail };
6 changes: 6 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
11 changes: 11 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}

0 comments on commit 6c75ea0

Please sign in to comment.