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$() {
 | 
			
		||||
    if (
 | 
			
		||||
      Array.isArray(this.progressTimestamps) &&
 | 
			
		||||
      this.progressTimestamps.length
 | 
			
		||||
    ) {
 | 
			
		||||
      return Observable.of(this.progressTimestamps);
 | 
			
		||||
    }
 | 
			
		||||
    const id = this.getId();
 | 
			
		||||
    const filter = {
 | 
			
		||||
      where: { id },
 | 
			
		||||
@@ -994,6 +1000,12 @@ export default function(User) {
 | 
			
		||||
    });
 | 
			
		||||
  };
 | 
			
		||||
  User.prototype.getCompletedChallenges$ = function getCompletedChallenges$() {
 | 
			
		||||
    if (
 | 
			
		||||
      Array.isArray(this.completedChallenges) &&
 | 
			
		||||
      this.completedChallenges.length
 | 
			
		||||
    ) {
 | 
			
		||||
      return Observable.of(this.completedChallenges);
 | 
			
		||||
    }
 | 
			
		||||
    const id = this.getId();
 | 
			
		||||
    const filter = {
 | 
			
		||||
      where: { id },
 | 
			
		||||
 
 | 
			
		||||
@@ -28,7 +28,7 @@ import {
 | 
			
		||||
} from './fixtures';
 | 
			
		||||
 | 
			
		||||
describe('boot/challenge', () => {
 | 
			
		||||
  xdescribe('backendChallengeCompleted');
 | 
			
		||||
  xdescribe('backendChallengeCompleted', () => {});
 | 
			
		||||
 | 
			
		||||
  describe('buildUserUpdate', () => {
 | 
			
		||||
    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', () => {
 | 
			
		||||
    const mockHomeLocation = 'https://www.example.com';
 | 
			
		||||
 
 | 
			
		||||
@@ -110,9 +110,7 @@ export function getUserById(id, User = loopback.getModelByType('User')) {
 | 
			
		||||
      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) {
 | 
			
		||||
@@ -126,12 +124,14 @@ export function getUserById(id, User = loopback.getModelByType('User')) {
 | 
			
		||||
          }
 | 
			
		||||
        });
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      instance.completedChallengeCount = completedChallengeCount;
 | 
			
		||||
      instance.completedProjectCount = completedProjectCount;
 | 
			
		||||
      instance.completedCertCount = getCompletedCertCount(instance);
 | 
			
		||||
      instance.completedLegacyCertCount = getLegacyCertCount(instance);
 | 
			
		||||
      instance.completedChallenges = [];
 | 
			
		||||
      delete instance.progressTimestamps;
 | 
			
		||||
      instance.points =
 | 
			
		||||
        (instance.progressTimestamps && instance.progressTimestamps.length) ||
 | 
			
		||||
        1;
 | 
			
		||||
      return resolve(instance);
 | 
			
		||||
    })
 | 
			
		||||
  );
 | 
			
		||||
 
 | 
			
		||||
@@ -599,13 +599,12 @@ describe('user stats', () => {
 | 
			
		||||
        .then(user => {
 | 
			
		||||
          expect(user).toEqual(mockUser);
 | 
			
		||||
 | 
			
		||||
          // fields added or removed by getUserById
 | 
			
		||||
          expect(user).not.toHaveProperty('progressTimestamps');
 | 
			
		||||
          expect(user).toHaveProperty('progressTimestamps');
 | 
			
		||||
          expect(user).toHaveProperty('completedChallengeCount');
 | 
			
		||||
          expect(user).toHaveProperty('completedProjectCount');
 | 
			
		||||
          expect(user).toHaveProperty('completedCertCount');
 | 
			
		||||
          expect(user).toHaveProperty('completedLegacyCertCount');
 | 
			
		||||
          expect(user.completedChallenges).toEqual([]);
 | 
			
		||||
          expect(user).toHaveProperty('completedChallenges');
 | 
			
		||||
        })
 | 
			
		||||
        .then(done)
 | 
			
		||||
        .catch(done);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user