diff --git a/gameservice/game-service.js b/gameservice/game-service.js index 90f0f99b..4598d2cf 100644 --- a/gameservice/game-service.js +++ b/gameservice/game-service.js @@ -34,7 +34,7 @@ app.post('/addgame', async (req, res) => { // Guarda el nuevo juego en la base de datos const savedGame = await newGame.save(); - res.status(201).json(savedGame); + res.status(200).json(savedGame); } catch (error) { res.status(500).json({ error: 'Internal Server Error' }); } diff --git a/gameservice/game-service.test.js b/gameservice/game-service.test.js index a4953fca..37a333ed 100644 --- a/gameservice/game-service.test.js +++ b/gameservice/game-service.test.js @@ -20,8 +20,19 @@ afterAll(async () => { describe('Game Service', () => { it('should add a new game on POST /addgame', async () => { const newGame = { - user: mongoose.Types.ObjectId(), // ID de usuario simulado - questions: mongoose.Types.ObjectId(), // ID de pregunta simulado + user: 'testUser', + questions: [ + { + question: 'Mocked Question', + correct: 'Mocked Correct Answer', + incorrects: ['Mocked Option 1', 'Mocked Option 2'] + }, + { + question: 'Mocked Question2', + correct: 'Mocked Correct Answer2', + incorrects: ['Mocked Option 1', 'Mocked Option 2'] + } + ], answers: [ { response: 'User response', @@ -32,7 +43,7 @@ describe('Game Service', () => { }; const response = await request(app).post('/addgame').send(newGame); - expect(response.status).toBe(201); + expect(response.status).toBe(200); expect(response.body).toHaveProperty('user', newGame.user.toString()); }); }); diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index 73547264..cfef67d6 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -20,9 +20,9 @@ describe('Gateway Service', () => { }else if (url.endsWith('/addquestion')) { return Promise.resolve({ data: { - question: 'What is the capital of France?', - options: ['Paris', 'Berlin', 'Madrid', 'Rome'], - correctOptionIndex: 0 + question: 'Mocked Question', + correct: 'Mocked Correct Answer', + incorrects: ['Mocked Option 1', 'Mocked Option 2'] } }); } @@ -100,9 +100,9 @@ describe('Gateway Service', () => { const response = await request(app) .post('/addquestion') .send({ - question: 'What is the capital of France?', - options: ['Paris', 'Berlin', 'Madrid', 'Rome'], - correctOptionIndex: 0, + question: 'Mocked Question', + correct: 'Mocked Correct Answer', + incorrects: ['Mocked Option 1', 'Mocked Option 2'] }); expect(response.statusCode).toBe(200); diff --git a/questionservice/question-model.js b/questionservice/question-model.js index 89bf65ed..5e3e72aa 100644 --- a/questionservice/question-model.js +++ b/questionservice/question-model.js @@ -5,28 +5,22 @@ const questionSchema = new mongoose.Schema({ type: String, required: true, }, - options: { + correct: { + type: String, + required: true, + }, + incorrects: { type: [String], required: true, validate: { validator: function (options) { - return options.length >= 2 && options.length <= 4; + return options.length >= 1 && options.length <= 3; }, - message: 'Options must have between 2 and 4 elements.', - }, - }, - correctOptionIndex: { - type: Number, - required: true, - validate: { - validator: function (index) { - return index >= 0 && index < 4; // Max of 4 options - }, - message: 'Correct option index must be between 0 and 3.', + message: 'Options must be between 2 and 4 elements.', }, }, }); const Question = mongoose.model('Question', questionSchema); -module.exports = Question; +module.exports = Question; \ No newline at end of file diff --git a/questionservice/question-service.js b/questionservice/question-service.js index 8748e0a3..5977b501 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -19,21 +19,21 @@ const validateRequiredFields = (req, fields) => { // Ruta para agregar una nueva pregunta app.post('/addquestion', async (req, res) => { try { - validateRequiredFields(req, ['question', 'options', 'correctOptionIndex']); + validateRequiredFields(req, ['question', 'correct', 'incorrects']); - const { question, options, correctOptionIndex } = req.body; + const { question, correct, incorrects } = req.body; // Crea una nueva instancia del modelo de preguntas const newQuestion = new Question({ question, - options, - correctOptionIndex, + correct, + incorrects, }); // Guarda la nueva pregunta en la base de datos const savedQuestion = await newQuestion.save(); - res.status(201).json(savedQuestion); + res.status(200).json(savedQuestion); } catch (error) { res.status(500).json({ error: 'Internal Server Error' }); } diff --git a/questionservice/question-service.test.js b/questionservice/question-service.test.js index 980b79f6..dcac9805 100644 --- a/questionservice/question-service.test.js +++ b/questionservice/question-service.test.js @@ -19,13 +19,13 @@ afterAll(async () => { describe('Question Service', () => { it('should add a new question on POST /addquestion', async () => { const newQuestion = { - question: 'What is the capital of France?', - options: ['Paris', 'Berlin', 'Madrid', 'Rome'], - correctOptionIndex: 0, + question: 'Mocked Question', + correct: 'Mocked Correct Answer', + incorrects: ['Mocked Option 1', 'Mocked Option 2'] }; const response = await request(app).post('/addquestion').send(newQuestion); - expect(response.status).toBe(201); + expect(response.status).toBe(200); expect(response.body).toHaveProperty('question', 'What is the capital of France?'); });