From 0605f9c91fd7fdd8d3b9bd3d5142e0a2dcac2853 Mon Sep 17 00:00:00 2001 From: SeghirOumo Date: Mon, 29 Jul 2024 14:14:15 +0200 Subject: [PATCH] Sosynpl[#155]: added creator to question --- .../web/server/plugins/sosynpl/functions.js | 1 + .../plugins/sosynpl/schemas/QuestionSchema.js | 5 +++ backend/web/tests/sosynpl/question.test.js | 31 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 backend/web/tests/sosynpl/question.test.js diff --git a/backend/web/server/plugins/sosynpl/functions.js b/backend/web/server/plugins/sosynpl/functions.js index cc71ddb0f2..99b4b777d2 100644 --- a/backend/web/server/plugins/sosynpl/functions.js +++ b/backend/web/server/plugins/sosynpl/functions.js @@ -625,6 +625,7 @@ const preProcessGet = async ({ model, fields, id, user, params }) => { id = s._id // } } + if (model == 'question') params.creator=user //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/QuestionSchema.js b/backend/web/server/plugins/sosynpl/schemas/QuestionSchema.js index 192338c644..ae1f915c24 100644 --- a/backend/web/server/plugins/sosynpl/schemas/QuestionSchema.js +++ b/backend/web/server/plugins/sosynpl/schemas/QuestionSchema.js @@ -3,6 +3,11 @@ const { schemaOptions } = require('../../../utils/schemas') const Schema = mongoose.Schema const QuestionSchema = new Schema({ + creator: { + type: Schema.Types.ObjectId, + ref: 'customerFreelance', + required: [true, `L'auteur est obligatoire`] + }, title: { type: String, required: [true, `Le titre est obligatoire`], diff --git a/backend/web/tests/sosynpl/question.test.js b/backend/web/tests/sosynpl/question.test.js new file mode 100644 index 0000000000..aacc2bde0e --- /dev/null +++ b/backend/web/tests/sosynpl/question.test.js @@ -0,0 +1,31 @@ +const mongoose = require('mongoose') +const moment = require('moment') +const { MONGOOSE_OPTIONS, loadFromDb } = require('../../server/utils/database') +const CustomerFreelance = require('../../server/models/CustomerFreelance') +const Question = require('../../server/models/Question') +const { CUSTOMER_DATA } = require('./data/base_data') +require('../../server/plugins/sosynpl/functions') +require('../../server/models/Sector') +require('../../server/models/JobFile') +require('../../server/models/Job') + +describe('Questions', () => { + + beforeAll(async () => { + const DBNAME = `test${moment().unix()}` + await mongoose.connect(`mongodb://localhost/${DBNAME}`, MONGOOSE_OPTIONS) + }) + + afterAll(async () => { + await mongoose.connection.dropDatabase() + await mongoose.connection.close() + }) + + it('must get question creator', async() => { + await CustomerFreelance.create({...CUSTOMER_DATA}) + const user = await CustomerFreelance.findOne({}) + await Question.create({creator: user._id, title:'Test'}) + const [question] = await loadFromDb({model:'question', user, fields:['creator.fullname','title']}) + expect(question.creator.fullname).toEqual(user.fullname) + }) +}) \ No newline at end of file