Commit 59c9ae5b authored by Christopher Fullarton's avatar Christopher Fullarton

Deletes the object id if existing when adding new quizzes

parent 8f93b539
......@@ -139,6 +139,7 @@ class QuizDAO extends AbstractDAO {
public async addQuiz(quizDoc: IQuiz): Promise<Document & QuizModelItem> {
await AMQPConnector.channel.assertExchange(AMQPConnector.buildQuizExchange(quizDoc.name), 'fanout');
delete quizDoc._id;
return QuizModel.create(quizDoc);
}
......
......@@ -29,7 +29,6 @@ export class ExpiryQuizRouter extends AbstractRouter {
const expiryQuizzes = await QuizDAO.getExpiryQuizzes();
const baseQuiz: any = expiryQuizzes.find(val => val.name === quizname);
baseQuiz.name = baseQuiz.name + (expiryQuizzes.length + 1);
delete baseQuiz._id;
const doc = await QuizDAO.addQuiz(baseQuiz);
......
......@@ -175,16 +175,17 @@ export class QuizRouter extends AbstractRouter {
): Promise<IMessage> {
const duplicateQuizzes = [];
const quizData = [];
const savedQuizzes = [];
const uploadedQuizzes = [];
uploadedFiles.forEach(file => {
quizData.push({
uploadedQuizzes.push({
fileName: file.originalname,
quiz: QuizDAO.convertLegacyQuiz(JSON.parse(file.buffer.toString('UTF-8'))),
});
});
await asyncForEach(quizData, async (data: { fileName: string, quiz: QuizModelItem }) => {
await asyncForEach(uploadedQuizzes, async (data: { fileName: string, quiz: QuizModelItem }) => {
const existingQuiz = await QuizDAO.getQuizByName(data.quiz.name);
if (existingQuiz) {
duplicateQuizzes.push({
......@@ -196,7 +197,9 @@ export class QuizRouter extends AbstractRouter {
data.quiz.privateKey = privateKey;
data.quiz.visibility = QuizVisibility.Account;
await QuizDAO.addQuiz(data.quiz);
savedQuizzes.push((
await QuizDAO.addQuiz(data.quiz)
).toJSON());
}
});
......@@ -205,7 +208,7 @@ export class QuizRouter extends AbstractRouter {
step: MessageProtocol.UploadFile,
payload: {
duplicateQuizzes,
quizData: quizData.filter(insertedQuiz => !duplicateQuizzes.find(duplicateQuiz => duplicateQuiz.fileName === insertedQuiz.fileName)),
quizData: savedQuizzes,
},
};
}
......@@ -743,7 +746,6 @@ export class QuizRouter extends AbstractRouter {
throw new NotFoundError('Quiz name not found');
}
delete quiz.id;
quiz.name = await QuizDAO.getRenameAsToken(quiz.name);
quiz.privateKey = privateKey;
quiz.state = QuizState.Active;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment