diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index ce9b70c6e2..08e1d94df0 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -183,6 +183,12 @@ declareComputedField({model: 'post', field: 'liked', getterFn: isLiked, requires // Ticket start declareEnumField({model:'ticket', field: 'status', instance: 'String', enumValues: TICKET_STATUS}) declareEnumField({model:'ticket', field: 'tag', instance: 'String', enumValues: TICKET_TAG}) +// declareVirtualField({model:`ticket`, field: `conversation`, instance: `Array`, multiple: false, +// caster: { +// instance: `ObjectID`, +// options: {ref: `ticket`} +// } +// }) // Ticket End // Permission start @@ -201,6 +207,17 @@ declareVirtualField({model: `group`, field: `available_trainees_count`, instance declareVirtualField({model: `group`, field: `excluded_trainees_count`, instance: `Number`, requires: `trainees,available_trainees`}) // Group end +// HelpDeskConversation start +const CONVERSATION_MODELS = [`helpDeskConversation`, `sessionConversation`, `conversation`] +CONVERSATION_MODELS.forEach(model => { + declareVirtualField({model, field: 'messages', instance: 'Array', multiple: true, + caster: { + instance: 'ObjectID', + options: {ref: 'message'}}, + }) +}) +// HelpDeskConversation end + const preCreate = async ({model, params, user}) => { params.creator=params.creator || user._id params.last_updater=user._id diff --git a/backend/web/server/plugins/aftral-lms/schemas/ConversationSchema.js b/backend/web/server/plugins/aftral-lms/schemas/ConversationSchema.js index 95ac38183c..6ddda48e19 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/ConversationSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/ConversationSchema.js @@ -7,15 +7,16 @@ const ConversationSchema = new Schema({ type: mongoose.Schema.Types.ObjectId, ref: 'user', }, - messages: [{ - type: mongoose.Schema.Types.ObjectId, - ref: 'message', - }], newest_message: { type: mongoose.Schema.Types.ObjectId, ref: 'message', }, -}, -schemaOptions) +},schemaOptions) + +ConversationSchema.virtual('messages', { + ref: 'message', + localField: '_id', + foreignField: 'conversation', +}) module.exports=ConversationSchema diff --git a/backend/web/server/plugins/aftral-lms/schemas/HelpDeskConversationSchema.js b/backend/web/server/plugins/aftral-lms/schemas/HelpDeskConversationSchema.js index dd4daf91b9..f5e0f701b1 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/HelpDeskConversationSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/HelpDeskConversationSchema.js @@ -6,10 +6,12 @@ const HelpDeskConversationSchema = new Schema({ user: { //trainee, trainer, conceptor type: Schema.Types.ObjectId, ref: `user`, + required: [true, `L'utilisateur est obligatoire`] }, ticket: { type: Schema.Types.ObjectId, ref: `ticket`, + required: [true, `Le ticket est obligatoire`] }, block: { type: Schema.Types.ObjectId, diff --git a/backend/web/server/plugins/aftral-lms/schemas/SessionConversationSchema.js b/backend/web/server/plugins/aftral-lms/schemas/SessionConversationSchema.js index 3d83c840e9..95449033de 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/SessionConversationSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/SessionConversationSchema.js @@ -7,10 +7,10 @@ const SessionConversationSchema = new Schema({ type: Schema.Types.ObjectId, ref: `user`, }, - session: { + group: { type: Schema.Types.ObjectId, - ref: `ticket`, - } + ref: `group`, + }, }, schemaOptions) diff --git a/backend/web/tests/aftral-lms/block.test.js b/backend/web/tests/aftral-lms/block.test.js index 5aaf4147e1..b506ebd222 100644 --- a/backend/web/tests/aftral-lms/block.test.js +++ b/backend/web/tests/aftral-lms/block.test.js @@ -138,7 +138,6 @@ describe('User', () => { }) it('must return homeworks submitted count', async() => { - const [data] = await loadFromDb({model:`block`, user:conceptor, id, fields:[`session`,`homeworks_missing_count`,`homeworks`,`homeworks_submitted_count`,`trainees_count`]}) expect(data.homeworks_missing_count).toEqual(0) expect(data.homeworks_submitted_count).toEqual(2) diff --git a/backend/web/tests/aftral-lms/ticket.test.js b/backend/web/tests/aftral-lms/ticket.test.js new file mode 100644 index 0000000000..f264f8516f --- /dev/null +++ b/backend/web/tests/aftral-lms/ticket.test.js @@ -0,0 +1,79 @@ +const mongoose = require('mongoose') +const { MONGOOSE_OPTIONS, loadFromDb, idEqual } = 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') +require('../../server/models/Certification') +require('../../server/models/Permission') +require('../../server/models/PermissionGroup') +const { ROLE_APPRENANT, ROLE_FORMATEUR, RESOURCE_TYPE_PDF, ACHIEVEMENT_RULE_CHECK, ACHIEVEMENT_RULE_SUCCESS, ACHIEVEMENT_RULE_CONSULT, RESOURCE_TYPE_VIDEO, ACHIEVEMENT_RULE_DOWNLOAD, ROLE_CONCEPTEUR, BLOCK_STATUS_CURRENT, BLOCK_STATUS_FINISHED, BLOCK_STATUS_UNAVAILABLE, ACHIEVEMENT_RULE, ROLE_HELPDESK } = require('../../server/plugins/aftral-lms/consts') +const Block = require('../../server/models/Block') +const Homework = require('../../server/models/Homework') +const Progress = require('../../server/models/Progress') +const ProductCode = require('../../server/models/ProductCode') +const { addChildAction } = require('../../server/plugins/aftral-lms/actions') +const HelpDeskConversation = require('../../server/models/HelpDeskConversation') +const Ticket = require('../../server/models/Ticket') +const Message = require('../../server/models/Message') +require('../../server/models/Feed') +require('../../server/models/Certification') + +describe(`Ticket`, () => { + let user, ticket, conversation, message, helpDesk + beforeAll(async() => { + await mongoose.connect(`mongodb://localhost/aftral-test`, MONGOOSE_OPTIONS) + + user = await User.create({ + firstname: `John`, + lastname: `Doe`, + role: ROLE_CONCEPTEUR, + password: `Test`, + email: `t@t.com`, + }) + + helpDesk = await User.create({ + firstname: `Jeanette`, + lastname: `Doe`, + role: ROLE_HELPDESK, + password: `Test`, + email: `t@t.com`, + }) + + ticket = await Ticket.create({ + user: user._id, + content: `content`, + title: `Test ticket` + }) + + conversation = await HelpDeskConversation.create({ + user: user._id, + ticket: ticket._id + }) + + ticket.conversation = conversation._id + await ticket.save() + + message = await Message.create({ + conversation: conversation._id, + sender: user._id, + content: `Test message` + }) + }) + + afterAll(async() => { + await mongoose.connection.dropDatabase() + await mongoose.connection.close() + }) + + it(`must return ticket conversation messages`, async() => { + const [data] = await loadFromDb({ + model: `ticket`, + fields: [`conversation.messages`] + }) + expect(data.conversation.messages[0].content).toEqual(`Test message`) + }) +}) \ No newline at end of file