Made it so when a user is deleted, all of their stats are also deleted
This commit is contained in:
@@ -2,7 +2,7 @@ import {PERMISSIONS} from "@/constants/userPermissions";
|
|||||||
import {app, adminApp} from "@/firebase";
|
import {app, adminApp} from "@/firebase";
|
||||||
import {User} from "@/interfaces/user";
|
import {User} from "@/interfaces/user";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {deleteDoc, doc, getDoc, getFirestore} from "firebase/firestore";
|
import {collection, deleteDoc, doc, getDoc, getDocs, getFirestore, query, where} from "firebase/firestore";
|
||||||
import {getAuth} from "firebase-admin/auth";
|
import {getAuth} from "firebase-admin/auth";
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {NextApiRequest, NextApiResponse} from "next";
|
import {NextApiRequest, NextApiResponse} from "next";
|
||||||
@@ -41,7 +41,7 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetUser = docTargetUser.data() as User;
|
const targetUser = {...docTargetUser.data(), id: docTargetUser.id} as User;
|
||||||
|
|
||||||
const permission = PERMISSIONS.deleteUser[targetUser.type];
|
const permission = PERMISSIONS.deleteUser[targetUser.type];
|
||||||
if (!permission.includes(user.type)) {
|
if (!permission.includes(user.type)) {
|
||||||
@@ -53,6 +53,14 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
await deleteDoc(doc(db, "users", id));
|
await deleteDoc(doc(db, "users", id));
|
||||||
|
|
||||||
res.json({ok: true});
|
res.json({ok: true});
|
||||||
|
|
||||||
|
const statsQuery = query(collection(db, "stats"), where("user", "==", targetUser.id));
|
||||||
|
const statsSnapshot = await getDocs(statsQuery);
|
||||||
|
await Promise.all(
|
||||||
|
statsSnapshot.docs.map(async (doc) => {
|
||||||
|
return await deleteDoc(doc.ref);
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|||||||
Reference in New Issue
Block a user