From f138e7e66cd162e6234d1bfd0d0aa2f4f44d5352 Mon Sep 17 00:00:00 2001 From: SeghirOumo Date: Fri, 30 Aug 2024 15:54:36 +0200 Subject: [PATCH] Aftral[#120]: filter on ticket when user isn't conceptor or helpdesk --- .../server/plugins/aftral-lms/functions.js | 14 +++--- backend/web/tests/aftral-lms/ticket.test.js | 49 ++++++++++++++++--- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index 2b336b9035..aff2a36e9c 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -183,13 +183,7 @@ 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 + // Ticket end // Permission start declareEnumField({model:'permission', field: 'value', instance: 'String', enumValues: PERMISSIONS}) @@ -516,7 +510,7 @@ const preprocessGet = async ({model, fields, id, user, params}) => { return computeStatistics({model, fields, id, user, params}) .then(data => ({data})) } - if (model == `helpDeskConversation` && !id && user.role !== ROLE_HELPDESK) { + if (model == `helpDeskConversation` && !id && ![ROLE_HELPDESK, ROLE_CONCEPTEUR].includes(user.role)) { params['filter.user']=user } @@ -531,6 +525,10 @@ const preprocessGet = async ({model, fields, id, user, params}) => { params[`filter.creator`] = user._id id=undefined } + + if (model == `ticket` && ![ROLE_CONCEPTEUR, ROLE_HELPDESK].includes(user.role)) { + params[`filter.user`] = user._id + } return Promise.resolve({model, fields, id, user, params}) } diff --git a/backend/web/tests/aftral-lms/ticket.test.js b/backend/web/tests/aftral-lms/ticket.test.js index ca23c91d4f..08f59a9562 100644 --- a/backend/web/tests/aftral-lms/ticket.test.js +++ b/backend/web/tests/aftral-lms/ticket.test.js @@ -23,14 +23,22 @@ require('../../server/models/Feed') require('../../server/models/Certification') describe(`Ticket`, () => { - let user, ticket, conversation, message, helpDesk + let user, ticket, conversation, message, helpDesk, user2, ticket2, conversation2, message2 beforeAll(async() => { await mongoose.connect(`mongodb://localhost/aftral-test`, MONGOOSE_OPTIONS) user = await User.create({ firstname: `John`, lastname: `Doe`, - role: ROLE_CONCEPTEUR, + role: ROLE_APPRENANT, + password: `Test`, + email: `t@t.com`, + }) + + user2 = await User.create({ + firstname: `Jonn`, + lastname: `Doe`, + role: ROLE_APPRENANT, password: `Test`, email: `t@t.com`, }) @@ -49,19 +57,38 @@ describe(`Ticket`, () => { title: `Test ticket` }) + ticket2 = await Ticket.create({ + user: user2._id, + content: `content2`, + title: `Test ticket2` + }) + conversation = await HelpDeskConversation.create({ user: user._id, ticket: ticket._id }) + + conversation2 = await HelpDeskConversation.create({ + user: user2._id, + ticket: ticket2._id + }) ticket.conversation = [conversation._id] + ticket2.conversation = [conversation2._id] await ticket.save() + await ticket2.save() message = await Message.create({ conversation: conversation._id, sender: user._id, content: `Test message` }) + + message2 = await Message.create({ + conversation: conversation2._id, + sender: user2._id, + content: `Test message 2` + }) }) afterAll(async() => { @@ -69,11 +96,21 @@ describe(`Ticket`, () => { await mongoose.connection.close() }) - it(`must return ticket conversation messages`, async() => { - const [data] = await loadFromDb({ + it(`must return all ticket conversation for helpDesk`, async() => { + const data = await loadFromDb({ + model: `ticket`, + fields: [`conversation.messages`], + user: helpDesk + }) + expect(data.length).toEqual(2) + }) + + it(`must return my tickets if not concepteur or helpDesk`, async () => { + const data = await loadFromDb({ model: `ticket`, - fields: [`conversation.messages`] + fields: [`conversation.messages`], + user: user }) - expect(data.conversation[0].messages[0].content).toEqual(`Test message`) + expect(data.length).toEqual(1) }) }) \ No newline at end of file