| 
									
										
										
										
											2018-02-23 12:20:13 +00:00
										 |  |  | import debug from 'debug'; | 
					
						
							|  |  |  | import { pickBy } from 'lodash'; | 
					
						
							|  |  |  | import { Observable } from 'rx'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  | import { cachedMap, getChallenge } from '../utils/map'; | 
					
						
							| 
									
										
										
										
											2018-02-23 12:20:13 +00:00
										 |  |  | import { shapeChallenges } from '../../common/app/redux/utils'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const log = debug('fcc:services:challenge'); | 
					
						
							|  |  |  | const isDev = debug.enabled('fcc:*'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default function getChallengesForBlock(app) { | 
					
						
							|  |  |  |   const challengeMap = cachedMap(app.models); | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2018-02-23 17:08:42 +00:00
										 |  |  |     name: 'challenge', | 
					
						
							| 
									
										
										
										
											2018-02-23 12:20:13 +00:00
										 |  |  |     read: function readChallengesForBlock( | 
					
						
							| 
									
										
										
										
											2018-02-23 17:08:42 +00:00
										 |  |  |         req, | 
					
						
							|  |  |  |         resource, | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  |         { dashedName, blockName} = {}, | 
					
						
							| 
									
										
										
										
											2018-02-23 17:08:42 +00:00
										 |  |  |         config, | 
					
						
							|  |  |  |         cb | 
					
						
							|  |  |  |       ) { | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  |       const getChallengeBlock$ = challengeMap | 
					
						
							| 
									
										
										
										
											2018-02-23 12:20:13 +00:00
										 |  |  |         .flatMap(({ | 
					
						
							|  |  |  |           result: { superBlocks }, | 
					
						
							|  |  |  |           entities: { | 
					
						
							|  |  |  |             block: fullBlockMap, | 
					
						
							|  |  |  |             challenge: challengeMap | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }) => { | 
					
						
							| 
									
										
										
										
											2018-02-23 17:08:42 +00:00
										 |  |  |           log(`sourcing challenges for the ${blockName} block`); | 
					
						
							| 
									
										
										
										
											2018-02-24 08:44:12 +00:00
										 |  |  |           const requestedChallenges = pickBy( | 
					
						
							|  |  |  |             challengeMap, | 
					
						
							|  |  |  |             ch => ch.block === blockName | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |           const entities = { | 
					
						
							|  |  |  |             block: { | 
					
						
							|  |  |  |               [blockName]: fullBlockMap[blockName] | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             challenge: requestedChallenges | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |           const { challenge, block } = shapeChallenges(entities, isDev); | 
					
						
							|  |  |  |           return Observable.of({ | 
					
						
							|  |  |  |             result: { superBlocks }, | 
					
						
							|  |  |  |             entities: { challenge, block } | 
					
						
							| 
									
										
										
										
											2018-02-23 12:20:13 +00:00
										 |  |  |           }); | 
					
						
							| 
									
										
										
										
											2018-02-24 08:44:12 +00:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-02-23 17:08:42 +00:00
										 |  |  |       return Observable.if( | 
					
						
							|  |  |  |         () => !!dashedName, | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  |         getChallenge(dashedName, blockName, challengeMap, 'en'), | 
					
						
							| 
									
										
										
										
											2018-02-23 17:08:42 +00:00
										 |  |  |         getChallengeBlock$ | 
					
						
							|  |  |  |       ) | 
					
						
							| 
									
										
										
										
											2018-02-23 12:20:13 +00:00
										 |  |  |         .subscribe( | 
					
						
							|  |  |  |           result => cb(null, result), | 
					
						
							|  |  |  |           cb | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |