Skip to content

Commit

Permalink
Aftral[premieroctet#120] fixed flex not descending in object, turned …
Browse files Browse the repository at this point in the history
…conversation to array
  • Loading branch information
SeghirOumo committed Aug 29, 2024
1 parent 42aafbf commit 997d474
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
13 changes: 12 additions & 1 deletion backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ CONVERSATION_MODELS.forEach(model => {
instance: 'ObjectID',
options: {ref: 'message'}},
})
declareVirtualField({model, field: 'messages_count', instance: 'Number',
caster: {
instance: 'ObjectID',
options: {ref: 'message'}},
})
declareVirtualField({model, field: 'newest_message', instance: 'Array', multiple: false,
caster: {
instance: 'ObjectID',
options: {ref: 'message'}},
})
})
// HelpDeskConversation end

Expand Down Expand Up @@ -253,6 +263,7 @@ const preCreate = async ({model, params, user}) => {
if(model=='message'){
params.sender=params.creator
params.receiver=params.parent
console.log(params)
const model = await getModel(params.parent, [`helpDeskConversation`,`sessionConversation`,`session`])
if(model == `session`) {
const value = user.role == ROLE_APPRENANT
Expand Down Expand Up @@ -688,7 +699,7 @@ const postCreate = async ({model, params, data}) => {
})
await Ticket.findOneAndUpdate(
{_id:data._id},
{conversation: conv._id}
{conversation: [conv._id]}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ const ConversationSchema = new Schema({
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
newest_message: {
type: mongoose.Schema.Types.ObjectId,
ref: 'message',
},
},schemaOptions)

ConversationSchema.virtual('messages', {
Expand All @@ -19,4 +15,21 @@ ConversationSchema.virtual('messages', {
foreignField: 'conversation',
})

ConversationSchema.virtual('messages_count', {
ref: 'message',
localField: '_id',
foreignField: 'conversation',
count:true,
})

ConversationSchema.virtual('newest_message', {
ref: 'message',
localField: '_id',
foreignField: 'conversation',
options: {
sort: { creation_date: -1 }, limit:1
},
justOne: true,
})

module.exports=ConversationSchema
6 changes: 3 additions & 3 deletions backend/web/server/plugins/aftral-lms/schemas/TicketSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ const TicketSchema = new Schema({
type: String,
required: false,
},
conversation: {
conversation: [{
type: Schema.Types.ObjectId,
ref: `helpDeskConversation`,
}
ref: `conversation`,
}],
}, {...schemaOptions})

/* eslint-disable prefer-arrow-callback */
Expand Down
4 changes: 2 additions & 2 deletions backend/web/tests/aftral-lms/ticket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe(`Ticket`, () => {
ticket: ticket._id
})

ticket.conversation = conversation._id
ticket.conversation = [conversation._id]
await ticket.save()

message = await Message.create({
Expand All @@ -74,6 +74,6 @@ describe(`Ticket`, () => {
model: `ticket`,
fields: [`conversation.messages`]
})
expect(data.conversation.messages[0].content).toEqual(`Test message`)
expect(data.conversation[0].messages[0].content).toEqual(`Test message`)
})
})

0 comments on commit 997d474

Please sign in to comment.