-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHE-40 Added devRoute and seeder function to test Model and seed Data…
…base from Postman in Dev mode
- Loading branch information
1 parent
e8b571a
commit b5c00ae
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |