Skip to content

Commit

Permalink
Aftral[#likes]: added likes on resources
Browse files Browse the repository at this point in the history
  • Loading branch information
SeghirOumo committed Aug 14, 2024
1 parent f4280f6 commit 41f2cae
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
17 changes: 17 additions & 0 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const { getTraineeResources } = require('./user')
const { isMine } = require('./message')
const { DURATION_UNIT } = require('./consts')
const { isLiked } = require('./post')
const { isResourceLiked } = require('./resources')
const { isResourceDisliked } = require('./resources')

const GENERAL_FEED_ID='FFFFFFFFFFFFFFFFFFFFFFFF'

Expand Down Expand Up @@ -126,6 +128,14 @@ 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})

// Resource start
declareVirtualField({model:'resource', field: 'likes_count', instance: 'Number', requires:'likes'})
declareVirtualField({model:'resource', field: 'dislikes_count', instance: 'Number', requires:'dislikes'})

declareComputedField({model: 'resource', field: 'liked', getterFn: isResourceLiked})
declareComputedField({model: 'resource', field: 'disliked', getterFn: isResourceDisliked})
// Resource end

const preCreate = async ({model, params, user}) => {
params.creator=params.creator || user._id
params.last_updater=user._id
Expand Down Expand Up @@ -195,6 +205,13 @@ 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(params.liked == true) {
await Resource.updateOne({_id:id},{$pull: {dislikes: user._id}, $addToSet: {likes: user._id}})
}
if(params.disliked == true) {
await Resource.updateOne({_id:id},{$pull: {likes: user._id}, $addToSet: {dislikes: user._id}})
}
}
}
return {model, id, params, user, skip_validation}
Expand Down
12 changes: 11 additions & 1 deletion backend/web/server/plugins/aftral-lms/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,18 @@ const canReplay = async ({dataId, user }) => {
return blockHasStatus({user, block: dataId, status: BLOCK_STATUS_FINISHED})
}

const isResourceLiked = async (userId, params, data) => {
const likes = data.likes.map(l=> l._id)
return likes.includes(userId)
}

const isResourceDisliked = async (userId, params, data) => {
const dislikes = data.dislikes.map(l=> l._id)
return dislikes.includes(userId)
}

module.exports={
getFinishedResourcesCount, isResourceMine, setResourceAnnotation, getResourceAnnotation, getResourcesProgress, getUserHomeworks, onSpentTimeChanged,
getResourceType, getBlockSpentTime, getBlockSpentTimeStr, getResourcesCount, canPlay, canReplay, canResume,
getBlockResources
getBlockResources, isResourceLiked, isResourceDisliked
}
35 changes: 35 additions & 0 deletions backend/web/server/plugins/aftral-lms/schemas/ResourceSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const mongoose = require('mongoose')
const {schemaOptions} = require('../../../utils/schemas')
const Schema = mongoose.Schema
const {BLOCK_DISCRIMINATOR}=require('../consts')
const { DUMMY_REF } = require('../../../utils/database')

const ResourceSchema = new Schema({
shortName: {
Expand All @@ -21,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
1 change: 0 additions & 1 deletion backend/web/server/plugins/aftral-lms/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const getTraineeResources = async (userId, params, data) => {
)
)

console.log(resources)
return resources
}

Expand Down
34 changes: 34 additions & 0 deletions backend/web/tests/aftral-lms/db.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const mongoose = require('mongoose')
const { MONGOOSE_OPTIONS, loadFromDb } = require('../../server/utils/database')
require('../../server/plugins/aftral-lms/functions')
const User = require('../../server/models/User')
const Resource = require('../../server/models/Resource')
const Sequence = require('../../server/models/Sequence')
const Module = require('../../server/models/Module')
const Program = require('../../server/models/Program')
const Session = require('../../server/models/Session')
const ProductCode = require('../../server/models/ProductCode')
const Block = require('../../server/models/Block')
const { ROLE_APPRENANT, ROLE_CONCEPTEUR } = require('../../server/plugins/aftral-lms/consts')

jest.setTimeout(60000)

describe('User', () => {
beforeAll(async () => {
await mongoose.connect(`mongodb://localhost/aftral-lms`, MONGOOSE_OPTIONS)
})
afterAll(async () => {
await mongoose.connection.close()
})
it('must return if resource is mine', async () => {
const user = await User.findOne({ role: ROLE_APPRENANT })
const concepteur = await User.findOne({role: ROLE_CONCEPTEUR})
let [u] = await loadFromDb({ model: 'user', fields: ['resources'], user, id:user._id })
let resources = await loadFromDb({ model: 'resource', fields: ['likes','dislikes', 'liked', 'likes_count', 'dislikes_count'], user:concepteur})
resources = resources.filter(r=> {
return r.likes.length>0
})
console.log(resources[0].likes[0]._id, resources)
console.log(user._id)
})
})

0 comments on commit 41f2cae

Please sign in to comment.