Skip to content

Commit

Permalink
Aftral[premieroctet#116]: added trainees, available trainees and excl…
Browse files Browse the repository at this point in the history
…uded trainees counts to group
  • Loading branch information
SeghirOumo committed Aug 27, 2024
1 parent 5861891 commit 85995e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ declareVirtualField({model:`group`, field: `excluded_trainees`, instance: `Array
options: {ref: `user`}
}
})
declareVirtualField({model: `group`, field: `trainees_count`, instance: `Number`, requires: `trainees`})
declareVirtualField({model: `group`, field: `available_trainees_count`, instance: `Number`, requires: `available_trainees`})
declareVirtualField({model: `group`, field: `excluded_trainees_count`, instance: `Number`, requires: `trainees,available_trainees`})
// Group end

const preCreate = async ({model, params, user}) => {
params.creator=params.creator || user._id
Expand Down
12 changes: 12 additions & 0 deletions backend/web/server/plugins/aftral-lms/schemas/GroupSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,16 @@ GroupSchema.virtual('excluded_trainees', DUMMY_REF).get(function(){
return differenceIds
})

GroupSchema.virtual('trainees_count', DUMMY_REF).get(function(){
return this.trainees? this.trainees.length : 0
})

GroupSchema.virtual('available_trainees_count', DUMMY_REF).get(function(){
return this.available_trainees? this.available_trainees.length : 0
})

GroupSchema.virtual('excluded_trainees_count', DUMMY_REF).get(function(){
return this.available_trainees.length - this.trainees.length
})

module.exports = GroupSchema
15 changes: 13 additions & 2 deletions backend/web/tests/aftral-lms/group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,23 @@ describe('Groups', () => {
await mongoose.connection.close()
})

it('must return group removed trainees', async() => {
const [data] = await loadFromDb({model: `group`, user, fields: [`trainees`, `sessions.trainees`,`available_trainees`,`excluded_trainees`]})
it('must return group attributes', async() => {
const [data] = await loadFromDb({model: `group`, user, fields: [
`trainees`,
`sessions.trainees`,
`available_trainees`,
`excluded_trainees`,
`trainees_count`,
`available_trainees_count`,
`excluded_trainees_count`,
]})
// console.log(JSON.stringify(data.sessions, null, 2))
// await preCreate({model: `group`, user, params})
// await prePut({model: `group`, user, id:group._id, params:{sessions:[session2._id, session3._id],creator:user._id}})
expect(data.trainees.length).toEqual(4)
expect(idEqual(data.excluded_trainees[0], trainee5._id)).toBeTruthy()
expect(data.available_trainees_count).toEqual(5)
expect(data.trainees_count).toEqual(4)
expect(data.excluded_trainees_count).toEqual(1)
})
})

0 comments on commit 85995e3

Please sign in to comment.