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,21 +37,20 @@ 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(
null,
{
entities: { entities: {
user: { user: {
[user.username]: { [user.username]: {
@ -60,16 +61,12 @@ export default function userServices() {
} }
}, },
result: user.username result: user.username
} }))
), )
.subscribe(
user => cb(null, user),
cb cb
); );
} }
debug('user is not signed in');
// Zalgo!!!
return process.nextTick(() => {
cb(null, {});
});
}
}; };
} }