freeCodeCamp/api-server/server/utils/get-curriculum.js

31 lines
1.1 KiB
JavaScript

import { flatten } from 'lodash';
import { getChallengesForLang } from '../../../curriculum/getChallenges';
// TODO: this caching is handy if we want to field requests that need to 'query'
// the curriculum, but if we force the client to handle
// redirectToCurrentChallenge and, instead, only report the current challenge
// id via the user object, then we should *not* store this so it can be garbage
// collected.
let curriculum;
export async function getCurriculum() {
// NOTE: this is always 'english' because we are only interested in the slugs
// and those should not change between the languages.
curriculum = curriculum ? curriculum : getChallengesForLang('english');
return curriculum;
}
export async function getChallenges() {
return getCurriculum().then(curriculum => {
return Object.keys(curriculum)
.map(key => curriculum[key].blocks)
.reduce((challengeArray, superBlock) => {
const challengesForBlock = Object.keys(superBlock).map(
key => superBlock[key].challenges
);
return [...challengeArray, ...flatten(challengesForBlock)];
}, []);
});
}