feat(api): Add a challenge API, GET idToNameMap
This commit is contained in:
committed by
mrugesh mohapatra
parent
dace86663f
commit
491912448b
@ -1,12 +1,52 @@
|
|||||||
import { Observable } from 'rx';
|
import { Observable } from 'rx';
|
||||||
|
import { isEmpty } from 'lodash';
|
||||||
|
|
||||||
export default function(Challenge) {
|
export default function(Challenge) {
|
||||||
|
let challengeIdToNameMap;
|
||||||
|
|
||||||
Challenge.on('dataSourceAttached', () => {
|
Challenge.on('dataSourceAttached', () => {
|
||||||
Challenge.findOne$ =
|
Challenge.findOne$ = Observable.fromNodeCallback(
|
||||||
Observable.fromNodeCallback(Challenge.findOne, Challenge);
|
Challenge.findOne,
|
||||||
Challenge.findById$ =
|
Challenge
|
||||||
Observable.fromNodeCallback(Challenge.findById, Challenge);
|
);
|
||||||
Challenge.find$ =
|
Challenge.findById$ = Observable.fromNodeCallback(
|
||||||
Observable.fromNodeCallback(Challenge.find, Challenge);
|
Challenge.findById,
|
||||||
|
Challenge
|
||||||
|
);
|
||||||
|
Challenge.find$ = Observable.fromNodeCallback(Challenge.find, Challenge);
|
||||||
|
|
||||||
|
Challenge.find({ isPrivate: false }, (err, challenges) => {
|
||||||
|
if (err) {
|
||||||
|
throw Error(err);
|
||||||
|
}
|
||||||
|
challengeIdToNameMap = challenges.reduce((map, challenge) => {
|
||||||
|
const { id, title } = challenge;
|
||||||
|
map[id] = title;
|
||||||
|
return map;
|
||||||
|
}, {});
|
||||||
|
});
|
||||||
|
|
||||||
|
function getIdToNameMap(cb) {
|
||||||
|
if (isEmpty(challengeIdToNameMap)) {
|
||||||
|
// We are waiting for the find query to resolve
|
||||||
|
return setTimeout(() => getIdToNameMap(cb), 50);
|
||||||
|
}
|
||||||
|
return cb(null, challengeIdToNameMap);
|
||||||
|
}
|
||||||
|
Challenge.getIdToNameMap = getIdToNameMap;
|
||||||
|
});
|
||||||
|
|
||||||
|
Challenge.remoteMethod('getIdToNameMap', {
|
||||||
|
returns: [
|
||||||
|
{
|
||||||
|
arg: 'user',
|
||||||
|
type: 'object',
|
||||||
|
root: true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
http: {
|
||||||
|
path: '/get-id-to-name',
|
||||||
|
verb: 'GET'
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -133,6 +133,13 @@
|
|||||||
"principalType": "ROLE",
|
"principalType": "ROLE",
|
||||||
"principalId": "$everyone",
|
"principalId": "$everyone",
|
||||||
"permission": "ALLOW"
|
"permission": "ALLOW"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"accessType": "EXECUTE",
|
||||||
|
"principalType": "ROLE",
|
||||||
|
"principalId": "$everyone",
|
||||||
|
"permission": "ALLOW",
|
||||||
|
"property": "getIdToNameMap"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"methods": {}
|
"methods": {}
|
||||||
|
Reference in New Issue
Block a user