Skip to content

Commit

Permalink
Aftral[premieroctet#71]: hid likes and dislikes in block
Browse files Browse the repository at this point in the history
  • Loading branch information
SeghirOumo committed Aug 20, 2024
1 parent 503eb68 commit 99dffc7
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 @@ -310,19 +310,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._likes.length > 0
}
return template.likes.some(like => idEqual(like, userId))
return template._likes.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._dislikes.length > 0
}
return template.dislikes.some(dislike => idEqual(dislike, userId))
return template._dislikes.some(dislike => idEqual(dislike, userId))
}

const setBlockLiked = async ({ id, attribute, value, user }) => {
Expand All @@ -331,17 +331,17 @@ const setBlockLiked = async ({ id, attribute, value, user }) => {
return mongoose.models['block'].findByIdAndUpdate(template._id,
{
$pull: {
dislikes: user._id
_dislikes: user._id
},
$addToSet: {
likes: user._id
_likes: user._id
}
}
)
}
else{
return mongoose.models['block'].findByIdAndUpdate(template._id,
{$pull: {likes: user._id}})
{$pull: {_likes: user._id}})
}
}

Expand All @@ -351,25 +351,25 @@ const setBlockDisliked = async ({ id, attribute, value, user }) => {
return mongoose.models['block'].findByIdAndUpdate(template._id,
{
$pull: {
likes: user._id
_likes: user._id
},
$addToSet: {
dislikes: user._id
_dislikes: user._id
}
}
)
}
else{
return mongoose.models['block'].findByIdAndUpdate(template._id,
{$pull: {dislikes: user._id}})
{$pull: {_dislikes: 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, _likes:1, _dislikes: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, _likes:1, _dislikes: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.parent.parent.parent.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:'_likes'})
declareVirtualField({model, field: 'dislikes_count', instance: 'Number', requires:'_dislikes'})

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:'_likes,origin'})
declareComputedField({model, field: 'disliked', getterFn: getBlockDisliked, setterFn: setBlockDisliked, requires:'_dislikes,origin'})
})

declareEnumField({model: 'homework', field: 'scale', enumValues: SCALE})
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 @@ -191,15 +191,15 @@ const BlockSchema = new Schema({
default: false,
required: true,
},
likes:{
_likes:{
type: [{
type: Schema.Types.ObjectId,
ref: 'user',
}],
required: true,
default: []
},
dislikes:{
_dislikes:{
type: [{
type: Schema.Types.ObjectId,
ref: 'user',
Expand Down Expand Up @@ -232,11 +232,11 @@ BlockSchema.virtual('search_text', {localField: 'tagada', foreignField: 'tagada'
})

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

BlockSchema.virtual('dislikes_count', DUMMY_REF).get(function(){
return this.dislikes.length || 0
return this._dislikes.length || 0
})

// Validate Succes achievemnt
Expand Down

0 comments on commit 99dffc7

Please sign in to comment.