diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index dd6837f723..df1a576834 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -134,6 +134,13 @@ 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: 'resources', field: 'likes_count', instance: 'Number', requires:'likes'}) +declareVirtualField({model: 'resources', field: 'dislikes_count', instance: 'Number', requires:'dislikes'}) + +declareComputedField({model: 'resources', field: 'liked', getterFn: isBlockLiked, requires:'likes'}) +declareComputedField({model: 'resources', 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 @@ -208,6 +215,20 @@ const prePut = async ({model, id, params, user, skip_validation}) => { params.achievement_rule=DEFAULT_ACHIEVEMENT_RULE[params.resource_type] params.achievement_status = block.achievement_status } + } + if (model=='post'){ + if('liked' in params){ + + await Post.updateOne( + {_id:id}, + { + ...params.liked ? {$addToSet: {likes: user._id}} + : {$pull: {likes: user._id}} + } + )} + } + + 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 @@ -223,17 +244,6 @@ const prePut = async ({model, id, params, user, skip_validation}) => { await Resource.updateOne({_id: id}, {$pull: {dislikes: user._id}}) } } - if (model=='post'){ - if('liked' in params){ - - await Post.updateOne( - {_id:id}, - { - ...params.liked ? {$addToSet: {likes: user._id}} - : {$pull: {likes: user._id}} - } - )} - } return {model, id, params, user, skip_validation} } diff --git a/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js b/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js index 86d29a63c2..9124e5b456 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js @@ -22,6 +22,40 @@ 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