fix: Centralise user deserialization
This commit is contained in:
committed by
mrugesh mohapatra
parent
0c23844793
commit
72a0d63aa0
@ -1,10 +1,13 @@
|
||||
import loopback from 'loopback';
|
||||
import compose from 'lodash/fp/compose';
|
||||
import map from 'lodash/fp/map';
|
||||
import sortBy from 'lodash/fp/sortBy';
|
||||
import trans from 'lodash/fp/transform';
|
||||
import last from 'lodash/fp/last';
|
||||
import forEachRight from 'lodash/fp/forEachRight';
|
||||
import { isEmpty } from 'lodash';
|
||||
import moment from 'moment-timezone';
|
||||
|
||||
import { dayCount } from '../utils/date-utils';
|
||||
|
||||
const transform = trans.convert({ cap: false });
|
||||
@ -100,3 +103,54 @@ export function calcLongestStreak(cals, tz = 'UTC') {
|
||||
|
||||
return dayCount(longest, tz);
|
||||
}
|
||||
|
||||
export function getUserById(id, User = loopback.getModelByType('User')) {
|
||||
return new Promise((resolve, reject) =>
|
||||
User.findById(id, (err, instance) => {
|
||||
if (err || isEmpty(instance)) {
|
||||
return reject(err || 'No user instance found');
|
||||
}
|
||||
instance.points =
|
||||
(instance.progressTimestamps && instance.progressTimestamps.length) ||
|
||||
1;
|
||||
let completedChallengeCount = 0;
|
||||
let completedProjectCount = 0;
|
||||
if ('completedChallenges' in instance) {
|
||||
completedChallengeCount = instance.completedChallenges.length;
|
||||
instance.completedChallenges.forEach(item => {
|
||||
if (
|
||||
'challengeType' in item &&
|
||||
(item.challengeType === 3 || item.challengeType === 4)
|
||||
) {
|
||||
completedProjectCount++;
|
||||
}
|
||||
});
|
||||
}
|
||||
instance.completedChallengeCount = completedChallengeCount;
|
||||
instance.completedProjectCount = completedProjectCount;
|
||||
instance.completedCertCount = getCompletedCertCount(instance);
|
||||
instance.completedLegacyCertCount = getLegacyCertCount(instance);
|
||||
instance.completedChallenges = [];
|
||||
delete instance.progressTimestamps;
|
||||
return resolve(instance);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function getCompletedCertCount(user) {
|
||||
return [
|
||||
'isApisMicroservicesCert',
|
||||
'is2018DataVisCert',
|
||||
'isFrontEndLibsCert',
|
||||
'isInfosecQaCert',
|
||||
'isJsAlgoDataStructCert',
|
||||
'isRespWebDesignCert'
|
||||
].reduce((sum, key) => (user[key] ? sum + 1 : sum), 0);
|
||||
}
|
||||
|
||||
function getLegacyCertCount(user) {
|
||||
return ['isFrontEndCert', 'isBackEndCert', 'isDataVisCert'].reduce(
|
||||
(sum, key) => (user[key] ? sum + 1 : sum),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
@ -5,8 +5,10 @@ import sinon from 'sinon';
|
||||
import {
|
||||
prepUniqueDaysByHours,
|
||||
calcCurrentStreak,
|
||||
calcLongestStreak
|
||||
calcLongestStreak,
|
||||
getUserById
|
||||
} from './user-stats';
|
||||
import { mockUserID, mockApp, mockUser } from '../boot_tests/fixtures';
|
||||
|
||||
// setting now to 2016-02-03T11:00:00 (PST)
|
||||
const clock = sinon.useFakeTimers(1454526000000);
|
||||
@ -577,4 +579,51 @@ describe('user stats', () => {
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
describe('getUserById', () => {
|
||||
const stubUser = {
|
||||
findById(id, cb) {
|
||||
cb(null, { id: 123 });
|
||||
}
|
||||
};
|
||||
it('returns a promise', () => {
|
||||
expect.assertions(3);
|
||||
expect(typeof getUserById('123', stubUser).then).toEqual('function');
|
||||
expect(typeof getUserById('123', stubUser).catch).toEqual('function');
|
||||
expect(typeof getUserById('123', stubUser).finally).toEqual('function');
|
||||
});
|
||||
|
||||
it('resolves a user for a given id', done => {
|
||||
expect.assertions(7);
|
||||
return getUserById(mockUserID, mockApp.models.User)
|
||||
.then(user => {
|
||||
expect(user).toEqual(mockUser);
|
||||
|
||||
// fields added or removed by getUserById
|
||||
expect(user).not.toHaveProperty('progressTimestamps');
|
||||
expect(user).toHaveProperty('completedChallengeCount');
|
||||
expect(user).toHaveProperty('completedProjectCount');
|
||||
expect(user).toHaveProperty('completedCertCount');
|
||||
expect(user).toHaveProperty('completedLegacyCertCount');
|
||||
expect(user.completedChallenges).toEqual([]);
|
||||
})
|
||||
.then(done)
|
||||
.catch(done);
|
||||
});
|
||||
|
||||
it('throws when no user is found', done => {
|
||||
const noUserError = new Error('No user found');
|
||||
const throwyUserModel = {
|
||||
findById(_, cb) {
|
||||
cb(noUserError);
|
||||
}
|
||||
};
|
||||
expect(
|
||||
getUserById('not-a-real-id', throwyUserModel).catch(error => {
|
||||
expect(error).toEqual(noUserError);
|
||||
done();
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user