Prevented the bug where the application is crashing

This commit is contained in:
Tiago Ribeiro
2024-03-24 02:32:12 +00:00
parent 29b2c8b3b8
commit 22f2b43692
8 changed files with 86 additions and 68 deletions

View File

@@ -11,7 +11,7 @@ import {
import axios from "axios";
import {speakingReverseMarking, writingReverseMarking} from "./score";
export const evaluateWritingAnswer = async (exercise: WritingExercise, solution: UserSolution, id: string): Promise<object | undefined> => {
export const evaluateWritingAnswer = async (exercise: WritingExercise, solution: UserSolution, id: string): Promise<UserSolution | undefined> => {
const response = await axios.post<Evaluation>("/api/evaluate/writing", {
question: `${exercise.prompt} ${exercise.attachment ? exercise.attachment.description : ""}`.replaceAll("\n", ""),
answer: solution.solutions[0].solution.trim().replaceAll("\n", " "),
@@ -35,12 +35,16 @@ export const evaluateWritingAnswer = async (exercise: WritingExercise, solution:
return undefined;
};
export const evaluateSpeakingAnswer = async (exercise: SpeakingExercise | InteractiveSpeakingExercise, solution: UserSolution, id: string) => {
export const evaluateSpeakingAnswer = async (
exercise: SpeakingExercise | InteractiveSpeakingExercise,
solution: UserSolution,
id: string,
): Promise<UserSolution | undefined> => {
switch (exercise?.type) {
case "speaking":
return {...(await evaluateSpeakingExercise(exercise, exercise.id, solution, id)), id};
return {...(await evaluateSpeakingExercise(exercise, exercise.id, solution, id)), id} as UserSolution;
case "interactiveSpeaking":
return {...(await evaluateInteractiveSpeakingExercise(exercise.id, solution, id)), id};
return {...(await evaluateInteractiveSpeakingExercise(exercise.id, solution, id)), id} as UserSolution;
default:
return undefined;
}
@@ -51,7 +55,12 @@ export const downloadBlob = async (url: string): Promise<Buffer> => {
return Buffer.from(blobResponse.data, "binary");
};
const evaluateSpeakingExercise = async (exercise: SpeakingExercise, exerciseId: string, solution: UserSolution, id: string) => {
const evaluateSpeakingExercise = async (
exercise: SpeakingExercise,
exerciseId: string,
solution: UserSolution,
id: string,
): Promise<UserSolution | undefined> => {
const formData = new FormData();
const url = solution.solutions[0].solution.trim() as string;
@@ -92,7 +101,7 @@ const evaluateSpeakingExercise = async (exercise: SpeakingExercise, exerciseId:
return undefined;
};
const evaluateInteractiveSpeakingExercise = async (exerciseId: string, solution: UserSolution, id: string) => {
const evaluateInteractiveSpeakingExercise = async (exerciseId: string, solution: UserSolution, id: string): Promise<UserSolution | undefined> => {
const promiseParts = solution.solutions.map(async (x: {prompt: string; blob: string}) => {
const blob = await downloadBlob(x.blob);
if (!x.blob.startsWith("blob")) await axios.post("/api/storage/delete", {path: x.blob});