fix: reduce the number of db calls for getSessionUser (#37385)
* Fix: Reduce the number of db calls for getSessionUser * Fix unit tests
This commit is contained in:
@ -983,6 +983,12 @@ export default function(User) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
User.prototype.getPoints$ = function getPoints$() {
|
User.prototype.getPoints$ = function getPoints$() {
|
||||||
|
if (
|
||||||
|
Array.isArray(this.progressTimestamps) &&
|
||||||
|
this.progressTimestamps.length
|
||||||
|
) {
|
||||||
|
return Observable.of(this.progressTimestamps);
|
||||||
|
}
|
||||||
const id = this.getId();
|
const id = this.getId();
|
||||||
const filter = {
|
const filter = {
|
||||||
where: { id },
|
where: { id },
|
||||||
@ -994,6 +1000,12 @@ export default function(User) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
User.prototype.getCompletedChallenges$ = function getCompletedChallenges$() {
|
User.prototype.getCompletedChallenges$ = function getCompletedChallenges$() {
|
||||||
|
if (
|
||||||
|
Array.isArray(this.completedChallenges) &&
|
||||||
|
this.completedChallenges.length
|
||||||
|
) {
|
||||||
|
return Observable.of(this.completedChallenges);
|
||||||
|
}
|
||||||
const id = this.getId();
|
const id = this.getId();
|
||||||
const filter = {
|
const filter = {
|
||||||
where: { id },
|
where: { id },
|
||||||
|
@ -28,7 +28,7 @@ import {
|
|||||||
} from './fixtures';
|
} from './fixtures';
|
||||||
|
|
||||||
describe('boot/challenge', () => {
|
describe('boot/challenge', () => {
|
||||||
xdescribe('backendChallengeCompleted');
|
xdescribe('backendChallengeCompleted', () => {});
|
||||||
|
|
||||||
describe('buildUserUpdate', () => {
|
describe('buildUserUpdate', () => {
|
||||||
it('returns an Object with a nested "completedChallenges" property', () => {
|
it('returns an Object with a nested "completedChallenges" property', () => {
|
||||||
@ -332,9 +332,9 @@ describe('boot/challenge', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
xdescribe('modernChallengeCompleted');
|
xdescribe('modernChallengeCompleted', () => {});
|
||||||
|
|
||||||
xdescribe('projectCompleted');
|
xdescribe('projectCompleted', () => {});
|
||||||
|
|
||||||
describe('redirectToCurrentChallenge', () => {
|
describe('redirectToCurrentChallenge', () => {
|
||||||
const mockHomeLocation = 'https://www.example.com';
|
const mockHomeLocation = 'https://www.example.com';
|
||||||
|
@ -110,9 +110,7 @@ export function getUserById(id, User = loopback.getModelByType('User')) {
|
|||||||
if (err || isEmpty(instance)) {
|
if (err || isEmpty(instance)) {
|
||||||
return reject(err || 'No user instance found');
|
return reject(err || 'No user instance found');
|
||||||
}
|
}
|
||||||
instance.points =
|
|
||||||
(instance.progressTimestamps && instance.progressTimestamps.length) ||
|
|
||||||
1;
|
|
||||||
let completedChallengeCount = 0;
|
let completedChallengeCount = 0;
|
||||||
let completedProjectCount = 0;
|
let completedProjectCount = 0;
|
||||||
if ('completedChallenges' in instance) {
|
if ('completedChallenges' in instance) {
|
||||||
@ -126,12 +124,14 @@ export function getUserById(id, User = loopback.getModelByType('User')) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.completedChallengeCount = completedChallengeCount;
|
instance.completedChallengeCount = completedChallengeCount;
|
||||||
instance.completedProjectCount = completedProjectCount;
|
instance.completedProjectCount = completedProjectCount;
|
||||||
instance.completedCertCount = getCompletedCertCount(instance);
|
instance.completedCertCount = getCompletedCertCount(instance);
|
||||||
instance.completedLegacyCertCount = getLegacyCertCount(instance);
|
instance.completedLegacyCertCount = getLegacyCertCount(instance);
|
||||||
instance.completedChallenges = [];
|
instance.points =
|
||||||
delete instance.progressTimestamps;
|
(instance.progressTimestamps && instance.progressTimestamps.length) ||
|
||||||
|
1;
|
||||||
return resolve(instance);
|
return resolve(instance);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
@ -599,13 +599,12 @@ describe('user stats', () => {
|
|||||||
.then(user => {
|
.then(user => {
|
||||||
expect(user).toEqual(mockUser);
|
expect(user).toEqual(mockUser);
|
||||||
|
|
||||||
// fields added or removed by getUserById
|
expect(user).toHaveProperty('progressTimestamps');
|
||||||
expect(user).not.toHaveProperty('progressTimestamps');
|
|
||||||
expect(user).toHaveProperty('completedChallengeCount');
|
expect(user).toHaveProperty('completedChallengeCount');
|
||||||
expect(user).toHaveProperty('completedProjectCount');
|
expect(user).toHaveProperty('completedProjectCount');
|
||||||
expect(user).toHaveProperty('completedCertCount');
|
expect(user).toHaveProperty('completedCertCount');
|
||||||
expect(user).toHaveProperty('completedLegacyCertCount');
|
expect(user).toHaveProperty('completedLegacyCertCount');
|
||||||
expect(user.completedChallenges).toEqual([]);
|
expect(user).toHaveProperty('completedChallenges');
|
||||||
})
|
})
|
||||||
.then(done)
|
.then(done)
|
||||||
.catch(done);
|
.catch(done);
|
||||||
|
Reference in New Issue
Block a user