Skip to content

Commit

Permalink
Aftral[premieroctet#90]: changed how user resources are calculated
Browse files Browse the repository at this point in the history
  • Loading branch information
SeghirOumo committed Aug 12, 2024
1 parent 30884ef commit 626c581
Showing 1 changed file with 8 additions and 40 deletions.
48 changes: 8 additions & 40 deletions backend/web/server/plugins/aftral-lms/user.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
const mongoose = require('mongoose')
const Session = require('../../models/Session')
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 { 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 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
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
}


Expand Down

0 comments on commit 626c581

Please sign in to comment.