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 debug from 'debug';
// use old rxjs
import { Observable } from 'rx';
const publicUserProps = [
'id',
@ -35,21 +37,20 @@ const publicUserProps = [
'currentChallengeId',
'challengeMap'
];
const log = debug('fcc:services:user');
// const log = debug('fcc:services:user');
export default function userServices() {
return {
name: 'user',
read: (req, resource, params, config, cb) => {
let { user } = req;
if (user) {
log('user is signed in');
return user.getChallengeMap$()
const { user } = req;
Observable.if(
() => !user,
Observable.of({}),
user.getChallengeMap$()
.map(challengeMap => ({ ...user.toJSON(), challengeMap }))
.subscribe(
user => cb(
null,
{
.map(user => ({
entities: {
user: {
[user.username]: {
@ -60,16 +61,12 @@ export default function userServices() {
}
},
result: user.username
}
),
}))
)
.subscribe(
user => cb(null, user),
cb
);
}
debug('user is not signed in');
// Zalgo!!!
return process.nextTick(() => {
cb(null, {});
});
}
};
}