Commit 9448f4a4 authored by Christopher Mark Fullarton's avatar Christopher Mark Fullarton
Browse files

Waits now until the quiz is created in the db before returning an uploaded quiz

parent 84b3bd32
export async function asyncForEach(array, callback): Promise<void> {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
...@@ -33,6 +33,7 @@ import { QuizVisibility } from '../../enums/QuizVisibility'; ...@@ -33,6 +33,7 @@ import { QuizVisibility } from '../../enums/QuizVisibility';
import { ExcelWorkbook } from '../../export/ExcelWorkbook'; import { ExcelWorkbook } from '../../export/ExcelWorkbook';
import { IQuizStatusPayload } from '../../interfaces/IQuizStatusPayload'; import { IQuizStatusPayload } from '../../interfaces/IQuizStatusPayload';
import { IQuizEntity, IQuizSerialized } from '../../interfaces/quizzes/IQuizEntity'; import { IQuizEntity, IQuizSerialized } from '../../interfaces/quizzes/IQuizEntity';
import { asyncForEach } from '../../lib/async-for-each';
import { MatchTextToAssetsDb } from '../../lib/cache/assets'; import { MatchTextToAssetsDb } from '../../lib/cache/assets';
import { Leaderboard } from '../../lib/leaderboard/leaderboard'; import { Leaderboard } from '../../lib/leaderboard/leaderboard';
import { QuizModel } from '../../models/quiz/QuizModelItem'; import { QuizModel } from '../../models/quiz/QuizModelItem';
...@@ -140,10 +141,10 @@ export class QuizRouter extends AbstractRouter { ...@@ -140,10 +141,10 @@ export class QuizRouter extends AbstractRouter {
} }
@Post('/upload') @Post('/upload')
public uploadQuiz( public async uploadQuiz(
@HeaderParam('authorization') privateKey: string, // @HeaderParam('authorization') privateKey: string, //
@UploadedFiles('uploadFiles[]') uploadedFiles: any, // @UploadedFiles('uploadFiles[]') uploadedFiles: any, //
): object { ): Promise<object> {
const duplicateQuizzes = []; const duplicateQuizzes = [];
const quizData = []; const quizData = [];
...@@ -155,7 +156,7 @@ export class QuizRouter extends AbstractRouter { ...@@ -155,7 +156,7 @@ export class QuizRouter extends AbstractRouter {
}); });
}); });
quizData.forEach((data: { fileName: string, quiz: IQuizEntity }) => { await asyncForEach(quizData, async (data: { fileName: string, quiz: IQuizEntity }) => {
const existingQuiz = QuizDAO.getQuizByName(data.quiz.name); const existingQuiz = QuizDAO.getQuizByName(data.quiz.name);
if (existingQuiz) { if (existingQuiz) {
duplicateQuizzes.push({ duplicateQuizzes.push({
...@@ -174,7 +175,7 @@ export class QuizRouter extends AbstractRouter { ...@@ -174,7 +175,7 @@ export class QuizRouter extends AbstractRouter {
throw result; throw result;
} }
quizValidator.save(); await quizValidator.save();
} }
}); });
......
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