diff --git a/backend/web/package.json b/backend/web/package.json index e6177daf01..c5c4cb6e02 100644 --- a/backend/web/package.json +++ b/backend/web/package.json @@ -134,6 +134,5 @@ "systemjs-plugin-json": "^0.3.0", "tree-traversal": "^1.1.2" }, - "version": "0.0.0", - "packageManager": "yarn@3.6.4" + "version": "0.0.0" } diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index 510e0f2572..3b896e8d89 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -31,7 +31,7 @@ const Comment = require('../../models/Comment') const { parseAsync } = require('@babel/core') const Progress = require('../../models/Progress') const { BadRequestError } = require('../../utils/errors') -const { getTraineeResources } = require('./user') +const { getTraineeCurrentResources } = require('./user') const { isMine } = require('./message') const { DURATION_UNIT } = require('./consts') const { isLiked } = require('./post') @@ -110,7 +110,7 @@ declareEnumField({model: 'purchase', field: 'status', enumValues: PURCHASE_STATU const USER_MODELS=['user', 'loggedUser', 'contact'] USER_MODELS.forEach(model => { declareEnumField({model, field: 'role', instance: 'String', enumValues: ROLES}) - declareComputedField({model, field: 'resources', getterFn: getTraineeResources}) + declareComputedField({model, field: 'current_resources', getterFn: getTraineeCurrentResources}) declareVirtualField({model, field: 'fullname', instance: 'String', requires:'firstname,lastname'}) }) diff --git a/backend/web/server/plugins/aftral-lms/schemas/UserSchema.js b/backend/web/server/plugins/aftral-lms/schemas/UserSchema.js index b4a0f6a91d..306b9fbece 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/UserSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/UserSchema.js @@ -43,14 +43,11 @@ const UserSchema = new Schema({ ref: 'program', required: false, }, - resources: { - type: [{ - type: Schema.Types.ObjectId, - ref: 'resource', - }], - required: true, - default: [], - }, + // Trainee current resources (i.e. achievment status==BLOCK_STATUS_CURRENT) + current_resources: [{ + type: Schema.Types.ObjectId, + ref: 'resource', + }], }, schemaOptions) UserSchema.virtual('fullname', DUMMY_REF).get(function() { diff --git a/backend/web/server/plugins/aftral-lms/user.js b/backend/web/server/plugins/aftral-lms/user.js index 8fe04010da..3fbf80db83 100644 --- a/backend/web/server/plugins/aftral-lms/user.js +++ b/backend/web/server/plugins/aftral-lms/user.js @@ -1,42 +1,19 @@ +const Progress = require('../../models/Progress') +const Resource = require('../../models/Resource') const { loadFromDb } = require('../../utils/database') +const { BLOCK_STATUS_CURRENT } = require('./consts') -const getTraineeResources = async (userId, params, data) => { - let sessions = await loadFromDb({model: 'session', fields:[ - 'trainees', - 'children.children.children.children.spent_time_str', - 'children.children.children.children.name', - 'children.children.children.children.resource_type', - 'children.children.children.children.achievement_status', - 'children.children.children.children.children.spent_time_str', - 'children.children.children.children.children.name', - 'children.children.children.children.children.resource_type', - 'children.children.children.children.children.achievement_status', - ], user:data}) - - sessions = sessions.filter(s => s.trainees.some(t => t._id.toString() === data._id.toString())) - - const resources = sessions.flatMap(session => - session.children.flatMap(program => - program.children.flatMap(child => { - if (child.type === 'chapter') { - return child.children.flatMap(modulee => - modulee.children.flatMap(sequence => - sequence.children - ) - ) - } else { - return child.children.flatMap(sequence => - sequence.children - ) - } - }) - ) - ) - - return resources +const getTraineeCurrentResources = async (userId, params, data, fields) => { + // Find curent blocks for this user + let userCurrentBlockIds=await Progress.find({user: userId, achievement_status: BLOCK_STATUS_CURRENT}) + userCurrentBlockIds=userCurrentBlockIds.map(p => p.block._id) + let resources = await loadFromDb({ + model: 'resource', fields, params: {'filter._id': {$in: userCurrentBlockIds}} + }) + return resources.map(r => new Resource(r)) } module.exports = { - getTraineeResources + getTraineeCurrentResources,getTraineeCurrentResources } \ No newline at end of file diff --git a/backend/web/server/utils/database.js b/backend/web/server/utils/database.js index 0d3eceaba1..87bf878ee4 100644 --- a/backend/web/server/utils/database.js +++ b/backend/web/server/utils/database.js @@ -681,13 +681,13 @@ const getFieldsToCompute = ({model, fields}) => { } const addComputedFields = async ( - fields, + originalFields, userId, queryParams, data, model, ) => { - fields=getFieldsToCompute({model, fields}) + let fields=getFieldsToCompute({model, fields: originalFields}) if (lodash.isEmpty(fields)) { return data } @@ -719,13 +719,14 @@ const addComputedFields = async ( const requiredCompFields = lodash.pick(compFields, presentCompFields) return runPromisesWithDelay( - Object.keys(requiredCompFields).map(f => () => - requiredCompFields[f](newUserId, queryParams, data) + Object.keys(requiredCompFields).map(f => () => { + const displayFields=getRequiredSubFields(originalFields, f) + return requiredCompFields[f](newUserId, queryParams, data, displayFields) .then(res => { data[f] = res return data }) - ), + }), )}) .then(() => data) }) diff --git a/studio-test/package.json b/studio-test/package.json index 80611126a7..28c6a669e4 100644 --- a/studio-test/package.json +++ b/studio-test/package.json @@ -85,6 +85,5 @@ }, "devDependencies": { "@types/mime": "^3.0.1" - }, - "packageManager": "yarn@3.6.4" + } } diff --git a/studio/package.json b/studio/package.json index 6546a488d3..fa0dbad9ff 100644 --- a/studio/package.json +++ b/studio/package.json @@ -134,6 +134,5 @@ "eslint src/**/*.{ts,tsx} --fix", "git add" ] - }, - "packageManager": "yarn@3.6.4" + } }