From e8b571a2a05dc199627b096902de3bae5e7cd7a4 Mon Sep 17 00:00:00 2001 From: Brok3Turtl3 Date: Tue, 26 Mar 2024 10:08:36 -0400 Subject: [PATCH 1/2] CHE-40 Created registrations model and associated type file --- server/models/graduateInvitationModel.ts | 40 ++++++++++++++++++++++++ server/types/graduateInvitation.ts | 12 +++++++ 2 files changed, 52 insertions(+) create mode 100644 server/models/graduateInvitationModel.ts create mode 100644 server/types/graduateInvitation.ts diff --git a/server/models/graduateInvitationModel.ts b/server/models/graduateInvitationModel.ts new file mode 100644 index 0000000..8dafe8f --- /dev/null +++ b/server/models/graduateInvitationModel.ts @@ -0,0 +1,40 @@ +import mongoose from "mongoose"; +import { IGraduateInvitation } from "../types/graduateInvitation"; + +const graduateInvitationSchema = new mongoose.Schema({ + email: { + type: String, + required: true, + unique: true, + }, + token: { + type: String, + required: true, + }, + tokenExpiry: { + type: Date, + required: true, + }, + isRegistered: { + type: Boolean, + required: true, + default: false, + }, + createdAt: { + type: Date, + default: Date.now, + }, + name: String, + registeredAt: Date, + lastEmailSent: { + type: Date, + default: Date.now, + }, +}); + +const GraduateInvitation = mongoose.model( + "GraduateInvitation", + graduateInvitationSchema +); + +export default GraduateInvitation; diff --git a/server/types/graduateInvitation.ts b/server/types/graduateInvitation.ts new file mode 100644 index 0000000..79c4020 --- /dev/null +++ b/server/types/graduateInvitation.ts @@ -0,0 +1,12 @@ +import { Document } from "mongoose"; + +export interface IGraduateInvitation extends Document { + email: string; + token: string; + tokenExpiry: Date; + isRegistered: boolean; + createdAt?: Date; + name?: string; + registeredAt?: Date; + lastEmailSent?: Date; +} From b5c00aeaee17d184148021e4d3a85ece39888d2b Mon Sep 17 00:00:00 2001 From: Brok3Turtl3 Date: Tue, 26 Mar 2024 14:19:36 -0400 Subject: [PATCH 2/2] CHE-40 Added devRoute and seeder function to test Model and seed Database from Postman in Dev mode --- dev-tools/scripts/alumniDatabaseSeeder.ts | 33 +++++++++++++++++++++++ server/controllers/devControllers.ts | 18 +++++++++++++ server/index.ts | 2 ++ server/routes/devRoutes.ts | 9 +++++++ 4 files changed, 62 insertions(+) create mode 100644 dev-tools/scripts/alumniDatabaseSeeder.ts create mode 100644 server/controllers/devControllers.ts create mode 100644 server/routes/devRoutes.ts diff --git a/dev-tools/scripts/alumniDatabaseSeeder.ts b/dev-tools/scripts/alumniDatabaseSeeder.ts new file mode 100644 index 0000000..b08f97b --- /dev/null +++ b/dev-tools/scripts/alumniDatabaseSeeder.ts @@ -0,0 +1,33 @@ +import mongoose from "mongoose"; +import GraduateInvitation from "../../server/models/graduateInvitationModel"; +import crypto from "crypto"; + +const alumniList = [ + { email: "J@email.com", name: "Jane Doe" }, + { email: "Jh@email.com", name: "John Doe" }, +]; + +const generateToken = () => { + return crypto.randomBytes(20).toString("hex"); +}; + +export const seedDatabase = async () => { + await GraduateInvitation.deleteMany(); + + const invitations = alumniList.map((alumnus) => ({ + email: alumnus.email, + token: generateToken(), + tokenExpiry: new Date(Date.now() + 48 * 60 * 60 * 1000), + isRegistered: false, + createdAt: new Date(), + name: alumnus.name, + lastEmailSent: new Date(), + })); + + try { + await GraduateInvitation.insertMany(invitations); + console.log("Database seeded successfully."); + } catch (error) { + console.error("Error seeding database:", error); + } +}; diff --git a/server/controllers/devControllers.ts b/server/controllers/devControllers.ts new file mode 100644 index 0000000..44411a4 --- /dev/null +++ b/server/controllers/devControllers.ts @@ -0,0 +1,18 @@ +import { Request, Response, NextFunction } from "express"; +import { seedDatabase } from "../../dev-tools/scripts/alumniDatabaseSeeder"; + +const seedRegistrationDatabase = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + await seedDatabase(); + res.status(200).send("Database seeded successfully."); + } catch (error) { + console.error("Error seeding database:", error); + res.status(500).send("Error seeding database."); + } +}; + +export { seedRegistrationDatabase }; diff --git a/server/index.ts b/server/index.ts index 7cff99b..a89bf0d 100644 --- a/server/index.ts +++ b/server/index.ts @@ -2,6 +2,7 @@ import path from "path"; import express, { Request, Response, Application, NextFunction } from "express"; import userRoutes from "./routes/userRoutes"; import profileRoutes from "./routes/profileRoutes"; +import devRoutes from "./routes/devRoutes"; import connectDB from "./config/db"; import dotenv from "dotenv"; import { notFound, errorHandler } from "./controllers/errorControllers"; @@ -16,6 +17,7 @@ connectDB(); app.use("/api/users", userRoutes); app.use("/api/profiles", profileRoutes); +app.use("/api/devRoutes", devRoutes); console.log(`ENV BEFORE CHECK: ${process.env.NODE_ENV}`); diff --git a/server/routes/devRoutes.ts b/server/routes/devRoutes.ts new file mode 100644 index 0000000..0741773 --- /dev/null +++ b/server/routes/devRoutes.ts @@ -0,0 +1,9 @@ +import express from "express"; + +import { seedRegistrationDatabase } from "../controllers/devControllers"; + +const router = express.Router(); + +router.get("/", seedRegistrationDatabase); + +export default router;