Updated export to now work based on session
This commit is contained in:
@@ -6,6 +6,10 @@ import {
|
|||||||
getDoc,
|
getDoc,
|
||||||
deleteDoc,
|
deleteDoc,
|
||||||
updateDoc,
|
updateDoc,
|
||||||
|
getDocs,
|
||||||
|
query,
|
||||||
|
collection,
|
||||||
|
where,
|
||||||
} from "firebase/firestore";
|
} from "firebase/firestore";
|
||||||
import { withIronSessionApiRoute } from "iron-session/next";
|
import { withIronSessionApiRoute } from "iron-session/next";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
@@ -20,7 +24,7 @@ import {
|
|||||||
import blobStream from "blob-stream";
|
import blobStream from "blob-stream";
|
||||||
import { Stat } from "@/interfaces/user";
|
import { Stat } from "@/interfaces/user";
|
||||||
import { User } from "@/interfaces/user";
|
import { User } from "@/interfaces/user";
|
||||||
|
import { Module } from "@/interfaces";
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
@@ -30,44 +34,84 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const streamToBuffer = async (
|
export const streamToBuffer = async (
|
||||||
stream: NodeJS.ReadableStream,
|
stream: NodeJS.ReadableStream
|
||||||
): Promise<Buffer> => {
|
): Promise<Buffer> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const chunks: Buffer[] = [];
|
const chunks: Buffer[] = [];
|
||||||
stream.on('data', (data) => {
|
stream.on("data", (data) => {
|
||||||
chunks.push(data);
|
chunks.push(data);
|
||||||
});
|
});
|
||||||
stream.on('end', () => {
|
stream.on("end", () => {
|
||||||
resolve(Buffer.concat(chunks));
|
resolve(Buffer.concat(chunks));
|
||||||
});
|
});
|
||||||
stream.on('error', reject);
|
stream.on("error", reject);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface ModuleScore {
|
||||||
|
score: number;
|
||||||
|
total: number;
|
||||||
|
module: Module;
|
||||||
|
}
|
||||||
|
|
||||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||||
// debugger;
|
// debugger;
|
||||||
|
debugger;
|
||||||
if (req.session.user) {
|
if (req.session.user) {
|
||||||
const { id } = req.query as { id: string };
|
const { id } = req.query as { id: string };
|
||||||
|
// const codeCheckerRef = await getDocs(
|
||||||
|
// query(collection(db, "codes"), where("checkout", "==", checkout))
|
||||||
|
// );
|
||||||
|
|
||||||
const docRef = doc(db, "stats", id);
|
// const docRef = doc(db, "stats", id).where;
|
||||||
const docSnap = await getDoc(docRef);
|
// const docSnap = await getDoc(docRef);
|
||||||
|
|
||||||
if (docSnap.exists()) {
|
const docsSnap = await getDocs(
|
||||||
const stat = docSnap.data() as Stat;
|
query(
|
||||||
|
collection(db, "stats"),
|
||||||
|
where("session", "==", id),
|
||||||
|
where("user", "==", req.session.user.id)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
if (stat.user !== req.session.user.id) {
|
if (docsSnap.empty) {
|
||||||
res.status(401).json(undefined);
|
res.status(404).end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (stat.pdf) {
|
const docUser = await getDoc(doc(db, "users", req.session.user.id));
|
||||||
// res.status(200).end(docSnap.pdf);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
const docUser = await getDoc(doc(db, "users", stat.user));
|
|
||||||
|
|
||||||
if (docUser.exists()) {
|
if (docUser.exists()) {
|
||||||
const user = docUser.data() as User;
|
const user = docUser.data() as User;
|
||||||
|
|
||||||
|
const stats = docsSnap.docs.map((d) => d.data());
|
||||||
|
const results = stats.reduce((accm: ModuleScore[], { module, score }) => {
|
||||||
|
if (accm.find((e: ModuleScore) => e.module === module)) {
|
||||||
|
return accm.map((e: ModuleScore) => {
|
||||||
|
if (e.module === module) {
|
||||||
|
return {
|
||||||
|
...e,
|
||||||
|
score: e.score + score.correct,
|
||||||
|
total: e.total + score.total,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return e;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
...accm,
|
||||||
|
{
|
||||||
|
module,
|
||||||
|
score: score.correct,
|
||||||
|
total: score.total,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const [stat] = stats as Stat[];
|
||||||
|
|
||||||
const fileName = `${Date.now().toString()}.pdf`;
|
const fileName = `${Date.now().toString()}.pdf`;
|
||||||
const fileRef = ref(storage, `exam_report/${fileName}`);
|
const fileRef = ref(storage, `exam_report/${fileName}`);
|
||||||
const pdfStream = await ReactPDF.renderToStream(
|
const pdfStream = await ReactPDF.renderToStream(
|
||||||
@@ -82,11 +126,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const pdfBuffer = await streamToBuffer(pdfStream);
|
const pdfBuffer = await streamToBuffer(pdfStream);
|
||||||
const snapshot = await uploadBytes(fileRef, pdfBuffer, {
|
const snapshot = await uploadBytes(fileRef, pdfBuffer, {
|
||||||
contentType: 'application/pdf',
|
contentType: "application/pdf",
|
||||||
});
|
});
|
||||||
await updateDoc(docRef, {
|
docsSnap.docs.forEach(async (doc) => {
|
||||||
|
await updateDoc(doc.ref, {
|
||||||
pdf: snapshot.ref.fullPath,
|
pdf: snapshot.ref.fullPath,
|
||||||
});
|
});
|
||||||
|
});
|
||||||
res.status(200).end(snapshot.ref.fullPath);
|
res.status(200).end(snapshot.ref.fullPath);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -95,6 +141,3 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
res.status(500).json({ ok: false });
|
res.status(500).json({ ok: false });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(401).json(undefined);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user