Refactored /api/paypal, /api/permissions, /api/reset /api/sessions, /api/stats
This commit is contained in:
@@ -1,13 +1,6 @@
|
|||||||
// 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 { app } from "@/firebase";
|
import client from "@/lib/mongodb";
|
||||||
import {
|
|
||||||
getFirestore,
|
|
||||||
collection,
|
|
||||||
getDocs,
|
|
||||||
setDoc,
|
|
||||||
doc,
|
|
||||||
} 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";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
@@ -19,7 +12,7 @@ import { getAccessToken } from "@/utils/paypal";
|
|||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import { Group } from "@/interfaces/user";
|
import { Group } from "@/interfaces/user";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -67,26 +60,26 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
duration,
|
duration,
|
||||||
duration_unit
|
duration_unit
|
||||||
);
|
);
|
||||||
await setDoc(
|
|
||||||
doc(db, "users", req.session.user!.id),
|
await db.collection("users").updateOne(
|
||||||
|
{ id: req.session.user!.id },
|
||||||
{
|
{
|
||||||
|
$set: {
|
||||||
subscriptionExpirationDate: updatedExpirationDate.toISOString(),
|
subscriptionExpirationDate: updatedExpirationDate.toISOString(),
|
||||||
status: "active",
|
status: "active",
|
||||||
},
|
}
|
||||||
{ merge: true }
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await setDoc(doc(db, "paypalpayments", v4()), {
|
await db.collection("paypalpayments").insertOne({
|
||||||
|
id: v4(),
|
||||||
orderId: id,
|
orderId: id,
|
||||||
userId: req.session.user!.id,
|
userId: req.session.user!.id,
|
||||||
status: request.data.status,
|
status: request.data.status,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
value:
|
value: request.data.purchase_units[0].payments.captures[0].amount.value,
|
||||||
request.data.purchase_units[0].payments.captures[0].amount.value,
|
currency: request.data.purchase_units[0].payments.captures[0].amount.currency_code,
|
||||||
currency:
|
|
||||||
request.data.purchase_units[0].payments.captures[0].amount
|
|
||||||
.currency_code,
|
|
||||||
subscriptionDuration: duration,
|
subscriptionDuration: duration,
|
||||||
subscriptionDurationUnit: duration_unit,
|
subscriptionDurationUnit: duration_unit,
|
||||||
subscriptionExpirationDate: updatedExpirationDate.toISOString(),
|
subscriptionExpirationDate: updatedExpirationDate.toISOString(),
|
||||||
@@ -96,12 +89,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (user!.type === "corporate") {
|
if (user!.type === "corporate") {
|
||||||
const snapshot = await getDocs(collection(db, "groups"));
|
const groups = (
|
||||||
const groups: Group[] = (
|
await db.collection("groups").find<Group>({}).toArray()
|
||||||
snapshot.docs.map((doc) => ({
|
|
||||||
id: doc.id,
|
|
||||||
...doc.data(),
|
|
||||||
})) as Group[]
|
|
||||||
).filter((x) => x.admin === user!.id);
|
).filter((x) => x.admin === user!.id);
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
@@ -109,14 +98,14 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
.flatMap((x) => x.participants)
|
.flatMap((x) => x.participants)
|
||||||
.map(
|
.map(
|
||||||
async (x) =>
|
async (x) =>
|
||||||
await setDoc(
|
await db.collection("users").updateOne(
|
||||||
doc(db, "users", x),
|
{ id: x },
|
||||||
{
|
{
|
||||||
subscriptionExpirationDate:
|
$set: {
|
||||||
updatedExpirationDate.toISOString(),
|
subscriptionExpirationDate: updatedExpirationDate.toISOString(),
|
||||||
status: "active",
|
status: "active",
|
||||||
},
|
}
|
||||||
{ merge: true }
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
// 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 {app} from "@/firebase";
|
import client from "@/lib/mongodb";
|
||||||
import {getFirestore, doc, setDoc, getDoc} 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";
|
||||||
import { getPermissionDoc } from "@/utils/permissions.be";
|
import { getPermissionDoc } from "@/utils/permissions.be";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -37,7 +36,12 @@ async function patch(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const { users } = req.body;
|
const { users } = req.body;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await setDoc(doc(db, "permissions", id), {users}, {merge: true});
|
await db.collection("permissions").updateOne(
|
||||||
|
{ id: id },
|
||||||
|
{ $set: {...users, id: id} },
|
||||||
|
{ upsert: true }
|
||||||
|
);
|
||||||
|
|
||||||
return res.status(200).json({ ok: true });
|
return res.status(200).json({ ok: true });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|||||||
@@ -1,25 +1,10 @@
|
|||||||
// 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 { app } from "@/firebase";
|
|
||||||
import {
|
|
||||||
getFirestore,
|
|
||||||
collection,
|
|
||||||
getDocs,
|
|
||||||
query,
|
|
||||||
where,
|
|
||||||
doc,
|
|
||||||
setDoc,
|
|
||||||
addDoc,
|
|
||||||
getDoc,
|
|
||||||
deleteDoc,
|
|
||||||
} 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";
|
||||||
import { Permission } from "@/interfaces/permissions";
|
import { Permission } from "@/interfaces/permissions";
|
||||||
import { getPermissions, getPermissionDocs } from "@/utils/permissions.be";
|
import { getPermissions, getPermissionDocs } from "@/utils/permissions.be";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import {NextApiRequest, NextApiResponse} from "next";
|
import {NextApiRequest, NextApiResponse} from "next";
|
||||||
import {getAuth} from "firebase-admin/auth";
|
import {getAuth} from "firebase-admin/auth";
|
||||||
import {adminApp, app} from "@/firebase";
|
import {adminApp} from "@/firebase";
|
||||||
|
import client from "@/lib/mongodb";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {doc, getFirestore, setDoc} from "firebase/firestore";
|
|
||||||
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
const auth = getAuth(adminApp);
|
const auth = getAuth(adminApp);
|
||||||
const db = getFirestore(app);
|
|
||||||
|
|
||||||
export default withIronSessionApiRoute(verify, sessionOptions);
|
export default withIronSessionApiRoute(verify, sessionOptions);
|
||||||
|
|
||||||
@@ -19,8 +20,10 @@ async function verify(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const userRef = doc(db, "users", user.uid);
|
await db.collection("users").updateOne(
|
||||||
await setDoc(userRef, {isVerified: true}, {merge: true});
|
{ id: user.uid},
|
||||||
|
{ $set: {isVerified: true} }
|
||||||
|
);
|
||||||
|
|
||||||
res.status(200).json({ok: true});
|
res.status(200).json({ok: true});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
// 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 {app} from "@/firebase";
|
import client from "@/lib/mongodb";
|
||||||
import {getFirestore, doc, getDoc, deleteDoc} 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";
|
||||||
import {Session} from "@/hooks/useSessions";
|
import {Session} from "@/hooks/useSessions";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -23,14 +22,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const {id} = req.query as {id: string};
|
const {id} = req.query as {id: string};
|
||||||
|
|
||||||
const docRef = doc(db, "sessions", id);
|
const docSnap = await db.collection("sessions").findOne({ id: id });
|
||||||
const docSnap = await getDoc(docRef);
|
|
||||||
|
|
||||||
if (docSnap.exists()) {
|
if (docSnap) {
|
||||||
res.status(200).json({
|
res.status(200).json(docSnap);
|
||||||
id: docSnap.id,
|
|
||||||
...docSnap.data(),
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
res.status(404).json(undefined);
|
res.status(404).json(undefined);
|
||||||
}
|
}
|
||||||
@@ -44,11 +39,10 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const {id} = req.query as {id: string};
|
const {id} = req.query as {id: string};
|
||||||
|
|
||||||
const docRef = doc(db, "sessions", id);
|
const docSnap = await db.collection("sessions").findOne({ id: id });
|
||||||
const docSnap = await getDoc(docRef);
|
|
||||||
|
|
||||||
if (!docSnap.exists()) return res.status(404).json({ok: false});
|
if (!docSnap) return res.status(404).json({ok: false});
|
||||||
|
await db.collection("sessions").deleteOne({ id: id });
|
||||||
|
|
||||||
await deleteDoc(docRef);
|
|
||||||
return res.status(200).json({ok: true});
|
return res.status(200).json({ok: true});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
// 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 {app} from "@/firebase";
|
import client from "@/lib/mongodb";
|
||||||
import {getFirestore, collection, getDocs, query, where, doc, setDoc, addDoc, getDoc} 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";
|
||||||
import {Session} from "@/hooks/useSessions";
|
import {Session} from "@/hooks/useSessions";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -24,12 +23,8 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const {user} = req.query as {user?: string};
|
const {user} = req.query as {user?: string};
|
||||||
|
|
||||||
const q = user ? query(collection(db, "sessions"), where("user", "==", user)) : collection(db, "sessions");
|
const q = user ? {user: user} : {};
|
||||||
const snapshot = await getDocs(q);
|
const sessions = await db.collection("sessions").find<Session>(q).toArray();
|
||||||
const sessions = snapshot.docs.map((doc) => ({
|
|
||||||
id: doc.id,
|
|
||||||
...doc.data(),
|
|
||||||
})) as Session[];
|
|
||||||
|
|
||||||
res.status(200).json(
|
res.status(200).json(
|
||||||
sessions.filter((x) => {
|
sessions.filter((x) => {
|
||||||
@@ -45,9 +40,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
res.status(401).json({ok: false});
|
res.status(401).json({ok: false});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = req.body;
|
const session = req.body;
|
||||||
await setDoc(doc(db, "sessions", session.id), session, {merge: true});
|
|
||||||
|
await db.collection("sessions").updateOne(
|
||||||
|
{ id: session.id},
|
||||||
|
{ $set: session },
|
||||||
|
{ upsert: true }
|
||||||
|
);
|
||||||
|
|
||||||
res.status(200).json({ok: true});
|
res.status(200).json({ok: true});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,6 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { app, storage } from "@/firebase";
|
import { storage } from "@/firebase";
|
||||||
import {
|
import client from "@/lib/mongodb";
|
||||||
getFirestore,
|
|
||||||
doc,
|
|
||||||
getDoc,
|
|
||||||
updateDoc,
|
|
||||||
getDocs,
|
|
||||||
query,
|
|
||||||
collection,
|
|
||||||
where,
|
|
||||||
} 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";
|
||||||
import ReactPDF from "@react-pdf/renderer";
|
import ReactPDF from "@react-pdf/renderer";
|
||||||
@@ -35,7 +26,8 @@ import {
|
|||||||
} from "@/utils/pdf";
|
} from "@/utils/pdf";
|
||||||
import moment from "moment-timezone";
|
import moment from "moment-timezone";
|
||||||
import { getCorporateNameForStudent } from "@/utils/groups.be";
|
import { getCorporateNameForStudent } from "@/utils/groups.be";
|
||||||
const db = getFirestore(app);
|
|
||||||
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -281,19 +273,22 @@ async function getPdfUrl(pdfStream: any, docsSnap: any) {
|
|||||||
|
|
||||||
// upload the pdf to storage
|
// upload the pdf to storage
|
||||||
const pdfBuffer = await streamToBuffer(pdfStream);
|
const pdfBuffer = await streamToBuffer(pdfStream);
|
||||||
const snapshot = await uploadBytes(fileRef, pdfBuffer, {
|
await uploadBytes(fileRef, pdfBuffer, {
|
||||||
contentType: "application/pdf",
|
contentType: "application/pdf",
|
||||||
});
|
});
|
||||||
|
|
||||||
// update the stats entries with the pdf url to prevent duplication
|
// update the stats entries with the pdf url to prevent duplication
|
||||||
docsSnap.docs.forEach(async (doc: any) => {
|
await db.collection("stats").updateOne(
|
||||||
await updateDoc(doc.ref, {
|
{ id: docsSnap.id },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
pdf: {
|
pdf: {
|
||||||
path: refName,
|
path: refName,
|
||||||
version: process.env.PDF_VERSION,
|
version: process.env.PDF_VERSION,
|
||||||
},
|
}
|
||||||
});
|
}
|
||||||
});
|
}
|
||||||
|
);
|
||||||
return getDownloadURL(fileRef);
|
return getDownloadURL(fileRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,16 +297,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
if (req.session.user) {
|
if (req.session.user) {
|
||||||
const { id } = req.query as { id: string };
|
const { id } = req.query as { id: string };
|
||||||
// fetch stats entries for this particular user with the requested exam session
|
// fetch stats entries for this particular user with the requested exam session
|
||||||
const docsSnap = await getDocs(
|
const stats = await db.collection("stats").find<Stat>({ session: id }).toArray();
|
||||||
query(collection(db, "stats"), where("session", "==", id))
|
|
||||||
);
|
|
||||||
|
|
||||||
if (docsSnap.empty) {
|
if (stats.length == 0) {
|
||||||
res.status(400).end();
|
res.status(400).end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const stats = docsSnap.docs.map((d) => d.data()) as Stat[];
|
|
||||||
// verify if the stats already have a pdf generated
|
// verify if the stats already have a pdf generated
|
||||||
const hasPDF = stats.find(
|
const hasPDF = stats.find(
|
||||||
(s) => s.pdf?.path && s.pdf?.version === process.env.PDF_VERSION
|
(s) => s.pdf?.path && s.pdf?.version === process.env.PDF_VERSION
|
||||||
@@ -336,23 +328,22 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// generate the pdf report
|
// generate the pdf report
|
||||||
const docUser = await getDoc(doc(db, "users", userId));
|
const docUser = await db.collection("users").findOne<User>({ id: userId});
|
||||||
|
|
||||||
if (docUser.exists()) {
|
if (docUser) {
|
||||||
// we'll need the user in order to get the user data (name, email, focus, etc);
|
// we'll need the user in order to get the user data (name, email, focus, etc);
|
||||||
|
|
||||||
const [stat] = stats;
|
const [stat] = stats;
|
||||||
|
|
||||||
if (stat.module === "level") {
|
if (stat.module === "level") {
|
||||||
const user = docUser.data() as StudentUser;
|
const user = docUser as StudentUser;
|
||||||
|
|
||||||
const uniqueExercises = stats.map((s) => ({
|
const uniqueExercises = stats.map((s) => ({
|
||||||
name: "Gramar & Vocabulary",
|
name: "Gramar & Vocabulary",
|
||||||
result: `${s.score.correct}/${s.score.total}`,
|
result: `${s.score.correct}/${s.score.total}`,
|
||||||
}));
|
}));
|
||||||
const dates = stats.map((s) => moment(s.date));
|
const dates = stats.map((s) => moment(s.date));
|
||||||
const timeSpent = `${
|
const timeSpent = `${stats.reduce((accm, s: Stat) => accm + (s.timeSpent || 0), 0) / 60
|
||||||
stats.reduce((accm, s: Stat) => accm + (s.timeSpent || 0), 0) / 60
|
|
||||||
} minutes`;
|
} minutes`;
|
||||||
const score = stats.reduce((accm, s) => accm + s.score.correct, 0);
|
const score = stats.reduce((accm, s) => accm + s.score.correct, 0);
|
||||||
const corporateName = await getCorporateNameForStudent(userId);
|
const corporateName = await getCorporateNameForStudent(userId);
|
||||||
@@ -373,11 +364,11 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
const url = await getPdfUrl(pdfStream, docsSnap);
|
const url = await getPdfUrl(pdfStream, docUser);
|
||||||
res.status(200).end(url);
|
res.status(200).end(url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const user = docUser.data() as User;
|
const user = docUser as User;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const pdfStream = await getDefaultPDFStream(
|
const pdfStream = await getDefaultPDFStream(
|
||||||
@@ -386,7 +377,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
`${req.headers.origin || ""}${req.url}`
|
`${req.headers.origin || ""}${req.url}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const url = await getPdfUrl(pdfStream, docsSnap);
|
const url = await getPdfUrl(pdfStream, docUser);
|
||||||
res.status(200).end(url);
|
res.status(200).end(url);
|
||||||
return;
|
return;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -411,21 +402,17 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const { id } = req.query as { id: string };
|
const { id } = req.query as { id: string };
|
||||||
const docsSnap = await getDocs(
|
const stats = await db.collection("stats").find<Stat>({ session: id }).toArray();
|
||||||
query(collection(db, "stats"), where("session", "==", id))
|
|
||||||
);
|
|
||||||
|
|
||||||
if (docsSnap.empty) {
|
if (stats.length == 0) {
|
||||||
res.status(404).end();
|
res.status(404).end();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const stats = docsSnap.docs.map((d) => d.data());
|
|
||||||
|
|
||||||
const hasPDF = stats.find((s) => s.pdf?.path);
|
const hasPDF = stats.find((s) => s.pdf?.path);
|
||||||
|
|
||||||
if (hasPDF) {
|
if (hasPDF) {
|
||||||
const fileRef = ref(storage, hasPDF.pdf.path);
|
const fileRef = ref(storage, hasPDF.pdf!.path);
|
||||||
const url = await getDownloadURL(fileRef);
|
const url = await getDownloadURL(fileRef);
|
||||||
return res.redirect(url);
|
return res.redirect(url);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
// 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 {app} from "@/firebase";
|
import client from "@/lib/mongodb";
|
||||||
import {getFirestore, collection, getDocs, query, where, setDoc, doc, getDoc, deleteDoc} from "firebase/firestore";
|
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
|
||||||
import {sessionOptions} from "@/lib/session";
|
|
||||||
import {uuidv4} from "@firebase/util";
|
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (req.method === "GET") return GET(req, res);
|
if (req.method === "GET") return GET(req, res);
|
||||||
@@ -17,8 +13,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
async function GET(req: NextApiRequest, res: NextApiResponse) {
|
async function GET(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const {id} = req.query;
|
const {id} = req.query;
|
||||||
|
|
||||||
const snapshot = await getDoc(doc(db, "stats", id as string));
|
const snapshot = await db.collection("stats").findOne({ id: id as string});
|
||||||
if (!snapshot.exists()) return res.status(404).json({id: snapshot.id});
|
if (!snapshot) return res.status(404).json({id: id as string});
|
||||||
|
|
||||||
res.status(200).json({...snapshot.data(), id: snapshot.id});
|
res.status(200).json({...snapshot.data(), id: snapshot.id});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
// 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 {app} from "@/firebase";
|
import client from "@/lib/mongodb";
|
||||||
import {getFirestore, collection, getDocs, query, where, doc, setDoc, addDoc, getDoc, deleteDoc} 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";
|
||||||
import { Stat } from "@/interfaces/user";
|
import { Stat } from "@/interfaces/user";
|
||||||
import { Assignment } from "@/interfaces/results";
|
import { Assignment } from "@/interfaces/results";
|
||||||
import { groupBy } from "lodash";
|
import { groupBy } from "lodash";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -22,17 +21,9 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
res.status(401).json({ ok: false });
|
res.status(401).json({ ok: false });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const snapshot = await db.collection("stats").find<Stat>({}).toArray();
|
||||||
|
|
||||||
const q = query(collection(db, "stats"));
|
res.status(200).json(snapshot);
|
||||||
|
|
||||||
const snapshot = await getDocs(q);
|
|
||||||
|
|
||||||
res.status(200).json(
|
|
||||||
snapshot.docs.map((doc) => ({
|
|
||||||
id: doc.id,
|
|
||||||
...doc.data(),
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -42,10 +33,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const stats = req.body as Stat[];
|
const stats = req.body as Stat[];
|
||||||
await stats.forEach(async (stat) => await setDoc(doc(db, "stats", stat.id), stat));
|
stats.forEach(async (stat) => await db.collection("stats").updateOne(
|
||||||
await stats.forEach(async (stat) => {
|
{ id: stat.id },
|
||||||
const sessionDoc = await getDoc(doc(db, "sessions", stat.session));
|
{ $set: stat },
|
||||||
if (sessionDoc.exists()) await deleteDoc(sessionDoc.ref);
|
{ upsert: true }
|
||||||
|
));
|
||||||
|
stats.forEach(async (stat) => {
|
||||||
|
await db.collection("sessions").deleteOne({ id: stat.session })
|
||||||
});
|
});
|
||||||
|
|
||||||
const groupedStatsByAssignment = groupBy(
|
const groupedStatsByAssignment = groupBy(
|
||||||
@@ -55,19 +49,20 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
if (Object.keys(groupedStatsByAssignment).length > 0) {
|
if (Object.keys(groupedStatsByAssignment).length > 0) {
|
||||||
const assignments = Object.keys(groupedStatsByAssignment);
|
const assignments = Object.keys(groupedStatsByAssignment);
|
||||||
|
|
||||||
await assignments.forEach(async (assignmentId) => {
|
assignments.forEach(async (assignmentId) => {
|
||||||
const assignmentStats = groupedStatsByAssignment[assignmentId] as Stat[];
|
const assignmentStats = groupedStatsByAssignment[assignmentId] as Stat[];
|
||||||
|
|
||||||
const assignmentSnapshot = await getDoc(doc(db, "assignments", assignmentId));
|
const assignmentSnapshot = await db.collection("assignments").findOne<Assignment>({ id: assignmentId });
|
||||||
await setDoc(
|
await db.collection("assignments").updateOne(
|
||||||
doc(db, "assignments", assignmentId),
|
{ id: assignmentId },
|
||||||
{
|
{
|
||||||
|
$set: {
|
||||||
results: [
|
results: [
|
||||||
...(assignmentSnapshot.data() as Assignment).results,
|
...assignmentSnapshot ? assignmentSnapshot.results : [],
|
||||||
{ user: req.session.user?.id, type: req.session.user?.focus, stats: assignmentStats },
|
{ user: req.session.user?.id, type: req.session.user?.focus, stats: assignmentStats },
|
||||||
],
|
],
|
||||||
},
|
}
|
||||||
{merge: true},
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,29 +6,25 @@ import {sessionOptions} from "@/lib/session";
|
|||||||
import {calculateBandScore} from "@/utils/score";
|
import {calculateBandScore} from "@/utils/score";
|
||||||
import {groupByModule, groupBySession} from "@/utils/stats";
|
import {groupByModule, groupBySession} from "@/utils/stats";
|
||||||
import { MODULE_ARRAY } from "@/utils/moduleUtils";
|
import { MODULE_ARRAY } from "@/utils/moduleUtils";
|
||||||
import {getAuth} from "firebase/auth";
|
import client from "@/lib/mongodb";
|
||||||
import {collection, doc, getDoc, getDocs, getFirestore, query, updateDoc, where} from "firebase/firestore";
|
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {groupBy} from "lodash";
|
import {groupBy} from "lodash";
|
||||||
import {NextApiRequest, NextApiResponse} from "next";
|
import {NextApiRequest, NextApiResponse} from "next";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(update, sessionOptions);
|
export default withIronSessionApiRoute(update, sessionOptions);
|
||||||
|
|
||||||
async function update(req: NextApiRequest, res: NextApiResponse) {
|
async function update(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (req.session.user) {
|
if (req.session.user) {
|
||||||
const docUser = await getDoc(doc(db, "users", req.session.user.id));
|
const docUser = await db.collection("users").findOne({ id: req.session.user.id });
|
||||||
if (!docUser.exists()) {
|
|
||||||
|
if (!docUser) {
|
||||||
res.status(401).json(undefined);
|
res.status(401).json(undefined);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const q = query(collection(db, "stats"), where("user", "==", req.session.user.id));
|
const stats = await db.collection("stats").find<Stat>({ user: req.session.user.id }).toArray();
|
||||||
const stats = (await getDocs(q)).docs.map((doc) => ({
|
|
||||||
...(doc.data() as Stat),
|
|
||||||
id: doc.id,
|
|
||||||
})) as Stat[];
|
|
||||||
|
|
||||||
const groupedStats = groupBySession(stats);
|
const groupedStats = groupBySession(stats);
|
||||||
const sessionLevels: {[key in Module]: {correct: number; total: number}}[] = Object.keys(groupedStats).map((key) => {
|
const sessionLevels: {[key in Module]: {correct: number; total: number}}[] = Object.keys(groupedStats).map((key) => {
|
||||||
@@ -102,8 +98,10 @@ async function update(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
level: calculateBandScore(levelLevel.correct, levelLevel.total, "level", req.session.user.focus),
|
level: calculateBandScore(levelLevel.correct, levelLevel.total, "level", req.session.user.focus),
|
||||||
};
|
};
|
||||||
|
|
||||||
const userDoc = doc(db, "users", req.session.user.id);
|
await db.collection("users").updateOne(
|
||||||
await updateDoc(userDoc, {levels});
|
{ id: req.session.user.id},
|
||||||
|
{ $set: {levels} }
|
||||||
|
);
|
||||||
|
|
||||||
res.status(200).json({ok: true});
|
res.status(200).json({ok: true});
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
// 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 {app} from "@/firebase";
|
import client from "@/lib/mongodb";
|
||||||
import {getFirestore, collection, getDocs, query, where, doc, setDoc, addDoc} 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";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -16,14 +15,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const {user} = req.query;
|
const {user} = req.query;
|
||||||
const q = query(collection(db, "stats"), where("user", "==", user));
|
const snapshot = await db.collection("stats").find({ user: user }).toArray();
|
||||||
|
|
||||||
const snapshot = await getDocs(q);
|
res.status(200).json(snapshot);
|
||||||
|
|
||||||
res.status(200).json(
|
|
||||||
snapshot.docs.map((doc) => ({
|
|
||||||
id: doc.id,
|
|
||||||
...doc.data(),
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user