diff --git a/apps/api/src/questions/questions.service.ts b/apps/api/src/questions/questions.service.ts index 8fa797723..0d092e077 100644 --- a/apps/api/src/questions/questions.service.ts +++ b/apps/api/src/questions/questions.service.ts @@ -108,16 +108,15 @@ export class QuestionsService { trx, ); - const answerList = answerQuestion.answer.map((a) => { - if (typeof a !== "string") { - return a.value; + const answerList = answerQuestion.answer.map((answerElement) => { + if (typeof answerElement !== "string") { + return answerElement.value; } - return a; + return answerElement; }); - if (!answerList || answerList.length === 0) - throw new NotFoundException("User answers not found"); + if (!(answerList?.length === 0)) throw new NotFoundException("User answers not found"); const answers: { answer: string }[] = await this.questionsRepository.getQuestionAnswers( answerQuestion.questionId, diff --git a/apps/web/app/modules/Courses/Lesson/LessonItems/FillInTheBlanks/FillInTheBlanks.tsx b/apps/web/app/modules/Courses/Lesson/LessonItems/FillInTheBlanks/FillInTheBlanks.tsx index 2ec85a3ef..129119b63 100644 --- a/apps/web/app/modules/Courses/Lesson/LessonItems/FillInTheBlanks/FillInTheBlanks.tsx +++ b/apps/web/app/modules/Courses/Lesson/LessonItems/FillInTheBlanks/FillInTheBlanks.tsx @@ -4,6 +4,7 @@ import Viewer from "~/components/RichText/Viever"; import { Card } from "~/components/ui/card"; import { FillInTheTextBlanks } from "~/modules/Courses/Lesson/LessonItems/FillInTheBlanks/FillInTheTextBlanks"; import { TextBlank } from "~/modules/Courses/Lesson/LessonItems/FillInTheBlanks/TextBlank"; +import { handleCompletionForMediaLesson } from "~/utils/handleCompletionForMediaLesson"; type Answer = { id: string; @@ -75,7 +76,10 @@ export const FillInTheBlanks = ({ if (sortedWords.length > 0 && sortedWords.length <= maxAnswersAmount) { sendAnswer(sortedWords); - if (!isCompleted && !isQuiz && sortedWords.length === maxAnswersAmount) { + if ( + handleCompletionForMediaLesson(isCompleted, isQuiz) && + sortedWords.length === maxAnswersAmount + ) { updateLessonItemCompletion(lessonItemId); } } diff --git a/apps/web/app/modules/Courses/Lesson/LessonItems/FillInTheBlanks/dnd/FillInTheBlanksDnd.tsx b/apps/web/app/modules/Courses/Lesson/LessonItems/FillInTheBlanks/dnd/FillInTheBlanksDnd.tsx index 30c0981b5..b12bc39b2 100644 --- a/apps/web/app/modules/Courses/Lesson/LessonItems/FillInTheBlanks/dnd/FillInTheBlanksDnd.tsx +++ b/apps/web/app/modules/Courses/Lesson/LessonItems/FillInTheBlanks/dnd/FillInTheBlanksDnd.tsx @@ -14,6 +14,7 @@ import { arrayMove, sortableKeyboardCoordinates } from "@dnd-kit/sortable"; import { type FC, useEffect, useState } from "react"; import Viewer from "~/components/RichText/Viever"; +import { handleCompletionForMediaLesson } from "~/utils/handleCompletionForMediaLesson"; import { DndBlank } from "./DndBlank"; import { DraggableWord } from "./DraggableWord"; @@ -115,6 +116,9 @@ export const FillInTheBlanksDnd: FC = ({ }); }; + const handleCompletion = () => + handleCompletionForMediaLesson(isCompleted, isQuiz) && updateLessonItemCompletion(lessonItemId); + function handleDragEnd(event: DragEndEvent) { const { active, over } = event; @@ -190,9 +194,7 @@ export const FillInTheBlanksDnd: FC = ({ const sortedWords = filteredWords.sort((a, b) => a.index - b.index); if (sortedWords.length > 0 && sortedWords.length <= maxAnswersAmount) { sendAnswer(sortedWords); - if (!isCompleted && !isQuiz && sortedWords.length === maxAnswersAmount) { - updateLessonItemCompletion(lessonItemId); - } + handleCompletion(); } } @@ -213,9 +215,7 @@ export const FillInTheBlanksDnd: FC = ({ const sortedWords = filteredWords.sort((a, b) => a.index - b.index); if (sortedWords.length > 0 && sortedWords.length <= maxAnswersAmount) { sendAnswer(sortedWords); - if (!isCompleted && !isQuiz && sortedWords.length === maxAnswersAmount) { - updateLessonItemCompletion(lessonItemId); - } + handleCompletion(); } } diff --git a/apps/web/app/modules/Courses/Lesson/LessonItems/Question/Question.tsx b/apps/web/app/modules/Courses/Lesson/LessonItems/Question/Question.tsx index 0cf47b060..6919c6945 100644 --- a/apps/web/app/modules/Courses/Lesson/LessonItems/Question/Question.tsx +++ b/apps/web/app/modules/Courses/Lesson/LessonItems/Question/Question.tsx @@ -5,6 +5,7 @@ import { useFormContext } from "react-hook-form"; import { Textarea } from "~/components/ui/textarea"; import { useUserRole } from "~/hooks/useUserRole"; import { cn } from "~/lib/utils"; +import { handleCompletionForMediaLesson } from "~/utils/handleCompletionForMediaLesson"; import { FillInTheBlanksDnd } from "../FillInTheBlanks/dnd/FillInTheBlanksDnd"; import { FillInTheBlanks } from "../FillInTheBlanks/FillInTheBlanks"; @@ -71,11 +72,14 @@ export const Question = ({ } }, [isQuiz, isSubmitted]); + const handleCompletion = () => + handleCompletionForMediaLesson(isCompleted, isQuiz) && updateLessonItemCompletion(lessonItemId); + const handleClick = async (id: string) => { if (isSingleQuestion) { setSelectedOption([id]); await sendAnswer([id]); - !isCompleted && !isQuiz && updateLessonItemCompletion(lessonItemId); + handleCompletion(); } else { let newSelectedOptions: string[]; @@ -87,13 +91,13 @@ export const Question = ({ setSelectedOption(newSelectedOptions); await sendAnswer(newSelectedOptions); - !isCompleted && !isQuiz && updateLessonItemCompletion(lessonItemId); + handleCompletion(); } }; const handleOpenAnswerRequest = async (e: ChangeEvent) => { await sendOpenAnswer(e.target.value); - !isCompleted && !isQuiz && updateLessonItemCompletion(lessonItemId); + handleCompletion(); }; const canRenderCorrectAnswers = diff --git a/apps/web/app/utils/handleCompletionForMediaLesson.tsx b/apps/web/app/utils/handleCompletionForMediaLesson.tsx new file mode 100644 index 000000000..351854b3c --- /dev/null +++ b/apps/web/app/utils/handleCompletionForMediaLesson.tsx @@ -0,0 +1,3 @@ +export function handleCompletionForMediaLesson(isCompleted: boolean, isQuiz: boolean): boolean { + return !isCompleted && !isQuiz; +}