Skip to content

Commit

Permalink
Aftral[premieroctet#71]: fixed likes and dislikes on resource, now de…
Browse files Browse the repository at this point in the history
…aling with origin directly
  • Loading branch information
SeghirOumo committed Aug 19, 2024
1 parent 6b2f4fd commit 503eb68
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions backend/web/server/plugins/aftral-lms/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ getSession = async (userId, params, data) => {
const getBlockLiked = 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.likes.length > 0
}
Expand All @@ -319,17 +318,17 @@ 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,'----------------')
console.log(template)
if(user.role == ROLE_CONCEPTEUR) {
return template.dislikes.length > 0
}
return template.dislikes.some(dislike => idEqual(dislike, userId))
}

const setBlockLiked = async ({ id, attribute, value, user }) => {
const templateId = await getTemplate(id)
const template = await getTemplate(id)
if(value) {
return mongoose.models['block'].findByIdAndUpdate(templateId,
return mongoose.models['block'].findByIdAndUpdate(template._id,
{
$pull: {
dislikes: user._id
Expand All @@ -341,15 +340,15 @@ const setBlockLiked = async ({ id, attribute, value, user }) => {
)
}
else{
return mongoose.models['block'].findByIdAndUpdate(templateId,
return mongoose.models['block'].findByIdAndUpdate(template._id,
{$pull: {likes: user._id}})
}
}

const setBlockDisliked = async ({ id, attribute, value, user }) => {
const templateId = await getTemplate(id)
const template = await getTemplate(id)
if(value) {
return mongoose.models['block'].findByIdAndUpdate(templateId,
return mongoose.models['block'].findByIdAndUpdate(template._id,
{
$pull: {
likes: user._id
Expand All @@ -361,16 +360,16 @@ const setBlockDisliked = async ({ id, attribute, value, user }) => {
)
}
else{
return mongoose.models['block'].findByIdAndUpdate(templateId,
return mongoose.models['block'].findByIdAndUpdate(template._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

0 comments on commit 503eb68

Please sign in to comment.