/api/evaluate refactor
This commit is contained in:
@@ -6,12 +6,13 @@ import axios, {AxiosResponse} from "axios";
|
|||||||
import formidable from "formidable-serverless";
|
import formidable from "formidable-serverless";
|
||||||
import {ref, uploadBytes} from "firebase/storage";
|
import {ref, uploadBytes} from "firebase/storage";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import {app, storage} from "@/firebase";
|
import {storage} from "@/firebase";
|
||||||
import {doc, getDoc, getFirestore, setDoc} from "firebase/firestore";
|
import client from "@/lib/mongodb";
|
||||||
import {Stat} from "@/interfaces/user";
|
import {Stat} from "@/interfaces/user";
|
||||||
import {speakingReverseMarking} from "@/utils/score";
|
import {speakingReverseMarking} from "@/utils/score";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
function delay(ms: number) {
|
function delay(ms: number) {
|
||||||
@@ -53,18 +54,20 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const correspondingStat = await getCorrespondingStat(fields.id, 1);
|
const correspondingStat = await getCorrespondingStat(fields.id, 1);
|
||||||
|
|
||||||
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data, solution: uploadingAudios}));
|
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data, solution: uploadingAudios}));
|
||||||
await setDoc(
|
await db.collection("stats").updateOne(
|
||||||
doc(db, "stats", fields.id),
|
{ id: fields.id },
|
||||||
{
|
{
|
||||||
|
$set: {
|
||||||
solutions,
|
solutions,
|
||||||
score: {
|
score: {
|
||||||
correct: speakingReverseMarking[backendRequest.data.overall || 0] || 0,
|
correct: speakingReverseMarking[backendRequest.data.overall || 0] || 0,
|
||||||
missing: 0,
|
missing: 0,
|
||||||
total: 100,
|
total: 100,
|
||||||
},
|
},
|
||||||
isDisabled: false,
|
isDisabled: false
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{merge: true},
|
{ upsert: true }
|
||||||
);
|
);
|
||||||
console.log("🌱 - Updated the DB");
|
console.log("🌱 - Updated the DB");
|
||||||
});
|
});
|
||||||
@@ -72,9 +75,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
||||||
console.log(`🌱 - Try number ${index} - ${id}`);
|
console.log(`🌱 - Try number ${index} - ${id}`);
|
||||||
const correspondingStat = await getDoc(doc(db, "stats", id));
|
const correspondingStat = await db.collection("stats").findOne<Stat>({ id: id });
|
||||||
|
|
||||||
if (correspondingStat.exists()) return {...correspondingStat.data(), id} as Stat;
|
if (correspondingStat) return correspondingStat;
|
||||||
await delay(3 * 10000);
|
await delay(3 * 10000);
|
||||||
return getCorrespondingStat(id, index + 1);
|
return getCorrespondingStat(id, index + 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import axios, {AxiosResponse} from "axios";
|
|||||||
import formidable from "formidable-serverless";
|
import formidable from "formidable-serverless";
|
||||||
import {getDownloadURL, ref, uploadBytes} from "firebase/storage";
|
import {getDownloadURL, ref, uploadBytes} from "firebase/storage";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import {app, storage} from "@/firebase";
|
import {storage} from "@/firebase";
|
||||||
import {doc, getDoc, getFirestore, setDoc} from "firebase/firestore";
|
import client from "@/lib/mongodb";
|
||||||
import {Stat} from "@/interfaces/user";
|
import {Stat} from "@/interfaces/user";
|
||||||
import {speakingReverseMarking} from "@/utils/score";
|
import {speakingReverseMarking} from "@/utils/score";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
function delay(ms: number) {
|
function delay(ms: number) {
|
||||||
@@ -51,8 +51,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
solution: url,
|
solution: url,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
await setDoc(
|
await db.collection("stats").updateOne(
|
||||||
doc(db, "stats", fields.id),
|
{ id: fields.id },
|
||||||
{
|
{
|
||||||
solutions,
|
solutions,
|
||||||
score: {
|
score: {
|
||||||
@@ -62,7 +62,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
},
|
},
|
||||||
isDisabled: false,
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
{merge: true},
|
{upsert: true},
|
||||||
);
|
);
|
||||||
console.log("🌱 - Updated the DB");
|
console.log("🌱 - Updated the DB");
|
||||||
});
|
});
|
||||||
@@ -70,9 +70,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
||||||
console.log(`🌱 - Try number ${index} - ${id}`);
|
console.log(`🌱 - Try number ${index} - ${id}`);
|
||||||
const correspondingStat = await getDoc(doc(db, "stats", id));
|
const correspondingStat = await db.collection("stats").findOne<Stat>({ id: id });
|
||||||
|
|
||||||
if (correspondingStat.exists()) return {...correspondingStat.data(), id} as Stat;
|
if (correspondingStat) return correspondingStat;
|
||||||
await delay(3 * 10000);
|
await delay(3 * 10000);
|
||||||
return getCorrespondingStat(id, index + 1);
|
return getCorrespondingStat(id, index + 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
import type {NextApiRequest, NextApiResponse} from "next";
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
import {getFirestore, doc, getDoc, setDoc} from "firebase/firestore";
|
import client from "@/lib/mongodb";
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import axios, {AxiosResponse} from "axios";
|
import axios, {AxiosResponse} from "axios";
|
||||||
import {app} from "@/firebase";
|
|
||||||
import {Stat} from "@/interfaces/user";
|
import {Stat} from "@/interfaces/user";
|
||||||
import {writingReverseMarking} from "@/utils/score";
|
import {writingReverseMarking} from "@/utils/score";
|
||||||
|
|
||||||
@@ -19,7 +18,8 @@ function delay(ms: number) {
|
|||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -37,8 +37,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const correspondingStat = await getCorrespondingStat(req.body.id, 1);
|
const correspondingStat = await getCorrespondingStat(req.body.id, 1);
|
||||||
|
|
||||||
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data}));
|
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data}));
|
||||||
await setDoc(
|
await db.collection("stats").updateOne(
|
||||||
doc(db, "stats", (req.body as Body).id),
|
{ id: (req.body as Body).id},
|
||||||
{
|
{
|
||||||
solutions,
|
solutions,
|
||||||
score: {
|
score: {
|
||||||
@@ -48,16 +48,17 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
},
|
},
|
||||||
isDisabled: false,
|
isDisabled: false,
|
||||||
},
|
},
|
||||||
{merge: true},
|
{upsert: true},
|
||||||
);
|
);
|
||||||
console.log("🌱 - Updated the DB");
|
console.log("🌱 - Updated the DB");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
||||||
console.log(`🌱 - Try number ${index} - ${id}`);
|
console.log(`🌱 - Try number ${index} - ${id}`);
|
||||||
const correspondingStat = await getDoc(doc(db, "stats", id));
|
const correspondingStat = await db.collection("stats").findOne<Stat>({ id: id});
|
||||||
|
|
||||||
if (correspondingStat.exists()) return {...correspondingStat.data(), id} as Stat;
|
if (correspondingStat) return correspondingStat;
|
||||||
|
|
||||||
await delay(3 * 10000);
|
await delay(3 * 10000);
|
||||||
return getCorrespondingStat(id, index + 1);
|
return getCorrespondingStat(id, index + 1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user