diff --git a/backend/web/server/plugins/sosynpl/functions.js b/backend/web/server/plugins/sosynpl/functions.js index 547b2dc959..3120a0526f 100644 --- a/backend/web/server/plugins/sosynpl/functions.js +++ b/backend/web/server/plugins/sosynpl/functions.js @@ -513,6 +513,13 @@ declareVirtualField({model: 'mission', field: 'evaluation', instance: 'Array', m options: { ref: 'evaluation' } }, }) +//Announce Questions +declareVirtualField({model: 'announce', field: 'questions', instance: 'Array', multiple: true, + caster :{ + instance: 'ObjectID', + options: { ref: 'question' } + } +}) const soSynplRegister = props => { console.log(`Register with ${JSON.stringify(props)}`) @@ -618,6 +625,8 @@ const preProcessGet = async ({ model, fields, id, user, params }) => { id = s._id // } } + //If no Id when looking for questions, it means we're looking for FAQ and not all announces' questions + if (model == 'question' && !id) params['filter.announce'] = null return { model, fields, id, user, params } } diff --git a/backend/web/server/plugins/sosynpl/schemas/AnnounceSchema.js b/backend/web/server/plugins/sosynpl/schemas/AnnounceSchema.js index 3756828ad0..8f6e139170 100644 --- a/backend/web/server/plugins/sosynpl/schemas/AnnounceSchema.js +++ b/backend/web/server/plugins/sosynpl/schemas/AnnounceSchema.js @@ -369,4 +369,10 @@ AnnounceSchema.virtual('_duration_days', DUMMY_REF).get(function() { return this.duration*DURATION_UNIT_DAYS[this.duration_unit] }) +AnnounceSchema.virtual('questions', { + ref: 'question', + localField: '_id', + foreignField: 'announce', +}) + module.exports = AnnounceSchema diff --git a/backend/web/server/plugins/sosynpl/schemas/QuestionSchema.js b/backend/web/server/plugins/sosynpl/schemas/QuestionSchema.js new file mode 100644 index 0000000000..192338c644 --- /dev/null +++ b/backend/web/server/plugins/sosynpl/schemas/QuestionSchema.js @@ -0,0 +1,25 @@ +const mongoose = require('mongoose') +const { schemaOptions } = require('../../../utils/schemas') +const Schema = mongoose.Schema + +const QuestionSchema = new Schema({ + title: { + type: String, + required: [true, `Le titre est obligatoire`], + }, + answer: { + type: String, + required: false, + }, + tag: { + type: String, + required: false, + }, + announce: { + type: Schema.Types.ObjectId, + ref: 'announce', + required: false, + } +}, { ...schemaOptions }) + +module.exports = QuestionSchema \ No newline at end of file