Include unprocessed squad awardings into getAwarding and generify service call

pull/40/head
HardiReady 2018-06-24 14:06:27 +02:00
parent de22e744fa
commit 85850316a3
5 changed files with 71 additions and 86 deletions

View File

@ -1,4 +1,4 @@
### Get Awardings [GET /awardings{?userId,inProgress,fractFilter}]
### Get Awardings [GET /awardings{?userId,inProgress,fractFilter,squadId}]
List all awardings
@ -19,6 +19,9 @@ List all awardings
+ `OPFOR`
+ `GLOBAL`
+ squadId: `5aba54eaeadcce6332c6a774` (string, optional)
unique id of the squad
+ Response 200 (application/json; charset=utf-8)
@ -58,16 +61,3 @@ Create a new awarding proposal, that needs to be approved by higher permission l
+ Response 201 (application/json; charset=utf-8)
+ Attributes (Awarding, fixed-type)
### Get Unprocessed Squad Awardings [GET /awardings/unprocessed/{squadId}]
List all awardings that are requested and in pending decision status
**Permission: 1**
+ Parameters
+ squadId: `5aba54eaeadcce6332c6a774` (string, required) - unique id of the squad in which awardings are requested
+ Response 200 (application/json; charset=utf-8)
+ Attributes (array[AwardingPopulated], fixed-type)

View File

@ -12,7 +12,6 @@ const routerHandling = require('../middleware/router-handling');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl;
const checkSql = require('../middleware/permission-check').checkSql;
// Mongoose Model using mongoDB
const AwardingModel = require('../models/awarding');
@ -25,41 +24,52 @@ const resultSet = {
const awarding = new express.Router();
// routes **********************
awarding.route('/')
.get((req, res, next) => {
const listAwardsCall = (filter) =>
AwardingModel.find(filter, {}, {sort: {date: 'desc'}})
.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 {..}
}
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;
return next();
});
const filter = {};
if (req.query.userId) {
filter.userId = req.query.userId;
}
if (req.query.inProgress) {
if (req.query.inProgress === 'true') {
filter.confirmed = 0;
}
AwardingModel.find(filter, {}, {sort: {date: 'desc'}})
.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 {..}
}
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();
});
if (req.query.squadId) {
UserModel.find({squadId: req.query.squadId}, (err, users) => {
const squadUserIds = users.map((user) => new mongoose.Types.ObjectId(user._id));
filter.userId = {$in: squadUserIds};
return listAwardsCall(filter);
});
} else {
return listAwardsCall(filter);
}
})
.post(apiAuthenticationMiddleware, checkHl, (req, res, next) => {
@ -81,25 +91,6 @@ awarding.route('/')
.all(routerHandling.httpMethodNotAllowed);
awarding.route('/unprocessed/:squadId')
.get(apiAuthenticationMiddleware, checkSql, (req, res, next) => {
const filter = {squadId: req.params.squadId};
UserModel.find(filter, (err, users) => {
if (!users || users.length === 0) {
return next();
}
const squadUserIds = users.map((user) => new mongoose.Types.ObjectId(user._id));
AwardingModel.find({userId: {$in: squadUserIds}, confirmed: 0})
.populate('decorationId')
.populate('proposer', resultSet)
.populate('userId')
.exec((err, awards) => {
res.locals.items = awards;
next();
});
});
});
awarding.route('/:id')
.patch(apiAuthenticationMiddleware, checkHl, (req, res, next) => {
if (!req.body || (req.body._id && req.body._id !== req.params.id)) {

View File

@ -1,6 +1,6 @@
{
"name": "opt-cc",
"version": "1.7.7",
"version": "1.7.8",
"author": "Florian Hartwich <hardi@noarch.de>",
"private": true,
"scripts": {

View File

@ -5,7 +5,6 @@ export class AppConfig {
public readonly apiAppUserPath = this.apiUrl + '/account/';
public readonly apiAuthenticationPath = this.apiUrl + '/authenticate';
public readonly apiAwardPath = this.apiUrl + '/awardings';
public readonly apiAwardSquadPath = this.apiUrl + '/awardings/unprocessed';
public readonly apiCampaignPath = this.apiUrl + '/campaigns';
public readonly apiDecorationPath = this.apiUrl + '/decorations/';
public readonly apiLogsPath = this.apiUrl + '/logs';

View File

@ -12,30 +12,13 @@ export class AwardingService {
private config: AppConfig) {
}
getUnconfirmedAwards(fraction?: string) {
return this.http.get(this.config.apiAwardPath + '?inProgress=true&fractFilter=' + fraction)
.map(res => res.json());
}
checkUnprocessedAwards(fraction?: string) {
this.getUnconfirmedAwards(fraction).subscribe((items) => {
if (items.length > 0) {
this.hasUnprocessedAwards = true;
}
});
}
getUnprocessedSquadAwards(squadId?: string) {
return this.http.get(this.config.apiAwardSquadPath.concat('/').concat(squadId))
.map(res => res.json());
}
/**
* get awards array with populated decorations
*/
getUserAwardings(userId: string) {
return this.http.get(this.config.apiAwardPath + '?userId=' + userId)
.map(res => res.json());
getAwardings(fraction = '' , userId = '', inProgress = false, squadId = '') {
const getUrl = this.config.apiAwardPath
.concat('?inProgress=').concat(inProgress.toString())
.concat('&fractFilter=').concat(fraction)
.concat('&userId=').concat(userId)
.concat('&squadId=').concat(squadId);
return this.http.get(getUrl).map(res => res.json());
}
addAwarding(award: Award) {
@ -55,5 +38,27 @@ export class AwardingService {
return this.http.delete(this.config.apiAwardPath + '/' + awardingId);
}
getUnconfirmedAwards(fraction?: string) {
return this.getAwardings(fraction, '', true);
}
checkUnprocessedAwards(fraction?: string) {
this.getUnconfirmedAwards(fraction).subscribe((items) => {
if (items.length > 0) {
this.hasUnprocessedAwards = true;
}
});
}
getUnprocessedSquadAwards(squadId?: string) {
return this.getAwardings('', '', true, squadId);
}
/**
* get awards array with populated decorations
*/
getUserAwardings(userId: string) {
return this.getAwardings('', userId);
}
}