opt-cc/server/routes/request.js

181 lines
5.8 KiB
JavaScript
Raw Permalink Normal View History

2018-03-12 09:26:44 +01:00
'use strict';
2017-06-09 18:30:35 +02:00
// modules
const express = require('express');
// HTTP status codes by name
const codes = require('./http-codes');
const routerHandling = require('../middleware/router-handling');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkSql = require('../middleware/permission-check').checkSql;
const checkHl = require('../middleware/permission-check').checkHl;
2017-06-09 18:30:35 +02:00
// Mongoose Model using mongoDB
2017-06-10 13:16:15 +02:00
const UserModel = require('../models/user');
2017-06-09 18:30:35 +02:00
const AwardingModel = require('../models/awarding');
2017-06-10 13:16:15 +02:00
const PromotionModel = require('../models/promotion');
// result set for proposer(appUser) population
const resultSet = {
'__v': 0,
'updatedAt': 0,
'timestamp': 0,
'password': 0,
'permission': 0,
'secret': 0,
2018-03-12 09:26:44 +01:00
'activated': 0,
2017-06-10 13:16:15 +02:00
};
2017-06-09 18:30:35 +02:00
const request = new express.Router();
2017-06-09 18:30:35 +02:00
// routes **********************
request.route('/award')
2018-02-26 09:04:27 +01:00
.post(apiAuthenticationMiddleware, checkSql, (req, res, next) => {
const award = new AwardingModel(req.body);
award.confirmed = 0;
award.proposer = req.user._id;
// timestamp and default are set automatically by Mongoose Schema Validation
award.save((err) => {
if (err) {
err.status = codes.wrongrequest;
err.message += ' in fields: ' + Object.getOwnPropertyNames(err.errors);
return next(err);
}
res.status(codes.created);
res.locals.items = award;
next();
});
})
.all(
routerHandling.httpMethodNotAllowed
);
2017-06-09 18:30:35 +02:00
request.route('/promotion')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
2018-03-25 21:06:57 +02:00
// TODO: add SQL authentication?
2018-02-26 09:04:27 +01:00
const squadFilter = req.query.squadId;
const fractFilter = req.query.fractFilter;
const progressFilter = req.query.inProgress;
let filter;
if (squadFilter) {
filter = {squadId: squadFilter};
}
let userIds = [];
UserModel.find(filter).populate('squadId').exec((err, items) => {
if (err) {
err.status = codes.servererror;
return next(err);
}
for (let item of items) {
if (!fractFilter || (fractFilter && item.squadId && item.squadId.fraction === fractFilter)) {
userIds.push(item._id);
}
}
let promotionFilter = {
2018-03-12 09:26:44 +01:00
userId: {'$in': userIds},
2018-02-26 09:04:27 +01:00
};
if (progressFilter) {
promotionFilter.confirmed = 0;
}
PromotionModel.find(promotionFilter, {}, {sort: {timestamp: 'desc'}})
.populate('userId').populate('proposer', resultSet).exec((err, items) => {
if (err) {
err.status = codes.servererror;
return next(err);
}
if (items && items.length > 0) {
res.locals.items = items;
} else {
res.locals.items = [];
}
res.locals.processed = true;
next();
2018-03-12 09:26:44 +01:00
});
2018-02-26 09:04:27 +01:00
});
})
.post(apiAuthenticationMiddleware, checkSql, (req, res, next) => {
const promotion = new PromotionModel(req.body);
promotion.confirmed = 0;
promotion.proposer = req.user._id;
// timestamp and default are set automatically by Mongoose Schema Validation
promotion.save((err) => {
if (err) {
err.status = codes.wrongrequest;
err.message += ' in fields: ' + Object.getOwnPropertyNames(err.errors);
return next(err);
}
res.status(codes.created);
res.locals.items = promotion;
next();
});
})
.all(
routerHandling.httpMethodNotAllowed
);
2017-06-09 18:30:35 +02:00
request.route('/promotion/:id')
2018-02-26 09:04:27 +01:00
.patch(apiAuthenticationMiddleware, checkHl, (req, res, next) => {
if (!req.body || (req.body._id && req.body._id !== req.params.id)) {
// little bit different as in PUT. :id does not need to be in data, but if the _id and url id must match
const err = new Error('id of PATCH resource and send JSON body are not equal ' + req.params.id + ' ' +
req.body._id);
2018-02-26 09:04:27 +01:00
err.status = codes.notfound;
next(err);
return; // prevent node to process this function further after next() has finished.
}
req.body.updatedAt = new Date();
req.body.$inc = {__v: 1};
// PATCH is easier with mongoose than PUT. You simply update by all data that comes from outside. no need to
// reset attributes that are missing.
PromotionModel.findByIdAndUpdate(req.params.id, req.body, {new: true}, (err, item) => {
if (err) {
err.status = codes.wrongrequest;
2018-03-12 09:26:44 +01:00
} else if (!item) {
err = new Error('item not found');
2018-02-26 09:04:27 +01:00
err.status = codes.notfound;
2018-03-12 09:26:44 +01:00
} else {
2018-02-26 09:04:27 +01:00
if (item.confirmed === 1) {
let updateUser = {
_id: item.userId,
2018-03-12 09:26:44 +01:00
rankLvl: item.newRankLvl,
2018-02-26 09:04:27 +01:00
};
UserModel.findByIdAndUpdate(updateUser._id, updateUser, {new: true}, (err, item) => {
if (err) {
err.status = codes.wrongrequest;
2018-03-12 09:26:44 +01:00
} else if (!item) {
err = new Error('user not found');
2018-02-26 09:04:27 +01:00
err.status = codes.notfound;
}
});
}
res.locals.items = item;
}
next(err);
2018-03-12 09:26:44 +01:00
});
2018-02-26 09:04:27 +01:00
})
.all(
routerHandling.httpMethodNotAllowed
);
2017-06-09 18:30:35 +02:00
// this middleware function can be used, if you like or remove it
// it looks for object(s) in res.locals.items and if they exist, they are send to the client as json
request.use(routerHandling.emptyResponse);
module.exports = request;