diff --git a/backend/web/server/plugins/aftral-lms/block.js b/backend/web/server/plugins/aftral-lms/block.js index 0f69cce4d5..91adc6df30 100644 --- a/backend/web/server/plugins/aftral-lms/block.js +++ b/backend/web/server/plugins/aftral-lms/block.js @@ -298,9 +298,9 @@ const getBlockLiked = async (userId, params, data) => { const user = await User.findById(userId, {role:1}) const template = await getTemplate(data._id) if(user.role == ROLE_CONCEPTEUR) { - return template._likes.length > 0 + return template._liked_by.length > 0 } - return template._likes.some(like => idEqual(like, userId)) + return template._liked_by.some(like => idEqual(like, userId)) } const getBlockDisliked = async (userId, params, data) => { @@ -308,9 +308,9 @@ const getBlockDisliked = async (userId, params, data) => { const template = await getTemplate(data._id) console.log(template) if(user.role == ROLE_CONCEPTEUR) { - return template._dislikes.length > 0 + return template._disliked_by.length > 0 } - return template._dislikes.some(dislike => idEqual(dislike, userId)) + return template._disliked_by.some(dislike => idEqual(dislike, userId)) } const setBlockLiked = async ({ id, attribute, value, user }) => { @@ -319,17 +319,17 @@ const setBlockLiked = async ({ id, attribute, value, user }) => { return mongoose.models['block'].findByIdAndUpdate(template._id, { $pull: { - _dislikes: user._id + _disliked_by: user._id }, $addToSet: { - _likes: user._id + _liked_by: user._id } } ) } else{ return mongoose.models['block'].findByIdAndUpdate(template._id, - {$pull: {_likes: user._id}}) + {$pull: {_liked_by: user._id}}) } } @@ -339,25 +339,25 @@ const setBlockDisliked = async ({ id, attribute, value, user }) => { return mongoose.models['block'].findByIdAndUpdate(template._id, { $pull: { - _likes: user._id + _liked_by: user._id }, $addToSet: { - _dislikes: user._id + _disliked_by: user._id } } ) } else{ return mongoose.models['block'].findByIdAndUpdate(template._id, - {$pull: {_dislikes: user._id}}) + {$pull: {_disliked_by: user._id}}) } } const getTemplate = async(id) => { - let [currentBlock] = await mongoose.models.block.find({_id:id},{origin:1, _likes:1, _dislikes:1}) + let [currentBlock] = await mongoose.models.block.find({_id:id},{origin:1, _liked_by:1, _disliked_by:1}) let currentOrigin = currentBlock.origin while(currentOrigin) { - [currentBlock] = await mongoose.models.block.find({_id:currentOrigin},{origin:1, _likes:1, _dislikes:1}) + [currentBlock] = await mongoose.models.block.find({_id:currentOrigin},{origin:1, _liked_by:1, _disliked_by:1}) currentOrigin = currentBlock.origin } return currentBlock diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index 5f7e2f10f1..9829c2e1cd 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -78,11 +78,11 @@ BLOCK_MODELS.forEach(model => { declareComputedField({model, field: 'resources_count', getterFn: getResourcesCount}) declareComputedField({model, field: 'session', getterFn: getSession, requires:'parent'}) - declareVirtualField({model, field: 'likes_count', instance: 'Number', requires:'_likes'}) - declareVirtualField({model, field: 'dislikes_count', instance: 'Number', requires:'_dislikes'}) + declareVirtualField({model, field: 'likes_count', instance: 'Number', requires:'_liked_by'}) + declareVirtualField({model, field: 'dislikes_count', instance: 'Number', requires:'_disliked_by'}) - declareComputedField({model, field: 'liked', getterFn: getBlockLiked, setterFn: setBlockLiked, requires:'_likes,origin'}) - declareComputedField({model, field: 'disliked', getterFn: getBlockDisliked, setterFn: setBlockDisliked, requires:'_dislikes,origin'}) + declareComputedField({model, field: 'liked', getterFn: getBlockLiked, setterFn: setBlockLiked, requires:'_liked_by,origin'}) + declareComputedField({model, field: 'disliked', getterFn: getBlockDisliked, setterFn: setBlockDisliked, requires:'_disliked_by,origin'}) declareVirtualField({model, field: 'tickets_count', instance: 'Number', caster: { diff --git a/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js b/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js index 51cb7327d4..5ac9c5dcdd 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js @@ -188,7 +188,7 @@ const BlockSchema = new Schema({ default: false, required: true, }, - _likes:{ + _liked_by:{ type: [{ type: Schema.Types.ObjectId, ref: 'user', @@ -196,7 +196,7 @@ const BlockSchema = new Schema({ required: true, default: [] }, - _dislikes:{ + _disliked_by:{ type: [{ type: Schema.Types.ObjectId, ref: 'user', @@ -229,11 +229,11 @@ BlockSchema.virtual('search_text', {localField: 'tagada', foreignField: 'tagada' }) BlockSchema.virtual('likes_count', DUMMY_REF).get(function(){ - return this._likes.length || 0 + return this._liked_by.length || 0 }) BlockSchema.virtual('dislikes_count', DUMMY_REF).get(function(){ - return this._dislikes.length || 0 + return this._disliked_by.length || 0 }) BlockSchema.virtual('tickets', {