Skip to content

Commit

Permalink
Merge pull request #7 from ReflectionsProjections/subscription
Browse files Browse the repository at this point in the history
Subscription
  • Loading branch information
AydanPirani authored Apr 11, 2024
2 parents 008550a + 6303fce commit 0f2fed0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "ENV=DEVELOPMENT nodemon -w src/ -x tsx src/app.ts",
"start": "ENV=PRODUCTION tsx src/app.ts",
"dev": "nodemon -w src/ -x tsx src/app.ts",
"start": "tsx src/app.ts",
"lint": "yarn prettier . --write",
"kill": "kill -9 $(lsof -t -i :3000)",
"build": "tsc"
Expand Down
3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import bodyParser from "body-parser";
import errorHandler from "./middleware/error-handler";

import authRouter from "./services/auth/auth-router";
import subscriptionRouter from "./services/subscription/subscription-router";

const app = express();

Expand All @@ -20,7 +21,9 @@ app.use("/", morgan("dev"));

app.use("/", bodyParser.json());

// API routes
app.use("/auth", authRouter);
app.use("/subscription", subscriptionRouter);

app.get("/status", (_, res) => {
console.log(StatusCodes.OK);
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { getEnv } from "./utilities";

export const Environment = z.enum(["PRODUCTION", "DEVELOPMENT", "TESTING"]);

export const MailingListName = z.enum(["rp_interest"]);

export const Config = {
DEFAULT_APP_PORT: 3000,
ENV: Environment.parse(getEnv("ENV")),
Expand Down
9 changes: 9 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import mongoose, { Schema } from "mongoose";
import { RoleInfo, RoleSchema } from "./services/auth/auth-schema";
import {
SubscriptionValidator,
SubscriptionSchema,
} from "./services/subscription/subscription-schema";

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

Expand Down Expand Up @@ -32,4 +36,9 @@ function initializeModel(
// Example usage
export const Database = {
ROLES: initializeModel("roles", RoleSchema, RoleInfo),
SUBSCRIPTION: initializeModel(
"subscription",
SubscriptionSchema,
SubscriptionValidator
),
};
23 changes: 23 additions & 0 deletions src/services/subscription/subscription-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Router } from "express";
import { StatusCodes } from "http-status-codes";
import { SubscriptionValidator } from "./subscription-schema";
import { Database } from "../../database";

const subscriptionRouter = Router();

// Create a new subscription
subscriptionRouter.post("/", async (req, res, next) => {
try {
const subscriptionData = SubscriptionValidator.parse(req.body);
await Database.SUBSCRIPTION.findOneAndUpdate(
{ mailingList: subscriptionData.mailingList },
{ $push: { subscriptions: subscriptionData.email } },
{ upsert: true, new: true }
);
return res.status(StatusCodes.CREATED).json(subscriptionData);
} catch (error) {
next(error);
}
});

export default subscriptionRouter;
17 changes: 17 additions & 0 deletions src/services/subscription/subscription-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mongoose from "mongoose";
import { z } from "zod";
import { MailingListName } from "../../config";

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

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

export { SubscriptionValidator, SubscriptionSchema };

0 comments on commit 0f2fed0

Please sign in to comment.