Skip to content

Commit

Permalink
Aftral[premieroctet#71]: refactored likes and dislikes
Browse files Browse the repository at this point in the history
  • Loading branch information
SeghirOumo committed Aug 20, 2024
1 parent 60d0923 commit f1e92b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
24 changes: 12 additions & 12 deletions backend/web/server/plugins/aftral-lms/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,19 @@ 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) => {
const user = await User.findById(userId, {role:1})
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 }) => {
Expand All @@ -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}})
}
}

Expand All @@ -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
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 @@ -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: {
Expand Down
8 changes: 4 additions & 4 deletions backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ const BlockSchema = new Schema({
default: false,
required: true,
},
_likes:{
_liked_by:{
type: [{
type: Schema.Types.ObjectId,
ref: 'user',
}],
required: true,
default: []
},
_dislikes:{
_disliked_by:{
type: [{
type: Schema.Types.ObjectId,
ref: 'user',
Expand Down Expand Up @@ -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', {
Expand Down

0 comments on commit f1e92b1

Please sign in to comment.