Updated the code to solve the double stats creation
This commit is contained in:
@@ -4,7 +4,7 @@ import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import axios, {AxiosResponse} from "axios";
|
||||
import formidable from "formidable-serverless";
|
||||
import {ref, uploadBytes} from "firebase/storage";
|
||||
import {getDownloadURL, ref, uploadBytes} from "firebase/storage";
|
||||
import fs from "fs";
|
||||
import {app, storage} from "@/firebase";
|
||||
import {doc, getDoc, getFirestore, setDoc} from "firebase/firestore";
|
||||
@@ -25,16 +25,17 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (err) console.log(err);
|
||||
|
||||
const audioFile = files.audio;
|
||||
const audioFileRef = ref(storage, `speaking_recordings/${(audioFile as any).path.replace("upload_", "")}`);
|
||||
const audioFileRef = ref(storage, `speaking_recordings/${fields.id}.wav`);
|
||||
|
||||
const binary = fs.readFileSync((audioFile as any).path).buffer;
|
||||
const snapshot = await uploadBytes(audioFileRef, binary);
|
||||
const url = await getDownloadURL(snapshot.ref);
|
||||
const path = snapshot.metadata.fullPath;
|
||||
|
||||
res.status(200).json(null);
|
||||
|
||||
console.log("🌱 - Still processing");
|
||||
const backendRequest = await evaluate({answers: [{question: fields.question, answer: snapshot.metadata.fullPath}]});
|
||||
fs.rmSync((audioFile as any).path);
|
||||
const backendRequest = await evaluate({answers: [{question: fields.question, answer: path}]});
|
||||
console.log("🌱 - Process complete");
|
||||
|
||||
const correspondingStat = (await getDoc(doc(db, "stats", fields.id))).data() as Stat;
|
||||
@@ -42,7 +43,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const solutions = correspondingStat.solutions.map((x) => ({
|
||||
...x,
|
||||
evaluation: backendRequest.data,
|
||||
solution: snapshot.metadata.fullPath,
|
||||
solution: url,
|
||||
}));
|
||||
|
||||
await setDoc(
|
||||
|
||||
Reference in New Issue
Block a user