From 2f2a00bd0c25cac12d08e4f5b66272574cf378e3 Mon Sep 17 00:00:00 2001 From: SeghirOumo Date: Mon, 12 Aug 2024 15:33:01 +0200 Subject: [PATCH] Aftral[#90]: fixed trainee resources --- .../web/server/plugins/aftral-lms/block.js | 1 - backend/web/server/plugins/aftral-lms/user.js | 41 +++++++++++++++---- backend/web/tests/aftral-lms/user.test.js | 4 +- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/backend/web/server/plugins/aftral-lms/block.js b/backend/web/server/plugins/aftral-lms/block.js index 1759ba3e3f..8c5051a1f6 100644 --- a/backend/web/server/plugins/aftral-lms/block.js +++ b/backend/web/server/plugins/aftral-lms/block.js @@ -167,7 +167,6 @@ const getAttribute = attName => async (userId, params, data) => { } return !lodash.isNil(block[attName]) }) - console.log(block) res=block[attName] ATTRIBUTES_CACHE.set(key, res) } diff --git a/backend/web/server/plugins/aftral-lms/user.js b/backend/web/server/plugins/aftral-lms/user.js index 75a6cb8634..77f011f342 100644 --- a/backend/web/server/plugins/aftral-lms/user.js +++ b/backend/web/server/plugins/aftral-lms/user.js @@ -1,15 +1,40 @@ -const Session = require('../../models/Session') -const { getSessionBlocks } = require('./block') const { loadFromDb } = require('../../utils/database') const getTraineeResources = async (userId, params, data) => { - const sessions = await Session.find({ trainees: data._id }).populate('children') - const blocks = await Promise.all( - sessions.map(s => getSessionBlocks(s)) + 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 + ) + } + }) + ) ) - const resources = blocks.flat().filter(b => b.type == 'resource') - const res = await Promise.all(resources.map(b => loadFromDb({id:b._id, fields:['spent_time_str','name','resource_type','achievement_rule'], model:'resource', user:data}))) - return res + + console.log(resources) + return resources } diff --git a/backend/web/tests/aftral-lms/user.test.js b/backend/web/tests/aftral-lms/user.test.js index 42d5637aa6..2159728691 100644 --- a/backend/web/tests/aftral-lms/user.test.js +++ b/backend/web/tests/aftral-lms/user.test.js @@ -138,8 +138,8 @@ describe('User', () => { await addChildAction({parent: modulee1._id, child: sequence1._id}, conceptor) await addChildAction({parent: modulee1._id, child: sequence2._id}, conceptor) await addChildAction({parent: modulee2._id, child: sequence3._id}, conceptor) - await addChildAction({parent: sequence1._id, child: resource1._id}, conceptor) - await addChildAction({parent: sequence1._id, child: resource2._id}, conceptor) + //await addChildAction({parent: sequence1._id, child: resource1._id}, conceptor) + //await addChildAction({parent: sequence1._id, child: resource2._id}, conceptor) await addChildAction({parent: sequence2._id, child: resource3._id}, conceptor) await addChildAction({parent: sequence3._id, child: resource4._id}, conceptor)