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

feat(index.js, RSVPModel.js, rsvp.js): add rsvp model and routes #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
const express = require('express')
require('./db/mongoose')
const userRouter = require('./routers/user')
const applicationRouter = require('./routers/application')
const emailRequestRouter = require('./routers/emailRequest')
const resumeRouter = require('./routers/resume')
const rsvpRouter = require('./routers/rsvp')
const forgotPasswordRequestRouter = require('./routers/forgotPasswordRequest')
const morgan = require('morgan')
const port = process.env.PORT
const app = require('./app')

const port = process.env.PORT
app.use(express.json())
app.use(morgan('combined'))
app.use(userRouter)
app.use(applicationRouter)
app.use(emailRequestRouter)
app.use(forgotPasswordRequestRouter)
app.use(resumeRouter)
app.use(rsvpRouter)

app.listen(port, () => {
console.log('Server is up on port ' + port)
Expand Down
3 changes: 3 additions & 0 deletions src/middleware/authMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const authMiddleware = async (req, res, next) => {
if (!user || !user.emailVerified) {
throw new Error('Unable to authenticate user')
}
if (user.email.split('@')[1] === 'slohacks.com') {
req.admin = true
}

req.token = token
req.user = user
Expand Down
55 changes: 55 additions & 0 deletions src/models/RSVPModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const mongoose = require('mongoose')

const RSVP = mongoose.model('RSVP', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about how we should expire out RSVP, ex if user is accepted but RSVPS after 7 days how do we prevent them from creating a document

owner: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
attending: {
type: Boolean,
required: true
},
shirt: {
type: Number,
min: 0,
max: 4,
required: function () { return this.attending }
},
transportation: {
type: Number,
min: 0,
max: 2,
required: function () { return this.attending }
},
bus: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be "buses"

type: Number,
min: 0,
max: 2,
required: function () { return this.attending && this.travelType === 0 }
},
socal: {
type: Number,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the frontend sends this as a string. thoughts on number vs string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tommy and I think it is easier to store this data as a string as well, I am updating the Application model to reflect this.

min: 0,
max: 2,
required: function () { return this.attending && this.bus === 0 }
},
norcal: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

type: Number,
min: 0,
max: 2,
required: function () { return this.attending && this.bus === 1 }
},
flight: {
type: String,
required: function () { return this.attending && this.travelType === 1 }
},
misc: {
type: String
},
submittedAt: {
type: Date,
default: Date.now
}
})

module.exports = RSVP
38 changes: 38 additions & 0 deletions src/routers/rsvp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const express = require('express')
const RSVP = require('../models/RSVPModel')
const router = new express.Router()
const authMiddleware = require('../middleware/authMiddleware')

router.post('/rsvp', authMiddleware, async (req, res) => {
try {
const rsvp = new RSVP(req.body)
rsvp.owner = req.user._id
const existingRSVP = await RSVP.findOne({ owner: req.user._id })
if (existingRSVP) {
throw new Error('This user has already RSVP\'d')
}
await rsvp.save()
res.status(201).send(rsvp)
} catch (err) {
res.status(400).send({ errorMessage: err.message })
}
})

router.get('/rsvp/:id', authMiddleware, async (req, res) => {
try {
const { id: userID } = req.params
const rsvp = await RSVP.findOne({ owner: userID })
if ((!req.admin && userID !== req.user._id.toString())) {
throw new Error()
}
if (!rsvp) {
res.status(404).send({})
} else {
res.status(200).send(rsvp)
}
} catch (err) {
res.status(500).send({ errorMessage: err.message })
}
})

module.exports = router