refactor(services/use): Simplify logic declaritively

This commit is contained in:
Berkeley Martinez
2018-01-12 11:25:48 -08:00
parent 307716f9c8
commit 0cd21f930f

View File

@ -1,5 +1,7 @@
import debug from 'debug';
import _ from 'lodash'; import _ from 'lodash';
// import debug from 'debug';
// use old rxjs
import { Observable } from 'rx';
const publicUserProps = [ const publicUserProps = [
'id', 'id',
@ -35,41 +37,36 @@ const publicUserProps = [
'currentChallengeId', 'currentChallengeId',
'challengeMap' 'challengeMap'
]; ];
const log = debug('fcc:services:user');
// const log = debug('fcc:services:user');
export default function userServices() { export default function userServices() {
return { return {
name: 'user', name: 'user',
read: (req, resource, params, config, cb) => { read: (req, resource, params, config, cb) => {
let { user } = req; const { user } = req;
if (user) { Observable.if(
log('user is signed in'); () => !user,
return user.getChallengeMap$() Observable.of({}),
user.getChallengeMap$()
.map(challengeMap => ({ ...user.toJSON(), challengeMap })) .map(challengeMap => ({ ...user.toJSON(), challengeMap }))
.subscribe( .map(user => ({
user => cb( entities: {
null, user: {
{ [user.username]: {
entities: { ..._.pick(user, publicUserProps),
user: { isTwitter: !!user.twitter,
[user.username]: { isLinkedIn: !!user.linkedIn
..._.pick(user, publicUserProps), }
isTwitter: !!user.twitter,
isLinkedIn: !!user.linkedIn
}
}
},
result: user.username
} }
), },
cb result: user.username
); }))
} )
debug('user is not signed in'); .subscribe(
// Zalgo!!! user => cb(null, user),
return process.nextTick(() => { cb
cb(null, {}); );
});
} }
}; };
} }