From 61fa17c5e7f1e8b022c9c9d1b9a8d1f0ebc4a974 Mon Sep 17 00:00:00 2001 From: SeghirOumo Date: Mon, 12 Aug 2024 11:11:07 +0200 Subject: [PATCH] Revert "Aftral[#90]: changed how user resources are calculated" This reverts commit 626c581a63f769c009ccc72620c368828ae8bdf3. --- backend/web/server/plugins/aftral-lms/user.js | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/backend/web/server/plugins/aftral-lms/user.js b/backend/web/server/plugins/aftral-lms/user.js index 75a6cb8634..3d18af4d2b 100644 --- a/backend/web/server/plugins/aftral-lms/user.js +++ b/backend/web/server/plugins/aftral-lms/user.js @@ -1,15 +1,47 @@ +const mongoose = require('mongoose') const Session = require('../../models/Session') -const { getSessionBlocks } = require('./block') -const { loadFromDb } = require('../../utils/database') +const { getBlockResources } = require('./resources') +const Block = require('../../models/Block') +const Resource = require('../../models/Resource') +const Program = require('../../models/Program') +const Chapter = require('../../models/Chapter') +const Module = require('../../models/Module') +const Sequence = require('../../models/Sequence') +const { BLOCK_STATUS_CURRENT } = require('./consts') + +const getRelatedDocuments = async (Model, ids) => { + return await Model.find({ _id: { $in: ids } }).populate('children') +} + +const getIdsFromChildren = (documents) => { + return documents.flatMap(doc => doc.children.map(child => child._id)) +} 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)) - ) - 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 + + const programIds = getIdsFromChildren(sessions) + const programs = await getRelatedDocuments(Program, programIds) + + let modules + + if (programs[0]?.children[0]?.type === 'chapter') { + const chapterIds = getIdsFromChildren(programs) + const chapters = await getRelatedDocuments(Chapter, chapterIds) + + const moduleIds = getIdsFromChildren(chapters) + modules = await getRelatedDocuments(Module, moduleIds) + } else { + const moduleIds = getIdsFromChildren(programs) + modules = await getRelatedDocuments(Module, moduleIds) + } + + const sequenceIds = getIdsFromChildren(modules) + const sequences = await getRelatedDocuments(Sequence, sequenceIds) + + const resourceIds = getIdsFromChildren(sequences) + const resources = await Resource.find({ _id: { $in: resourceIds } }) + return resources }