From 720df45cfda918c5c5e8260dd97ae7017a1ecdf9 Mon Sep 17 00:00:00 2001 From: Florian Hartwich Date: Sat, 10 Jun 2017 22:07:32 +0200 Subject: [PATCH] Add hl confirmation menus --- api/cron-job/update-signatures.js | 15 ++- api/models/awarding.js | 8 +- api/routes/awardings.js | 49 +++++++- api/routes/command.js | 10 +- api/routes/request.js | 2 +- api/server.js | 2 +- api/signature-tool/signature-tool.js | 2 + static/src/app/app.component.html | 15 +++ static/src/app/app.config.ts | 2 +- static/src/app/app.routing.ts | 15 ++- .../src/app/army/army-member.component.html | 2 +- static/src/app/models/model-interfaces.ts | 2 +- .../request/award/req-award.component.html | 4 +- .../confirm-award/confirm-award.component.css | 43 +++++++ .../confirm-award.component.html | 49 ++++++++ .../confirm-award/confirm-award.component.ts | 42 +++++++ .../confirm-promotion.component.css | 43 +++++++ .../confirm-promotion.component.html | 105 ++++++++++++++++++ .../confirm-promotion.component.ts | 96 ++++++++++++++++ .../awarding-service/awarding.service.ts | 18 ++- 20 files changed, 496 insertions(+), 28 deletions(-) create mode 100644 static/src/app/request/confirm-award/confirm-award.component.css create mode 100644 static/src/app/request/confirm-award/confirm-award.component.html create mode 100644 static/src/app/request/confirm-award/confirm-award.component.ts create mode 100644 static/src/app/request/confirm-promotion/confirm-promotion.component.css create mode 100644 static/src/app/request/confirm-promotion/confirm-promotion.component.html create mode 100644 static/src/app/request/confirm-promotion/confirm-promotion.component.ts diff --git a/api/cron-job/update-signatures.js b/api/cron-job/update-signatures.js index 628b1fd..82f202d 100644 --- a/api/cron-job/update-signatures.js +++ b/api/cron-job/update-signatures.js @@ -5,10 +5,7 @@ const async = require('async'); const UserModel = require('../models/user'); const signatureTool = require('../signature-tool/signature-tool'); - -// Execute daily @ 02:30 AM -const cronJob = cron.job('00 30 02 * * *', () => { - +const createAllSignatures = () => { console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString() + ': cron job started - UPDATE SIGNATURES'); @@ -42,6 +39,12 @@ const cronJob = cron.job('00 30 02 * * *', () => { }) }); -}); +}; -module.exports = cronJob; +// Execute daily @ 02:30 AM +const cronJob = cron.job('00 30 02 * * *', createAllSignatures); + +module.exports = { + cronJob: cronJob, + createAllSignatures: createAllSignatures +}; diff --git a/api/models/awarding.js b/api/models/awarding.js index d653369..bd2b60d 100644 --- a/api/models/awarding.js +++ b/api/models/awarding.js @@ -22,8 +22,12 @@ const AwardingSchema = new Schema({ required: true }, confirmed: { - type: Boolean, - default: true + type: Number, + get: v => Math.round(v), + set: v => Math.round(v), + min: 0, + max: 2, + default: 0 }, date: { type: Date, diff --git a/api/routes/awardings.js b/api/routes/awardings.js index 96beea3..8658e68 100644 --- a/api/routes/awardings.js +++ b/api/routes/awardings.js @@ -36,6 +36,9 @@ awarding.route('/') if (req.query.userId) { filter.userId = req.query.userId; } + if (req.query.inProgress) { + filter.confirmed = 0; + } if (req.query.simple) { AwardingModel.find(filter, {}, {sort: {date: 'desc'}}, (err, items) => { if (err) { @@ -53,17 +56,26 @@ awarding.route('/') }); } else { AwardingModel.find(filter, {}, {sort: {date: 'desc'}}) - .populate('decorationId').populate('proposer', resultSet).exec((err, items) => { + .populate('decorationId').populate('proposer', resultSet).populate('userId').exec((err, items) => { if (err) { err.status = codes.servererror; return next(err); // with return before (or after) the next(err) we prevent that the code continues here after next(err) has finished. // this saves an extra else {..} } - // if the collection is empty we do not send empty arrays back. - if (items && items.length > 0) { + let results = []; + if (req.query.fractFilter) { + for (let item of items) { + if (item.decorationId.fraction === req.query.fractFilter) { + results.push(item) + } + } + res.locals.items = results; + + } else { res.locals.items = items; } + res.locals.processed = true; next(); }); @@ -72,6 +84,7 @@ awarding.route('/') .post(apiAuthenticationMiddleware, checkHl, (req, res, next) => { const award = new AwardingModel(req.body); + award.confirmed = 1; award.proposer = req.user._id; // timestamp and default are set automatically by Mongoose Schema Validation award.save((err) => { @@ -91,6 +104,36 @@ awarding.route('/') ); awarding.route('/:id') + + .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); + err.status = codes.notfound; + next(err); + return; // prevent node to process this function further after next() has finished. + } + + // optional task 3: increment version manually as we do not use .save(.) + 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. + AwardingModel.findByIdAndUpdate(req.params.id, req.body, {new: true}, (err, item) => { + if (err) { + err.status = codes.wrongrequest; + } + else if (!item) { + err = new Error("item not found"); + err.status = codes.notfound; + } + else { + res.locals.items = item; + } + next(err); + }) + }) + .delete(apiAuthenticationMiddleware, checkHl, (req, res, next) => { AwardingModel.findByIdAndRemove(req.params.id, (err, item) => { if (err) { diff --git a/api/routes/command.js b/api/routes/command.js index fc6f91e..786e12f 100644 --- a/api/routes/command.js +++ b/api/routes/command.js @@ -8,11 +8,19 @@ const logger = require('debug')('cc:command'); const codes = require('./http-codes'); const routerHandling = require('../middleware/router-handling'); +const createAllSignatures = require('../cron-job/update-signatures').createAllSignatures; const createSignature = require('../signature-tool/signature-tool'); const command = express.Router(); -// add middleware for bonus tasks 4 and 5 to find filter and offset/limit params for GET / and GET /:id +command.route('/createSignature') + .post((req, res, next) => { + createAllSignatures(); + }) + + .all( + routerHandling.httpMethodNotAllowed + ); command.route('/createSignature/:id') .post((req, res, next) => { diff --git a/api/routes/request.js b/api/routes/request.js index e107734..e11d641 100644 --- a/api/routes/request.js +++ b/api/routes/request.js @@ -33,7 +33,7 @@ request.route('/award') .post((req, res, next) => { const award = new AwardingModel(req.body); - award.confirmed = false; + award.confirmed = 0; award.proposer = req.user._id; // timestamp and default are set automatically by Mongoose Schema Validation award.save((err) => { diff --git a/api/server.js b/api/server.js index 51a3b16..c7b375f 100644 --- a/api/server.js +++ b/api/server.js @@ -17,7 +17,7 @@ const errorResponseWare = require('./middleware/error-response'); const apiAuthenticationMiddleware = require('./middleware/auth-middleware'); const checkSql = require('./middleware/permission-check').checkSql; const checkAdmin = require('./middleware/permission-check').checkAdmin; -const signatureCronJob = require('./cron-job/update-signatures'); +const signatureCronJob = require('./cron-job/update-signatures').cronJob; // router modules const authenticateRouter = require('./routes/authenticate'); diff --git a/api/signature-tool/signature-tool.js b/api/signature-tool/signature-tool.js index 3d0c63f..b965556 100644 --- a/api/signature-tool/signature-tool.js +++ b/api/signature-tool/signature-tool.js @@ -209,6 +209,8 @@ let saveJimpImageAndCompress = (image, userId, res, next) => { }).then((files) => { res.locals.items = {status: 'success'}; return next(); + }).catch((error) => { + console.log(error) }) }, 3000); }; diff --git a/static/src/app/app.component.html b/static/src/app/app.component.html index a0e53b5..bcf3a01 100644 --- a/static/src/app/app.component.html +++ b/static/src/app/app.component.html @@ -48,6 +48,21 @@ +