diff --git a/backend/web/server/plugins/aftral-lms/block.js b/backend/web/server/plugins/aftral-lms/block.js index 3759323d77..470489c7b4 100644 --- a/backend/web/server/plugins/aftral-lms/block.js +++ b/backend/web/server/plugins/aftral-lms/block.js @@ -4,8 +4,9 @@ const mongoose=require('mongoose') const Progress = require("../../models/Progress") const { BLOCK_STATUS_CURRENT, BLOCK_STATUS_FINISHED, BLOCK_STATUS_TO_COME, BLOCK_STATUS_UNAVAILABLE, ACHIEVEMENT_RULE_CHECK, ROLE_CONCEPTEUR } = require("./consts"); const { getBlockResources } = require("./resources"); -const { idEqual, loadFromDb } = require("../../utils/database"); +const { idEqual, loadFromDb, getModel } = require("../../utils/database"); const Block = require("../../models/Block"); +const User = require("../../models/User"); const NAMES_CACHE=new NodeCache() @@ -305,21 +306,65 @@ getSession = async (userId, params, data) => { } } -const isBlockLiked = async (userId, params, data) => { - const likes = data.likes.map(l=> l._id) - return likes.includes(userId) +const getBlockLiked = async (userId, params, data) => { + const user = await User.findById(userId, {role:1}) + if(user.role == ROLE_CONCEPTEUR) { + return data.likes.length > 0 + } + return data.likes.some(like => idEqual(like._id, userId)) +} + +const getBlockDisliked = async (userId, params, data) => { + const user = await User.findById(userId, {role:1}) + if(user.role == ROLE_CONCEPTEUR) { + return data.dislikes.length > 0 + } + return data.dislikes.some(dislike => idEqual(dislike._id, userId)) +} + +const setBlockLiked = ({ id, attribute, value, user }) => { + if(value) { + return mongoose.models['resource'].findByIdAndUpdate(id, + { + $pull: { + dislikes: user._id + }, + $addToSet: { + likes: user._id + } + } + ) + } + else{ + return mongoose.models['resource'].findByIdAndUpdate(id, + {$pull: {likes: user._id}}) + } } -const isBlockDisliked = async (userId, params, data) => { - const dislikes = data.dislikes.map(l=> l._id) - return dislikes.includes(userId) +const setBlockDisliked = ({ id, attribute, value, user }) => { + if(value) { + return mongoose.models['block'].findByIdAndUpdate(id, + { + $pull: { + likes: user._id + }, + $addToSet: { + dislikes: user._id + } + } + ) + } + else{ + return mongoose.models['block'].findByIdAndUpdate(id, + {$pull: {dislikes: user._id}}) + } } module.exports={ getBlockStatus, getBlockName, getSessionBlocks, setParentSession, cloneTree, getAttribute, LINKED_ATTRIBUTES, onBlockFinished, onBlockCurrent, onBlockAction, getNextResource, getPreviousResource, getParentBlocks,LINKED_ATTRIBUTES_CONVERSION, - ChainCache, ATTRIBUTES_CACHE,getSession, isBlockDisliked, isBlockLiked + ChainCache, ATTRIBUTES_CACHE,getSession, getBlockLiked, getBlockDisliked, setBlockLiked, setBlockDisliked } diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index fd96f9cb69..a03e72c3dd 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -18,7 +18,7 @@ require('../../models/Chapter') //Added chapter, it was removed somehow const { computeStatistics } = require('./statistics') const { searchUsers, searchBlocks } = require('./search') const { getUserHomeworks, getResourceType, getAchievementRules, getBlockSpentTime, getBlockSpentTimeStr, getResourcesCount, getFinishedResourcesCount, getRessourceSession } = require('./resources') -const { getBlockStatus, setParentSession, getAttribute, LINKED_ATTRIBUTES, onBlockAction, LINKED_ATTRIBUTES_CONVERSION, getSession, isBlockLiked, isBlockDisliked} = require('./block') +const { getBlockStatus, setParentSession, getAttribute, LINKED_ATTRIBUTES, onBlockAction, LINKED_ATTRIBUTES_CONVERSION, getSession} = require('./block') const { getResourcesProgress } = require('./resources') const { getResourceAnnotation } = require('./resources') const { setResourceAnnotation } = require('./resources') @@ -35,6 +35,10 @@ const { getTraineeResources } = require('./user') const { isMine } = require('./message') const { DURATION_UNIT } = require('./consts') const { isLiked } = require('./post') +const { getBlockLiked } = require('./block') +const { getBlockDisliked } = require('./block') +const { setBlockLiked } = require('./block') +const { setBlockDisliked } = require('./block') const GENERAL_FEED_ID='FFFFFFFFFFFFFFFFFFFFFFFF' @@ -77,8 +81,8 @@ BLOCK_MODELS.forEach(model => { declareVirtualField({model, field: 'likes_count', instance: 'Number', requires:'likes'}) declareVirtualField({model, field: 'dislikes_count', instance: 'Number', requires:'dislikes'}) - declareComputedField({model, field: 'liked', getterFn: isBlockLiked, requires:'likes'}) - declareComputedField({model, field: 'disliked', getterFn: isBlockDisliked, requires:'dislikes'}) + declareComputedField({model, field: 'liked', getterFn: getBlockLiked, setterFn: setBlockLiked, requires:'likes'}) + declareComputedField({model, field: 'disliked', getterFn: getBlockDisliked, setterFn: setBlockDisliked, requires:'dislikes'}) }) declareEnumField({model: 'homework', field: 'scale', enumValues: SCALE}) @@ -123,13 +127,6 @@ declareVirtualField({model:'post', field: 'comments_count', instance: 'Number', declareVirtualField({model:'post', field: 'likes_count', instance: 'Number', requires:'likes'}) declareComputedField({model: 'post', field: 'liked', getterFn: isLiked, requires:'likes'}) -//Resources -declareVirtualField({model: 'resource', field: 'likes_count', instance: 'Number', requires:'likes'}) -declareVirtualField({model: 'resource', field: 'dislikes_count', instance: 'Number', requires:'dislikes'}) - -declareComputedField({model: 'resource', field: 'liked', getterFn: isBlockLiked, requires:'likes'}) -declareComputedField({model: 'resource', field: 'disliked', getterFn: isBlockDisliked, requires:'dislikes'}) -//Resources end const preCreate = async ({model, params, user}) => { params.creator=params.creator || user._id params.last_updater=user._id @@ -202,7 +199,7 @@ const prePut = async ({model, id, params, user, skip_validation}) => { throw new Error(`Le type de ressource ne peut changer`) } params.achievement_rule=DEFAULT_ACHIEVEMENT_RULE[params.resource_type] - params.achievement_status = block.achievement_status + // params.achievement_status = block.achievement_status } } if (model=='post'){ @@ -217,23 +214,6 @@ const prePut = async ({model, id, params, user, skip_validation}) => { )} } - if (['block', 'resource'].includes(model)){ - if(params.liked == true) { - await Resource.updateOne({_id:id},{$pull: {dislikes: user._id}, $addToSet: {likes: user._id}}) - params.disliked = false - } - if(params.liked == false){ - await Resource.updateOne({_id: id}, {$pull: {likes: user._id}}) - } - if(params.disliked == true) { - await Resource.updateOne({_id:id},{$pull: {likes: user._id}, $addToSet: {dislikes: user._id}}) - params.liked = false - } - if(params.disliked == false){ - await Resource.updateOne({_id: id}, {$pull: {dislikes: user._id}}) - } - } - if(model == `program`) { const program = await Program.findById(id) params.codes = program.codes diff --git a/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js b/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js index 9124e5b456..86d29a63c2 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js @@ -22,40 +22,6 @@ const ResourceSchema = new Schema({ mine: { type: Boolean, }, - liked: { - type: Boolean, - required: true, - default: false, - }, - disliked: { - type: Boolean, - default: false, - required: true, - }, - likes:{ - type: [{ - type: Schema.Types.ObjectId, - ref: 'user', - }], - required: true, - default: [] - }, - dislikes:{ - type: [{ - type: Schema.Types.ObjectId, - ref: 'user', - }], - required: true, - default: [] - }, }, {...schemaOptions, ...BLOCK_DISCRIMINATOR}) -ResourceSchema.virtual('likes_count', DUMMY_REF).get(function(){ - return this.likes.length || 0 -}) - -ResourceSchema.virtual('dislikes_count', DUMMY_REF).get(function(){ - return this.dislikes.length || 0 -}) - module.exports = ResourceSchema diff --git a/backend/web/tests/aftral-lms/resource.test.js b/backend/web/tests/aftral-lms/resource.test.js index b2b16dd1ab..c22ee9f27d 100644 --- a/backend/web/tests/aftral-lms/resource.test.js +++ b/backend/web/tests/aftral-lms/resource.test.js @@ -15,7 +15,6 @@ const { updateAllDurations, updateDuration, lockSession } = require('../../serve require('../../server/plugins/aftral-lms/actions') const Block = require('../../server/models/Block') const { ACTIONS } = require('../../server/utils/studio/actions') -const Duration = require('../../server/models/Duration') const { SseKmsEncryptedObjectsStatus } = require('@aws-sdk/client-s3') @@ -49,4 +48,9 @@ describe('Test models computations', () => { expect(trainer_load.find(r => /desig/i.test(r.name)).mine).toBe(false) }) + it.only('must tell if i liked resource or not', async () => { + const user = await User.findOne({role:ROLE_APPRENANT}) + const [resource] = await loadFromDb({model:'resource',id:'66b60f473f1ec37a3a4cd961', user, fields:['liked','disliked','_locked']}) + console.log(resource) + }) }) diff --git a/studio-test/package.json b/studio-test/package.json index ee2f397a83..80611126a7 100644 --- a/studio-test/package.json +++ b/studio-test/package.json @@ -42,6 +42,7 @@ "next": "^12.3.1", "next-routes": "^1.4.2", "pdf-lib": "^1.17.1", + "process": "^0.11.10", "react": "^18.2.0", "react-chartjs-2": "^5.2.0", "react-dom": "^18.2.0", diff --git a/studio-test/yarn.lock b/studio-test/yarn.lock index c25046bc05..885aade0e5 100644 --- a/studio-test/yarn.lock +++ b/studio-test/yarn.lock @@ -4401,6 +4401,11 @@ process-nextick-args@~2.0.0: resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2" integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag== +process@^0.11.10: + version "0.11.10" + resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182" + integrity sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A== + prop-types@^15.6.0, prop-types@^15.6.2, prop-types@^15.8.1: version "15.8.1" resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"