Skip to content

Commit

Permalink
Merge pull request #17 from ReflectionsProjections/dev/divya/fix-subs…
Browse files Browse the repository at this point in the history
…cription-endpoint

🐛 Redoing subscription schema+endpoint
  • Loading branch information
divyack2 authored Apr 12, 2024
2 parents 3cd8e1e + d69c3cc commit 921663c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mongoose, { Schema } from "mongoose";
import { RoleValidator, RoleSchema } from "./services/auth/auth-schema";
import { EventSchema, EventValidator } from "./services/events/events-schema";
import {
SubscriptionValidator,
SubscriptionSchemaValidator,
SubscriptionSchema,
} from "./services/subscription/subscription-schema";

Expand Down Expand Up @@ -38,9 +38,9 @@ function initializeModel(
export const Database = {
ROLES: initializeModel("roles", RoleSchema, RoleValidator),
EVENTS: initializeModel("events", EventSchema, EventValidator),
SUBSCRIPTION: initializeModel(
"subscription",
SUBSCRIPTIONS: initializeModel(
"subscriptions",
SubscriptionSchema,
SubscriptionValidator
SubscriptionSchemaValidator
),
};
5 changes: 4 additions & 1 deletion src/services/subscription/subscription-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ const subscriptionRouter = Router();
// Create a new subscription
subscriptionRouter.post("/", async (req, res, next) => {
try {
// Validate the incoming user subscription
const subscriptionData = SubscriptionValidator.parse(req.body);
await Database.SUBSCRIPTION.findOneAndUpdate(

// Upsert the user info into the corresponding Subscription collection
await Database.SUBSCRIPTIONS.findOneAndUpdate(
{ mailingList: subscriptionData.mailingList },
{ $push: { subscriptions: subscriptionData.email } },
{ upsert: true, new: true }
Expand Down
16 changes: 13 additions & 3 deletions src/services/subscription/subscription-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ import mongoose from "mongoose";
import { z } from "zod";
import { MailingListName } from "../../config";

// Zod schema for subscription
// Zod schema for incoming user subscriptions
const SubscriptionValidator = z.object({
email: z.string().email(),
mailingList: MailingListName,
});

// Zod schema for validating subscription lists
const SubscriptionSchemaValidator = z.object({
mailingList: MailingListName,
subscriptions: z.array(z.string().email()),
});

// Mongoose schema for subscription
const SubscriptionSchema = new mongoose.Schema({
email: { type: String, required: true },
mailingList: { type: String, required: true },
subscriptions: [{ type: String, required: true }],
});

export { SubscriptionValidator, SubscriptionSchema };
export {
SubscriptionValidator,
SubscriptionSchemaValidator,
SubscriptionSchema,
};

0 comments on commit 921663c

Please sign in to comment.