Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHE-40] Create User Registration #48

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 40 additions & 0 deletions server/models/graduateInvitationModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import mongoose from "mongoose";
import { IGraduateInvitation } from "../types/graduateInvitation";

const graduateInvitationSchema = new mongoose.Schema<IGraduateInvitation>({
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<IGraduateInvitation>(
"GraduateInvitation",
graduateInvitationSchema
);

export default GraduateInvitation;
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;
12 changes: 12 additions & 0 deletions server/types/graduateInvitation.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading