Skip to content

Commit

Permalink
Aftral[premieroctet#120 & premieroctet#121]: added help desk conversa…
Browse files Browse the repository at this point in the history
…tion and session conversation
  • Loading branch information
SeghirOumo committed Aug 27, 2024
1 parent c874ce7 commit 3eadf1c
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 35 deletions.
20 changes: 20 additions & 0 deletions backend/web/server/models/HelpDeskConversation.js
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions backend/web/server/models/SessionConversation.js
Original file line number Diff line number Diff line change
@@ -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
79 changes: 45 additions & 34 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const MessageSchema = new Schema({
receiver: {
type: Schema.Types.ObjectId,
ref: 'user',
required: true,
required: false,
},
sender: {
type: Schema.Types.ObjectId,
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3eadf1c

Please sign in to comment.