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

Feature/sophiakevin/mailing list #37

Merged
merged 9 commits into from
Apr 30, 2024
Merged
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
10 changes: 10 additions & 0 deletions backend/package-lock.json

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

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/nodemailer": "^6.4.14",
"@typescript-eslint/eslint-plugin": "^6.18.0",
"@typescript-eslint/parser": "^6.18.0",
"eslint": "^8.56.0",
Expand Down
3 changes: 1 addition & 2 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ app.use(
app.use("/api/subscribers", subscriberRoutes);
app.use("/api/member", memberRoutes);
app.use("/api/BackgroundImage", backgroundImageRoutes);

app.use("/api/eventDetails", eventDetailsRoutes);
app.use("/api/volunteerDetails", volunteerDetailsRoutes);
app.use("/api/testimonial", testimonialRoutes);
app.use("/api/newsletter", newsletterRoutes); // Use newsletter routes
app.use("/api/newsletter", newsletterRoutes);
app.use("/api/emails", emailRoutes);

/**
Expand Down
54 changes: 52 additions & 2 deletions backend/src/controllers/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,75 @@ import { RequestHandler } from "express";
import { validationResult } from "express-validator";
import Subscriber from "src/models/subscriber";
import validationErrorParser from "src/util/validationErrorParser";
import createHttpError from "http-errors";

export const createSubscriber: RequestHandler = async (req, res, next) => {
const errors = validationResult(req);
const { email } = req.body;

let memSince;
if (req.body.memberSince) {
memSince = req.body.memberSince;
} else {
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, "0");
const day = String(currentDate.getDate()).padStart(2, "0");
memSince = `${year}-${month}-${day}`;
}

try {
// validationErrorParser is a helper that throws 400 if there are errors
validationErrorParser(errors);
const subscriber = await Subscriber.create({
email: email,
firstName: req.body.firstName,
lastName: req.body.lastName,
memberSince: memSince,
quarterlyUpdates: req.body.quarterlyUpdates,
specialUpdates: req.body.specialUpdates,
});

// successfully created subscriber in db
res.status(201).json(subscriber);
} catch (error) {
next(error);
}
};

export const getAllSubscribers: RequestHandler = async (req, res, next) => {
try {
const mailingListEntries = await Subscriber.find({});

if (!mailingListEntries || mailingListEntries.length === 0) {
return res.status(200).json({ message: "No mailing list entries found." });
}

// const formattedMailingList = mailingListEntries.map((entry) => {
// if (!entry.firstName) {
// entry.firstName = "";
// }
// if (!entry.lastName) {
// entry.lastName = "";
// }
// });

res.status(200).json(mailingListEntries);
} catch (error) {
next(error);
}
};

export const deleteSubscriber: RequestHandler = async (req, res, next) => {
const { id } = req.params;

try {
const subscriber = await Subscriber.findByIdAndDelete(id);

if (!subscriber) {
throw createHttpError(404, "Subscriber not found.");
}

res.status(200).json(subscriber);
} catch (error) {
next(error);
}
};
1 change: 1 addition & 0 deletions backend/src/models/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const subscriberSchema = new Schema({
email: { type: String, required: true },
firstName: { type: String },
lastName: { type: String },
memberSince: { type: String },
quarterlyUpdates: { type: Boolean },
specialUpdates: { type: Boolean },
});
Expand Down
7 changes: 2 additions & 5 deletions backend/src/routes/subscriber.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/*
* Newsletter subscription route requests.
*/

import express from "express";
import * as SubscriberController from "src/controllers/subscriber";
import * as SubscriberValidator from "src/validators/subscriber";

const router = express.Router();

router.post("/", SubscriberValidator.createSubscriber, SubscriberController.createSubscriber);

router.get("/", SubscriberController.getAllSubscribers);
router.delete("/:id", SubscriberValidator.deleteSubscriber, SubscriberController.deleteSubscriber);
export default router;
11 changes: 11 additions & 0 deletions backend/src/validators/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import Subscriber from "src/models/subscriber";
* 4. normalize email then
* 5. check if email already exists in db
*/

const makeIDValidator = () =>
body("_id")
.exists()
.withMessage("_id is required")
.bail()
.isString()
.withMessage("_id must be a number");

const makeEmailValidator = () =>
body("email")
.trim()
Expand Down Expand Up @@ -50,3 +59,5 @@ export const createSubscriber = [
makeQuarterlyUpdatesValidator(),
makeSpecialUpdatesValidator(),
];

export const deleteSubscriber = [makeIDValidator()];
109 changes: 70 additions & 39 deletions frontend/package-lock.json

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

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@emotion/styled": "^11.11.0",
"@mui/icons-material": "^5.15.12",
"@mui/material": "^5.15.12",
"@mui/x-data-grid": "^7.1.1",
"envalid": "^8.0.0",
"firebase": "^10.11.0",
"html2canvas": "^1.4.1",
Expand Down
3 changes: 3 additions & 0 deletions frontend/public/close_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions frontend/public/copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading