Skip to content

Commit

Permalink
Afvtral[premieroctet#51]: refactored likes for post
Browse files Browse the repository at this point in the history
  • Loading branch information
SeghirOumo committed Aug 20, 2024
1 parent f1e92b1 commit a49ebfd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
1 change: 0 additions & 1 deletion backend/web/server/plugins/aftral-lms/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ const getBlockLiked = async (userId, params, data) => {
const getBlockDisliked = async (userId, params, data) => {
const user = await User.findById(userId, {role:1})
const template = await getTemplate(data._id)
console.log(template)
if(user.role == ROLE_CONCEPTEUR) {
return template._disliked_by.length > 0
}
Expand Down
8 changes: 4 additions & 4 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ declareVirtualField({model:'post', field: 'comments_count', instance: 'Number',
instance: 'ObjectID',
options: {ref: 'block'}},
})
declareVirtualField({model:'post', field: 'likes_count', instance: 'Number', requires:'likes'})
declareComputedField({model: 'post', field: 'liked', getterFn: isLiked, requires:'likes'})
declareVirtualField({model:'post', field: 'likes_count', instance: 'Number', requires:'_liked_by'})
declareComputedField({model: 'post', field: 'liked', getterFn: isLiked, requires:'_liked_by'})
// Post end

// Ticket start
Expand Down Expand Up @@ -257,8 +257,8 @@ const prePut = async ({model, id, params, user, skip_validation}) => {
await Post.updateOne(
{_id:id},
{
...params.liked ? {$addToSet: {likes: user._id}}
: {$pull: {likes: user._id}}
...params.liked ? {$addToSet: {_liked_by: user._id}}
: {$pull: {_liked_by: user._id}}
}
)}
}
Expand Down
5 changes: 3 additions & 2 deletions backend/web/server/plugins/aftral-lms/post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { idEqual } = require("../../utils/database")

const isLiked = async(userId, params, data) => {
const likes = data._likes.map(l=> l._id)
return likes.includes(userId)
return data._liked_by.some(like=> idEqual(l._id,userId))
}

module.exports={
Expand Down
4 changes: 2 additions & 2 deletions backend/web/server/plugins/aftral-lms/schemas/PostSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const PostSchema = new Schema({
required: true,
enum: Object.keys(FEED_TYPE),
},
_likes: [{
_liked_by: [{
type: Schema.Types.ObjectId,
ref: 'user',
}],
Expand All @@ -43,7 +43,7 @@ const PostSchema = new Schema({
}, schemaOptions)

PostSchema.virtual('likes_count', DUMMY_REF).get(function(){
return this._likes.length || 0
return this._liked_by.length || 0
})

PostSchema.virtual('comments', {
Expand Down

0 comments on commit a49ebfd

Please sign in to comment.