From be60d16383b30e5e6b066371dd01eaf92ae38a31 Mon Sep 17 00:00:00 2001 From: zprindle Date: Mon, 22 Apr 2019 19:15:35 -0400 Subject: [PATCH] Added shift controller, route, and model --- server/controllers/shifts.js | 48 ++++++++++++++++++++++++++++++++++++ server/models/shift.js | 31 +++++++++++++++++++++++ server/routes/shift.js | 24 ++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 server/controllers/shifts.js create mode 100644 server/models/shift.js create mode 100644 server/routes/shift.js diff --git a/server/controllers/shifts.js b/server/controllers/shifts.js new file mode 100644 index 00000000..19e53324 --- /dev/null +++ b/server/controllers/shifts.js @@ -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 + } +} \ No newline at end of file diff --git a/server/models/shift.js b/server/models/shift.js new file mode 100644 index 00000000..64298791 --- /dev/null +++ b/server/models/shift.js @@ -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) \ No newline at end of file diff --git a/server/routes/shift.js b/server/routes/shift.js new file mode 100644 index 00000000..d5d73e4a --- /dev/null +++ b/server/routes/shift.js @@ -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) +} \ No newline at end of file