Skip to content

Commit

Permalink
CHE-40 Added devRoute and seeder function to test Model and seed Data…
Browse files Browse the repository at this point in the history
…base from Postman in Dev mode
  • Loading branch information
brok3turtl3 committed Mar 26, 2024
1 parent e8b571a commit b5c00ae
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
33 changes: 33 additions & 0 deletions dev-tools/scripts/alumniDatabaseSeeder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import mongoose from "mongoose";
import GraduateInvitation from "../../server/models/graduateInvitationModel";
import crypto from "crypto";

const alumniList = [
{ email: "[email protected]", name: "Jane Doe" },
{ email: "[email protected]", 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);
}
};
18 changes: 18 additions & 0 deletions server/controllers/devControllers.ts
Original file line number Diff line number Diff line change
@@ -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 };
2 changes: 2 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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}`);

Expand Down
9 changes: 9 additions & 0 deletions server/routes/devRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from "express";

import { seedRegistrationDatabase } from "../controllers/devControllers";

const router = express.Router();

router.get("/", seedRegistrationDatabase);

export default router;

0 comments on commit b5c00ae

Please sign in to comment.