Skip to content

Commit

Permalink
Aftral: [premieroctet#90] Trainee current resources
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaauvray committed Aug 20, 2024
1 parent d7d7518 commit 5cc79fd
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 56 deletions.
3 changes: 1 addition & 2 deletions backend/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'})
})

Expand Down
13 changes: 5 additions & 8 deletions backend/web/server/plugins/aftral-lms/schemas/UserSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
47 changes: 12 additions & 35 deletions backend/web/server/plugins/aftral-lms/user.js
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 6 additions & 5 deletions backend/web/server/utils/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
})
Expand Down
3 changes: 1 addition & 2 deletions studio-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,5 @@
},
"devDependencies": {
"@types/mime": "^3.0.1"
},
"packageManager": "yarn@3.6.4"
}
}
3 changes: 1 addition & 2 deletions studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,5 @@
"eslint src/**/*.{ts,tsx} --fix",
"git add"
]
},
"packageManager": "yarn@3.6.4"
}
}

0 comments on commit 5cc79fd

Please sign in to comment.