He creado un esquema de mangosta para cada detalle requerido. Puedes recibir ayuda de él. He analizado un poco sus requisitos y he agregado modelos para muchos esquemas, primer esquema de preguntas, exportado como modelo
import { Schema } from 'mongoose';
import { AnswerOptionSchema } from './answer-option-schema';
const mongoose = require('mongoose');
export const QuestionSchema: Schema = new Schema({
question: {
type: String,
minlength: 10,
maxlength: 1000,
},
answerOptions: {
type: [AnswerOptionSchema],
default: undefined,
validate: {
validator: function(value: any) {
return value && value.length === 4;
},
message: 'Answer options should be 4.'
}
}
}, {
timestamps: true
});
export const Question = mongoose.model('Question', QuestionSchema);
y aquí en el QuestionSchema
, he incrustado un AnswerOptionSchema
como
import { Schema } from 'mongoose';
export const AnswerOptionSchema: Schema = new Schema({
optionNumber: {
type: Number
},
answerBody: {
type: String,
minlength: 1,
maxlength: 200,
},
isCorrectAnswer: { // you can store the correct answer with question id in another model.
type: Boolean,
default: false
}
}, {
_id: false
});
Con la ayuda de estos esquemas, he creado un QuestionSetSchema
para agregar un conjunto de esquemas de preguntas como
import { Schema } from "mongoose";
import { QuestionSchema } from "./question-schema";
const mongoose = require('mongoose');
export const QuestionSetSchema: Schema = new Schema({
questionSet: {
type: [QuestionSchema],
validate: {
validator: function(value: any) {
return value.length === 12;
},
message: 'Question set must be 12.'
}
},
}, {
timestamps: true
});
export const QuestionSet = mongoose.model('QuestionSet', QuestionSetSchema);
Ya preparado con la pregunta, las opciones de respuesta y el conjunto, ahora falta diseñar el esquema candidato,
import { Schema } from "mongoose";
const mongoose = require('mongoose');
export const CandidateSchema: Schema = new Schema({
name: String,
email: String, // you can store other candidate related information here.
totalAttempt: {
type: Number,
default: 0,
validate: {
validator: function(value: number) {
return value === 3;
},
message: 'You have already done three attempts.'
}
},
candidateQuestionAnswers: {
type: [Schema.Types.ObjectId],
ref: 'CandidateQuesAnswer'
}
}, {
timestamps: true
});
export const Candidate = mongoose.model('Candidate', CandidateSchema);
Aquí, notará, también estoy calculando el intento total del candidato y las respuestas para cada conjunto dado por él en el CandidateQuesAnswer
modelo. Este modelo tiene la estructura como
import { Schema } from "mongoose";
export const CandidateQuesAnswerSchema = new Schema({
candidate: {
type: Schema.Types.ObjectId,
ref: 'Candidate'
},
questionSet: {
type: Schema.Types.ObjectId,
ref: 'QuestionSet'
},
questionAnswers: {
type: [Number] // You can add answer schema
},
totalScore: {
type: Number
},
isPassed: {
type: Boolean,
default: false
}
}, {
timestamps: true
});
CandidateQuesAnswerSchema.pre('save', function updateTotalScore(next) {
// update total score of the candidate here based on the correct questionAnswers and
// questionSet.
next();
});
CandidateQuesAnswerSchema.pre('save', function updateIsPassed(next) {
// update the isPassed based on the totalScore obtained by the candidate.
next();
});
export const CandidateQuesAnswer = mongoose.model('CandidateAnswer', CandidateQuesAnswerSchema);
Donde he usado pre save
ganchos proporcionados por mongoose
, antes de guardar el documento y calcular los valores para declarar el candidato aprobado o reprobado.