feat: use static query to get idToNameMap (#36722)

This commit is contained in:
Valeriy
2019-08-30 20:07:28 +03:00
committed by mrugesh
parent f50ac6e115
commit 469c3f05c2
6 changed files with 46 additions and 145 deletions

View File

@ -1,56 +0,0 @@
import { Observable } from 'rx';
import { isEmpty } from 'lodash';
import { dasherize } from '../utils';
export default function(Challenge) {
let challengeIdToNameMap;
Challenge.on('dataSourceAttached', () => {
Challenge.findOne$ = Observable.fromNodeCallback(
Challenge.findOne,
Challenge
);
Challenge.findById$ = Observable.fromNodeCallback(
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, block, dashedName, title, superBlock } = challenge;
map[id] = {
challengeTitle: title,
challengePath: `${superBlock}/${dasherize(block)}/${dashedName}`
};
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'
}
});
}

View File

@ -133,13 +133,6 @@
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"property": "getIdToNameMap"
}
],
"methods": {}