Skip to content

Commit

Permalink
Aftral[premieroctet#120]: filter on ticket when user isn't conceptor …
Browse files Browse the repository at this point in the history
…or helpdesk
  • Loading branch information
SeghirOumo committed Aug 30, 2024
1 parent bd7eb13 commit f138e7e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
14 changes: 6 additions & 8 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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
}

Expand All @@ -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})
}

Expand Down
49 changes: 43 additions & 6 deletions backend/web/tests/aftral-lms/ticket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
})
Expand All @@ -49,31 +57,60 @@ 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() => {
await mongoose.connection.dropDatabase()
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)
})
})

0 comments on commit f138e7e

Please sign in to comment.