Include unprocessed squad awardings into getAwarding and generify service call
parent
de22e744fa
commit
85850316a3
|
@ -1,4 +1,4 @@
|
||||||
### Get Awardings [GET /awardings{?userId,inProgress,fractFilter}]
|
### Get Awardings [GET /awardings{?userId,inProgress,fractFilter,squadId}]
|
||||||
|
|
||||||
List all awardings
|
List all awardings
|
||||||
|
|
||||||
|
@ -19,6 +19,9 @@ List all awardings
|
||||||
+ `OPFOR`
|
+ `OPFOR`
|
||||||
+ `GLOBAL`
|
+ `GLOBAL`
|
||||||
|
|
||||||
|
+ squadId: `5aba54eaeadcce6332c6a774` (string, optional)
|
||||||
|
unique id of the squad
|
||||||
|
|
||||||
|
|
||||||
+ Response 200 (application/json; charset=utf-8)
|
+ 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)
|
+ Response 201 (application/json; charset=utf-8)
|
||||||
|
|
||||||
+ Attributes (Awarding, fixed-type)
|
+ 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)
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ const routerHandling = require('../middleware/router-handling');
|
||||||
|
|
||||||
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
|
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
|
||||||
const checkHl = require('../middleware/permission-check').checkHl;
|
const checkHl = require('../middleware/permission-check').checkHl;
|
||||||
const checkSql = require('../middleware/permission-check').checkSql;
|
|
||||||
|
|
||||||
// Mongoose Model using mongoDB
|
// Mongoose Model using mongoDB
|
||||||
const AwardingModel = require('../models/awarding');
|
const AwardingModel = require('../models/awarding');
|
||||||
|
@ -25,41 +24,52 @@ const resultSet = {
|
||||||
|
|
||||||
const awarding = new express.Router();
|
const awarding = new express.Router();
|
||||||
|
|
||||||
|
|
||||||
// routes **********************
|
// routes **********************
|
||||||
awarding.route('/')
|
awarding.route('/')
|
||||||
.get((req, res, next) => {
|
.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 = {};
|
const filter = {};
|
||||||
if (req.query.userId) {
|
if (req.query.userId) {
|
||||||
filter.userId = req.query.userId;
|
filter.userId = req.query.userId;
|
||||||
}
|
}
|
||||||
if (req.query.inProgress) {
|
if (req.query.inProgress === 'true') {
|
||||||
filter.confirmed = 0;
|
filter.confirmed = 0;
|
||||||
}
|
}
|
||||||
AwardingModel.find(filter, {}, {sort: {date: 'desc'}})
|
if (req.query.squadId) {
|
||||||
.populate('decorationId').populate('proposer', resultSet).populate('userId')
|
UserModel.find({squadId: req.query.squadId}, (err, users) => {
|
||||||
.exec((err, items) => {
|
const squadUserIds = users.map((user) => new mongoose.Types.ObjectId(user._id));
|
||||||
if (err) {
|
filter.userId = {$in: squadUserIds};
|
||||||
err.status = codes.servererror;
|
return listAwardsCall(filter);
|
||||||
return next(err);
|
});
|
||||||
// with return before (or after) the next(err) we prevent that the code continues here
|
} else {
|
||||||
// after next(err) has finished. this saves an extra else {..}
|
return listAwardsCall(filter);
|
||||||
}
|
}
|
||||||
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();
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
.post(apiAuthenticationMiddleware, checkHl, (req, res, next) => {
|
.post(apiAuthenticationMiddleware, checkHl, (req, res, next) => {
|
||||||
|
@ -81,25 +91,6 @@ awarding.route('/')
|
||||||
|
|
||||||
.all(routerHandling.httpMethodNotAllowed);
|
.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')
|
awarding.route('/:id')
|
||||||
.patch(apiAuthenticationMiddleware, checkHl, (req, res, next) => {
|
.patch(apiAuthenticationMiddleware, checkHl, (req, res, next) => {
|
||||||
if (!req.body || (req.body._id && req.body._id !== req.params.id)) {
|
if (!req.body || (req.body._id && req.body._id !== req.params.id)) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "opt-cc",
|
"name": "opt-cc",
|
||||||
"version": "1.7.7",
|
"version": "1.7.8",
|
||||||
"author": "Florian Hartwich <hardi@noarch.de>",
|
"author": "Florian Hartwich <hardi@noarch.de>",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -5,7 +5,6 @@ export class AppConfig {
|
||||||
public readonly apiAppUserPath = this.apiUrl + '/account/';
|
public readonly apiAppUserPath = this.apiUrl + '/account/';
|
||||||
public readonly apiAuthenticationPath = this.apiUrl + '/authenticate';
|
public readonly apiAuthenticationPath = this.apiUrl + '/authenticate';
|
||||||
public readonly apiAwardPath = this.apiUrl + '/awardings';
|
public readonly apiAwardPath = this.apiUrl + '/awardings';
|
||||||
public readonly apiAwardSquadPath = this.apiUrl + '/awardings/unprocessed';
|
|
||||||
public readonly apiCampaignPath = this.apiUrl + '/campaigns';
|
public readonly apiCampaignPath = this.apiUrl + '/campaigns';
|
||||||
public readonly apiDecorationPath = this.apiUrl + '/decorations/';
|
public readonly apiDecorationPath = this.apiUrl + '/decorations/';
|
||||||
public readonly apiLogsPath = this.apiUrl + '/logs';
|
public readonly apiLogsPath = this.apiUrl + '/logs';
|
||||||
|
|
|
@ -12,30 +12,13 @@ export class AwardingService {
|
||||||
private config: AppConfig) {
|
private config: AppConfig) {
|
||||||
}
|
}
|
||||||
|
|
||||||
getUnconfirmedAwards(fraction?: string) {
|
getAwardings(fraction = '' , userId = '', inProgress = false, squadId = '') {
|
||||||
return this.http.get(this.config.apiAwardPath + '?inProgress=true&fractFilter=' + fraction)
|
const getUrl = this.config.apiAwardPath
|
||||||
.map(res => res.json());
|
.concat('?inProgress=').concat(inProgress.toString())
|
||||||
}
|
.concat('&fractFilter=').concat(fraction)
|
||||||
|
.concat('&userId=').concat(userId)
|
||||||
checkUnprocessedAwards(fraction?: string) {
|
.concat('&squadId=').concat(squadId);
|
||||||
this.getUnconfirmedAwards(fraction).subscribe((items) => {
|
return this.http.get(getUrl).map(res => res.json());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addAwarding(award: Award) {
|
addAwarding(award: Award) {
|
||||||
|
@ -55,5 +38,27 @@ export class AwardingService {
|
||||||
return this.http.delete(this.config.apiAwardPath + '/' + awardingId);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue