From 3eadf1c998482d60e9e1f61b5e263e88c35d247a Mon Sep 17 00:00:00 2001 From: SeghirOumo Date: Tue, 27 Aug 2024 16:59:46 +0200 Subject: [PATCH] Aftral[#120 & #121]: added help desk conversation and session conversation --- .../web/server/models/HelpDeskConversation.js | 20 +++++ .../web/server/models/SessionConversation.js | 20 +++++ .../server/plugins/aftral-lms/functions.js | 79 +++++++++++-------- .../schemas/HelpDeskConversationSchema.js | 22 ++++++ .../aftral-lms/schemas/MessageSchema.js | 7 +- .../schemas/SessionConversationSchema.js | 17 ++++ 6 files changed, 130 insertions(+), 35 deletions(-) create mode 100644 backend/web/server/models/HelpDeskConversation.js create mode 100644 backend/web/server/models/SessionConversation.js create mode 100644 backend/web/server/plugins/aftral-lms/schemas/HelpDeskConversationSchema.js create mode 100644 backend/web/server/plugins/aftral-lms/schemas/SessionConversationSchema.js diff --git a/backend/web/server/models/HelpDeskConversation.js b/backend/web/server/models/HelpDeskConversation.js new file mode 100644 index 0000000000..14c7a33fde --- /dev/null +++ b/backend/web/server/models/HelpDeskConversation.js @@ -0,0 +1,20 @@ +const {getDataModel}=require('../../config/config') + +let HelpDeskConversation = null + +try { + const Conversation = require(`./Conversation`) + if (Conversation) { + const HelpDeskConversationSchema=require(`../plugins/${getDataModel()}/schemas/HelpDeskConversationSchema`) + HelpDeskConversationSchema.plugin(require('mongoose-lean-virtuals')) + HelpDeskConversation = Conversation.discriminator('helpDeskConversation', HelpDeskConversationSchema) + } +} +catch (err) { + console.error(err) + if (err.code !== 'MODULE_NOT_FOUND') { + throw err + } +} + +module.exports = HelpDeskConversation diff --git a/backend/web/server/models/SessionConversation.js b/backend/web/server/models/SessionConversation.js new file mode 100644 index 0000000000..19b0b91dbc --- /dev/null +++ b/backend/web/server/models/SessionConversation.js @@ -0,0 +1,20 @@ +const {getDataModel}=require('../../config/config') + +let SessionConversation = null + +try { + const Conversation = require(`./Conversation`) + if (Conversation) { + const SessionConversationSchema=require(`../plugins/${getDataModel()}/schemas/SessionConversationSchema`) + SessionConversationSchema.plugin(require('mongoose-lean-virtuals')) + SessionConversation = Conversation.discriminator('sessionConversation', SessionConversationSchema) + } +} +catch (err) { + console.error(err) + if (err.code !== 'MODULE_NOT_FOUND') { + throw err + } +} + +module.exports = SessionConversation diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index bee90f9136..3891c63917 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -4,7 +4,7 @@ const { declareVirtualField, setPreCreateData, setPreprocessGet, setMaxPopulateDepth, setFilterDataUser, declareComputedField, declareEnumField, idEqual, getModel, declareFieldDependencies, setPostPutData, setPreDeleteData, setPrePutData, loadFromDb, setPostCreateData, } = require('../../utils/database') -const { RESOURCE_TYPE, PROGRAM_STATUS, ROLES, MAX_POPULATE_DEPTH, BLOCK_STATUS, ROLE_CONCEPTEUR, ROLE_FORMATEUR,ROLE_APPRENANT, FEED_TYPE_GENERAL, FEED_TYPE_SESSION, FEED_TYPE_GROUP, FEED_TYPE, ACHIEVEMENT_RULE, SCALE, RESOURCE_TYPE_LINK, DEFAULT_ACHIEVEMENT_RULE, BLOCK_STATUS_TO_COME, BLOCK_STATUS_CURRENT, TICKET_STATUS, TICKET_TAG, PERMISSIONS } = require('./consts') +const { RESOURCE_TYPE, PROGRAM_STATUS, ROLES, MAX_POPULATE_DEPTH, BLOCK_STATUS, ROLE_CONCEPTEUR, ROLE_FORMATEUR,ROLE_APPRENANT, FEED_TYPE_GENERAL, FEED_TYPE_SESSION, FEED_TYPE_GROUP, FEED_TYPE, ACHIEVEMENT_RULE, SCALE, RESOURCE_TYPE_LINK, DEFAULT_ACHIEVEMENT_RULE, BLOCK_STATUS_TO_COME, BLOCK_STATUS_CURRENT, TICKET_STATUS, TICKET_TAG, PERMISSIONS, ROLE_HELPDESK } = require('./consts') const mongoose = require('mongoose') require('../../models/Resource') const Session = require('../../models/Session') @@ -42,6 +42,8 @@ const { setBlockLiked } = require('./block') const { setBlockDisliked } = require('./block') const Permission = require('../../models/Permission') const Group = require('../../models/Group') +const HelpDeskConversation = require('../../models/HelpDeskConversation') +const SessionConversation = require('../../models/SessionConversation') const { getUserPermissions } = require('./user') const GENERAL_FEED_ID='FFFFFFFFFFFFFFFFFFFFFFFF' @@ -232,6 +234,27 @@ const preCreate = async ({model, params, user}) => { if(model=='message'){ params.sender=params.creator params.receiver=params.parent + const model = await getModel(params.parent, [`helpDeskConversation`,`sessionConversation`,`session`]) + if(model == `session`) { + const value = user.role == ROLE_APPRENANT + ? user._id + : user.role == ROLE_FORMATEUR + ? params.receiver + : null + if(value) { + const sessionConv = new SessionConversation({ + trainee: value, + session: params.parent + }) + await sessionConv.validate() + new Message({...params, converation:sessionConv._id}).validate() + await sessionConv.save() + params.conversation = sessionConv._id + } + } + else { + params.conversation=params.parent + } } if (model == 'comment'){ params.user = user._id @@ -462,40 +485,20 @@ const preprocessGet = async ({model, fields, id, user, params}) => { return computeStatistics({model, fields, id, user, params}) .then(data => ({data})) } - if (model=='conversation') { - const getPartner= (m, user) => { - return idEqual(m.sender._id, user._id) ? m.receiver : m.sender + if (model == `helpDeskConversation` && !id && user.role !== ROLE_HELPDESK) { + params['filter.user']=user + } + + if (model == `sessionConversation` && !id) { + let filter + if(user.role == ROLE_APPRENANT) { + filter = {trainees:user._id} } - - // Get non-group messages (i.e. no group attribute) - return Message.find({$or: [{sender: user._id}, {receiver: user._id}]}) - .populate({path: 'sender'}) - .populate({path: 'receiver'}) - .sort({CREATED_AT_ATTRIBUTE: 1}) - .then(messages => { - if (id) { - messages=messages.filter(m => idEqual(getPartner(m, user)._id, id)) - // If no messages for one parner, forge it - if (lodash.isEmpty(messages)) { - return User.findById(id) - .then(partner => { - const data=[{_id: partner._id, partner, messages: []}] - return {model, fields, id, data, params} - }) - } - } - const partnerMessages=lodash.groupBy(messages, m => getPartner(m, user)._id) - const convs=lodash(partnerMessages) - .values() - .map(msgs => { - const partner=getPartner(msgs[0], user) - msgs = msgs.map(m => ({...m.toObject(), mine : idEqual(m.sender._id, user._id)})) - return ({_id: partner._id, partner, messages: msgs, newest_message: lodash.maxBy(messages, 'creation_date')}) - }) - .sortBy(CREATED_AT_ATTRIBUTE, 'asc') - .value() - return {model, fields, id, data: convs, params} - }) + else if(user.role == ROLE_FORMATEUR) { + filter = {trainers:user._id} + } + const sessions = await Session.find({...filter}) + params[`filter.session`] = {$in: sessions} } if (model=='session') { @@ -654,6 +657,14 @@ const postCreate = async ({model, params, data}) => { {$addToSet: {homeworks:data._id}} ) } + + if(model == `ticket`) { + await HelpDeskConversation.create({ + ticket: data._id, + user: data.user, + block: data.block, + }) + } } setPostCreateData(postCreate) diff --git a/backend/web/server/plugins/aftral-lms/schemas/HelpDeskConversationSchema.js b/backend/web/server/plugins/aftral-lms/schemas/HelpDeskConversationSchema.js new file mode 100644 index 0000000000..dd4daf91b9 --- /dev/null +++ b/backend/web/server/plugins/aftral-lms/schemas/HelpDeskConversationSchema.js @@ -0,0 +1,22 @@ +const mongoose = require('mongoose') +const {schemaOptions} = require('../../../utils/schemas') +const Schema = mongoose.Schema + +const HelpDeskConversationSchema = new Schema({ + user: { //trainee, trainer, conceptor + type: Schema.Types.ObjectId, + ref: `user`, + }, + ticket: { + type: Schema.Types.ObjectId, + ref: `ticket`, + }, + block: { + type: Schema.Types.ObjectId, + ref: `block`, + required: false, + }, +}, +schemaOptions) + +module.exports=HelpDeskConversationSchema diff --git a/backend/web/server/plugins/aftral-lms/schemas/MessageSchema.js b/backend/web/server/plugins/aftral-lms/schemas/MessageSchema.js index 14b77f9d2e..78007827f3 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/MessageSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/MessageSchema.js @@ -17,7 +17,7 @@ const MessageSchema = new Schema({ receiver: { type: Schema.Types.ObjectId, ref: 'user', - required: true, + required: false, }, sender: { type: Schema.Types.ObjectId, @@ -28,6 +28,11 @@ const MessageSchema = new Schema({ type: Boolean, required: false, default: false, + }, + conversation: { + type: Schema.Types.ObjectId, + ref: `conversation`, + required: [true, `La conversation est obligatoire`] } }, schemaOptions) diff --git a/backend/web/server/plugins/aftral-lms/schemas/SessionConversationSchema.js b/backend/web/server/plugins/aftral-lms/schemas/SessionConversationSchema.js new file mode 100644 index 0000000000..3d83c840e9 --- /dev/null +++ b/backend/web/server/plugins/aftral-lms/schemas/SessionConversationSchema.js @@ -0,0 +1,17 @@ +const mongoose = require('mongoose') +const {schemaOptions} = require('../../../utils/schemas') +const Schema = mongoose.Schema + +const SessionConversationSchema = new Schema({ + trainee: { //trainee, trainer, conceptor + type: Schema.Types.ObjectId, + ref: `user`, + }, + session: { + type: Schema.Types.ObjectId, + ref: `ticket`, + } +}, +schemaOptions) + +module.exports=SessionConversationSchema