Skip to content

Commit

Permalink
change from common js to modules and add initial seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenf committed Apr 12, 2024
1 parent 3fb3c3b commit 22d8473
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 41 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions curriculum-back/db/Curriculum.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const mongoose = require('mongoose')
const mongoosePaginate = require('mongoose-paginate-v2')
import mongoose from 'mongoose'
import mongoosePaginate from 'mongoose-paginate-v2'

const CurriculumSchema = new mongoose.Schema({
name: {
Expand Down
2 changes: 1 addition & 1 deletion curriculum-back/db/User.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mongoose = require('mongoose')
import mongoose from 'mongoose'

const UserSchema = new mongoose.Schema({
username: {
Expand Down
2 changes: 1 addition & 1 deletion curriculum-back/db/UserProfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mongoose = require('mongoose')
import mongoose from 'mongoose'

const UserProfileSchema = new mongoose.Schema({
userId: {
Expand Down
2 changes: 1 addition & 1 deletion curriculum-back/db/Verification.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mongoose = require('mongoose')
import mongoose from 'mongoose'

const VerificationSchema = new mongoose.Schema({
userId: {
Expand Down
2 changes: 1 addition & 1 deletion curriculum-back/db/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mongoose = require('mongoose')
import mongoose from 'mongoose'

if (process.env.NODE_ENV === 'production') {
mongoose.connect(`mongodb://mongo/curriculumapp`, {
Expand Down
45 changes: 45 additions & 0 deletions curriculum-back/db/seeder/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { faker } from 'https://esm.sh/@faker-js/faker'
import User from '../User'
import Curriculum from '../Curriculum'

// Generate seed data for users
const generateUsers = async (numUsers) => {
try {
for (let i = 0; i < numUsers; i++) {
const hashedPassword = await hashPassword(faker.internet.password())
const user = new User({
name: faker.name.findName(),
email: faker.internet.email(),
password: hashedPassword,
})
await user.save()
}
console.log(`${numUsers} users seeded successfully.`)
} catch (error) {
console.error('Error seeding users:', error)
}
}

// Generate seed data for curricula
// const generateCurricula = async (numCurricula) => {
// try {
// for (let i = 0; i < numCurricula; i++) {
// const curriculum = new Curriculum({
// title: faker.lorem.words(3),
// description: faker.lorem.sentence(),
// // Add any other curriculum properties you need
// })
// await curriculum.save()
// }
// console.log(`${numCurricula} curricula seeded successfully.`)
// } catch (error) {
// console.error('Error seeding curricula:', error)
// }
// }

// Call the functions to generate seed data
const numUsers = 10 // Number of users to generate
// const numCurricula = 5 // Number of curricula to generate

generateUsers(numUsers)
// generateCurricula(numCurricula)
23 changes: 23 additions & 0 deletions curriculum-back/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion curriculum-back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "curriculum-back",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "app.js",
"engines": {
"node": ">=10.0.0",
Expand Down Expand Up @@ -41,6 +42,7 @@
"nodemon": "2.0.2"
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"babel-eslint": "10.0.3",
"eslint": "6.8.0",
"husky": "4.3.8",
Expand All @@ -54,4 +56,4 @@
"pre-push": "npm run lint:fix"
}
}
}
}
15 changes: 6 additions & 9 deletions curriculum-back/server/api/auth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const express = require('express')
const mongoose = require('mongoose')
const bcrypt = require('bcrypt')
import express from 'express'
import mongoose from 'mongoose'
import bcrypt from 'bcrypt'

const {
auth: { hashPassword },
jwt: { generateToken },
mailgun: { sendEmail }
} = require('../utils')
import { hashPassword } from '../utils/auth'
import { generateToken } from '../utils/jwt'
import { sendEmail } from '../utils/mailgun'

mongoose.set('debug', true)

Expand Down Expand Up @@ -43,7 +41,6 @@ router.route('/login')
router.route('/register')
.post(async (req, res) => {
const { username, email, password } = req.body
console.log(req.body)

try {
if (password.length >= 8) {
Expand Down
2 changes: 1 addition & 1 deletion curriculum-back/server/api/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express')
const mongoose = require('mongoose')
import mongoose from 'mongoose'
mongoose.set('debug', true)

const { Curriculum } = require('@db')
Expand Down
6 changes: 3 additions & 3 deletions curriculum-back/server/api/curricula.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const express = require('express')
const mongoose = require('mongoose')
import express from 'express'
import mongoose from 'mongoose'
mongoose.set('debug', true)

const { Curriculum, User } = require('@db')
import { Curriculum, User } from '@db'
const {
jwt: { decodeToken }
} = require('../utils')
Expand Down
12 changes: 6 additions & 6 deletions curriculum-back/server/api/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const express = require('express')
import express from 'express'
const router = express.Router()

const authMiddleware = require('../middleware/auth')
import authMiddleware from '../middleware/auth'

const { curricula, authCurricula } = require('./curricula')
const count = require('./count')
const auth = require('./auth')
const users = require('./users')
import { curricula, authCurricula } from './curricula'
import count from './count'
import auth from './auth'
import users from './users'

router.use('/curricula', curricula)
router.use('/curricula', authMiddleware, authCurricula)
Expand Down
2 changes: 1 addition & 1 deletion curriculum-back/server/api/users.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO: make /users/:username or email api endpoints

const express = require('express')
const mongoose = require('mongoose')
import mongoose from 'mongoose'

const { User, Curriculum } = require('@db')
const bcrypt = require('bcrypt')
Expand Down
14 changes: 7 additions & 7 deletions curriculum-back/server/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const express = require('express')
const helmet = require('helmet')
const cors = require('cors')
const morgan = require('morgan')
const treblle = require('@treblle/express')
import express from 'express'
import helmet from 'helmet'
import cors from 'cors'
import morgan from 'morgan'
import treblle from '@treblle/express'

const routes = require('./api')
require('../db')
import routes from './api'
import '../db'

const app = express()
const port = 5050
Expand Down
2 changes: 1 addition & 1 deletion curriculum-back/server/utils/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const bcrypt = require('bcrypt')
import bcrypt from 'bcrypt'
const saltRounds = 10

async function hashPassword(password) {
Expand Down
12 changes: 8 additions & 4 deletions curriculum-back/server/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module.exports = {
auth: require('./auth'),
jwt: require('./jwt'),
mailgun: require('./mailgun')
import auth from './auth'
import jwt from './jwt'
import mailgun from './mailgun'

export default {
auth,
jwt,
mailgun
}
2 changes: 1 addition & 1 deletion curriculum-back/server/utils/jwt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var jwt = require('jsonwebtoken')
import jwt from 'jsonwebtoken'

const secret = 'gwenstacy'
const header = {
Expand Down
3 changes: 2 additions & 1 deletion curriculum-back/server/utils/mailgun.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
if (process.env.NODE_ENV !== 'local') {
var api_key = process.env.MAILGUN_KEY
var domain = 'mg.studytracker.tech'
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain})
// var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain})
var mailgun = { messages: () => ({ send: () => {} })
}

async function sendEmail(payload) {
Expand Down

0 comments on commit 22d8473

Please sign in to comment.