Skip to content

Commit

Permalink
Merge branch 'aftral-lms-resources' into aftral-lms-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
SeghirOumo committed Aug 14, 2024
2 parents 3d49ca0 + 441947f commit 109a47e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
32 changes: 21 additions & 11 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
}

Expand Down
34 changes: 34 additions & 0 deletions backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 109a47e

Please sign in to comment.