Skip to content

Commit

Permalink
Add newletter fetching endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez committed Jan 23, 2025
1 parent aa7ff32 commit b3e1de8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
70 changes: 67 additions & 3 deletions src/services/newsletter/newsletter-router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,77 @@
import { Router } from "express";
import { NewsletterSubscription, SubscribeRequestSchema } from "./newsletter-schemas";
import {
NewsletterIdSchema,
NewsletterNotFoundError,
NewsletterNotFoundErrorSchema,
NewsletterSubscription,
NewsletterSubscriptionSchema,
NewsletterSubscriptionsSchema,
SubscribeRequestSchema,
} from "./newsletter-schemas";
import Models from "../../common/models";
import { UpdateQuery } from "mongoose";
import { StatusCode } from "status-code-enum";
import specification, { Tag } from "../../middleware/specification";
import { SuccessResponseSchema } from "../../common/schemas";
import { Role } from "../auth/auth-schemas";
import { z } from "zod";

const newsletterRouter = Router();

newsletterRouter.get(
"/",
specification({
method: "get",
path: "/newsletter/",
tag: Tag.NEWSLETTER,
role: Role.ADMIN,
summary: "Gets all of the newsletter and their subscribers",
responses: {
[StatusCode.SuccessOK]: {
description: "Successfully got the subscribers",
schema: NewsletterSubscriptionsSchema,
},
},
}),
async (_req, res) => {
const subscriptions = await Models.NewsletterSubscription.find();
return res.status(StatusCode.SuccessOK).send(subscriptions);
},
);

newsletterRouter.get(
"/:id",
specification({
method: "get",
path: "/newsletter/{id}/",
tag: Tag.NEWSLETTER,
role: Role.ADMIN,
summary: "Gets a newsletter and it's subscribers",
parameters: z.object({
id: NewsletterIdSchema,
}),
responses: {
[StatusCode.SuccessOK]: {
description: "Successfully got the subscribers",
schema: NewsletterSubscriptionSchema,
},
[StatusCode.ClientErrorNotFound]: {
description: "That newsletter does not exist",
schema: NewsletterNotFoundErrorSchema,
},
},
}),
async (req, res) => {
const subscription = await Models.NewsletterSubscription.findOne({
newsletterId: req.params.id,
});
if (!subscription) {
return res.status(StatusCode.ClientErrorNotFound).send(NewsletterNotFoundError);
}
return res.status(StatusCode.SuccessOK).send(subscription);
},
);

newsletterRouter.post(
"/subscribe/",
specification({
Expand All @@ -24,8 +88,8 @@ newsletterRouter.post(
},
},
}),
async (request, res) => {
const { listName, emailAddress } = request.body;
async (req, res) => {
const { listName, emailAddress } = req.body;

const updateQuery: UpdateQuery<NewsletterSubscription> = { $addToSet: { subscribers: emailAddress } };
await Models.NewsletterSubscription.findOneAndUpdate({ newsletterId: listName }, updateQuery, { upsert: true });
Expand Down
26 changes: 24 additions & 2 deletions src/services/newsletter/newsletter-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { prop } from "@typegoose/typegoose";
import { z } from "zod";
import { CreateErrorAndSchema } from "../../common/schemas";

export class NewsletterSubscription {
@prop({ required: true })
Expand All @@ -12,14 +13,35 @@ export class NewsletterSubscription {
public subscribers: string[];
}

export const NewsletterIdSchema = z.string().openapi("NewsletterId", { example: "hackillinois2025_interest" });

export const NewsletterSubscriptionSchema = z
.object({
newsletterId: NewsletterIdSchema,
subscribers: z.array(z.string()),
})
.openapi("NewsletterSubscription", {
example: {
newsletterId: "hackillinois2025_interest",
subscribers: ["user1", "user2", "user3"],
},
});

export const NewsletterSubscriptionsSchema = z.array(NewsletterSubscriptionSchema).openapi("NewsletterSubscriptions");

export const SubscribeRequestSchema = z
.object({
listName: z.string(),
listName: NewsletterIdSchema,
emailAddress: z.string(),
})
.openapi("SubscribeRequest", {
example: {
listName: "recruitment_interest",
listName: "hackillinois2025_interest",
emailAddress: "[email protected]",
},
});

export const [NewsletterNotFoundError, NewsletterNotFoundErrorSchema] = CreateErrorAndSchema({
error: "NotFound",
message: "That newsletter doesn't exist!",
});

0 comments on commit b3e1de8

Please sign in to comment.