fix(settings): fix-up user objects for solutions (#17556)

This commit is contained in:
Stuart Taylor
2018-06-12 16:50:35 +01:00
committed by mrugesh mohapatra
parent e10a9fcda0
commit f54b7c07f5
11 changed files with 200 additions and 107 deletions

View File

@@ -1,3 +1,10 @@
/**
*
* Any ref to fixCompletedChallengesItem should be removed post
* a db migration to fix all completedChallenges
*
*/
import _ from 'lodash';
import debug from 'debug';
import accepts from 'accepts';
@@ -8,17 +15,49 @@ import { getChallengeById, cachedMap } from '../utils/map';
import { dasherize } from '../utils';
import pathMigrations from '../resources/pathMigration.json';
import { fixCompletedChallengeItem } from '../../common/utils';
const log = debug('fcc:boot:challenges');
const learnURL = 'https://learn.freecodecamp.org';
const jsProjects = [
'aaa48de84e1ecc7c742e1124',
'a7f4d8f2483413a6ce226cac',
'56533eb9ac21ba0edf2244e2',
'aff0395860f5d3034dc0bfc9',
'aa2e6f85cab2ab736c9a9b24'
];
function buildUserUpdate(
user,
challengeId,
completedChallenge,
_completedChallenge,
timezone
) {
const { files } = _completedChallenge;
let completedChallenge = {};
if (jsProjects.includes(challengeId)) {
completedChallenge = {
..._completedChallenge,
files: Object.keys(files)
.map(key => files[key])
.map(file => _.pick(
file,
[
'contents',
'key',
'index',
'name',
'path',
'ext'
]
))
};
} else {
completedChallenge = _.omit(_completedChallenge, ['files']);
}
let finalChallenge;
const updateData = {};
const { timezone: userTimezone, completedChallenges = [] } = user;
@@ -46,7 +85,7 @@ function buildUserUpdate(
updateData.$set = {
completedChallenges: _.uniqBy(
[finalChallenge, ...completedChallenges],
[finalChallenge, ...completedChallenges.map(fixCompletedChallengeItem)],
'id'
)
};
@@ -167,6 +206,7 @@ export default function(app) {
const points = alreadyCompleted ? user.points : user.points + 1;
return user.update$(updateData)
.doOnNext(() => user.manualReload())
.doOnNext(({ count }) => log('%s documents updated', count))
.map(() => {
if (type === 'json') {
@@ -199,7 +239,7 @@ export default function(app) {
return req.user.getCompletedChallenges$()
.flatMap(() => {
const completedDate = Date.now();
const { id, solution, timezone } = req.body;
const { id, solution, timezone, files } = req.body;
const {
alreadyCompleted,
@@ -207,7 +247,7 @@ export default function(app) {
} = buildUserUpdate(
req.user,
id,
{ id, solution, completedDate },
{ id, solution, completedDate, files },
timezone
);
@@ -250,7 +290,7 @@ export default function(app) {
const completedChallenge = _.pick(
body,
[ 'id', 'solution', 'githubLink', 'challengeType' ]
[ 'id', 'solution', 'githubLink', 'challengeType', 'files' ]
);
completedChallenge.completedDate = Date.now();
@@ -278,6 +318,7 @@ export default function(app) {
} = buildUserUpdate(user, completedChallenge.id, completedChallenge);
return user.update$(updateData)
.doOnNext(() => user.manualReload())
.doOnNext(({ count }) => log('%s documents updated', count))
.doOnNext(() => {
if (type === 'json') {