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 2 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
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 app = express()
Expand All @@ -16,6 +17,7 @@ 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
57 changes: 57 additions & 0 deletions src/models/RSVPModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 }

Copy link
Member

Choose a reason for hiding this comment

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

Remove Blank Line

},
travelType: {
Copy link
Member

Choose a reason for hiding this comment

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

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 }

Copy link
Member

Choose a reason for hiding this comment

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

Remove additional blank line.

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

module.exports = RSVP
33 changes: 33 additions & 0 deletions src/routers/rsvp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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/', authMiddleware, async (req, res) => {
Copy link
Member

Choose a reason for hiding this comment

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

/rsvp/:id is more concise, Change it to that implementation. I remember talking about it doesnt make sense for us to pull a specific RSVP but just to be consistent with other routes/models, change the implementation to reflect that.

try {
const rsvp = await RSVP.findOne({ owner: req.user._id })
if (!rsvp) {
res.status(404).send({})
}
res.status(200).send(rsvp)
} catch (err) {
res.status(500).send({ errorMessage: err.message })
}
})

module.exports = router