Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Added shift controller, route, and model #415

Open
wants to merge 1 commit into
base: staging
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
48 changes: 48 additions & 0 deletions server/controllers/shifts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {pick} from 'lodash'
import {ADMIN_ROLE} from '../../common/constants'
import {UnauthorizedError} from '../lib/errors'
import Shift from '../models/shift'

export default {
async create(req, res) {
let newShift = {
...(pick(req.body, ['status', 'start', 'end'])),
}
if (!req.user.roles.find(r => r === ADMIN_ROLE) &&
newDonation.donor !== req.user._id) {
throw new UnauthorizedError
}
let shift = await Shift.create(newShift)
res.json({
shift
})
},

async assign(req, res) {
const shift = await Shift.findByIdAndUpdate(
shift.params.shiftId,
{$set: {filled: true, volunteer: req.volunteerId}}
)
res.json(shift)
},

async query(req, res) {
const shifts = await Shifts.find()
res.json(shifts)
},

async delete(req, res) {
const id = res.shift._id
const shift = await Shift.findByIdAndRemove(id)
res.json(shift)
},

hasAuthorization(req, res, next) {
if (req.user.roles.find(r => r === ADMIN_ROLE) ||
req.params.donationId === req.user._id) {
return next()
}

throw new UnauthorizedError
}
}
31 changes: 31 additions & 0 deletions server/models/shift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mongoose from 'mongoose'
import autoIncrement from 'mongoose-plugin-autoinc'
import {modelTypes} from '../../common/constants'

const {Schema} = mongoose

const ShiftSchema = new Schema ({
filled: {
type: Boolean,
default: false,
},
volunteer: {
type: Number,
ref: modelTypes.VOLUNTEER
},
start: {
type: Date,
default: Date.now
},
end: {
type: Date,
default: Date.now
}
})

ShiftSchema.plugin(autoIncrement, {
model: modelTypes.SHIFT,
startAt: 1
})

export default mongoose.model(modelTypes.SHIFT, ShiftSchema)
24 changes: 24 additions & 0 deletions server/routes/shift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Router from 'express-promise-router'
import shiftsController from '../controllers/shift.js'
import usersController from '../controllers/users.js'

const {requiresLogin} = usersController

export default () => {
const shiftsRouter = Router({mergeParams: true})

shiftsRouter.route('/shifts')
.get(requiresLogin, shiftsController.query)

shiftsRouter.route('admin/shift/new')
.post(requiresLogin, shiftsController.hasAuthorization, shiftsController.create)

shiftsRouter.route('/admin/shifts/:shiftId')
.put(shiftsController.assign)
.delete(requiresLogin, shiftsController.hasAuthorization, shiftsController.delete)

shiftsRouter.route('/admin/shifts')
.get(shiftsController.query)

shiftsRouter.param('shiftId', shiftsController.shiftById)
}