New feature on the account creation:

It automatically stores who created the code and adds the registered user to a group administrated by that creator
This commit is contained in:
Tiago Ribeiro
2023-10-10 23:00:36 +01:00
parent 1aa4f0ddfd
commit 634a396434
6 changed files with 55 additions and 13 deletions

View File

@@ -69,7 +69,7 @@ const CreatePanel = ({user, users, group, onCreate}: CreateDialogProps) => {
return (
<div className="flex flex-col gap-12 mt-4 w-full px-4 py-2">
<div className="flex flex-col gap-8">
<Input name="name" type="text" label="Name" defaultValue={name} onChange={setName} required />
<Input name="name" type="text" label="Name" defaultValue={name} onChange={setName} required disabled={group?.disableEditing} />
<div className="flex flex-col gap-3 w-full">
<label className="font-normal text-base text-mti-gray-dim">Participants</label>
<div className="flex gap-8 w-full">

View File

@@ -5,6 +5,7 @@ import {sessionOptions} from "@/lib/session";
import {withIronSessionApiRoute} from "iron-session/next";
import {getFirestore, doc, setDoc, query, collection, where, getDocs} from "firebase/firestore";
import {DemographicInformation, Type} from "@/interfaces/user";
import {addUserToGroupOnCreation} from "@/utils/registration";
const auth = getAuth(app);
const db = getFirestore(app);
@@ -36,7 +37,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
return;
}
const codeData = codeDocs[0].data() as {code: string; type: Type};
const codeData = codeDocs[0].data() as {code: string; type: Type; creator: string};
createUserWithEmailAndPassword(auth, email, password)
.then(async (userCredentials) => {
@@ -55,6 +56,7 @@ async function login(req: NextApiRequest, res: NextApiResponse) {
await setDoc(doc(db, "users", userId), user);
await setDoc(codeDocs[0].ref, {userId: userId}, {merge: true});
await addUserToGroupOnCreation(userId, codeData.type, codeData.creator);
req.session.user = {...user, id: userId};
await req.session.save();

View File

@@ -2,7 +2,7 @@ import {PERMISSIONS} from "@/constants/userPermissions";
import {app, adminApp} from "@/firebase";
import {User} from "@/interfaces/user";
import {sessionOptions} from "@/lib/session";
import {collection, deleteDoc, doc, getDoc, getDocs, getFirestore, query, where} from "firebase/firestore";
import {collection, deleteDoc, doc, getDoc, getDocs, getFirestore, query, setDoc, where} from "firebase/firestore";
import {getAuth} from "firebase-admin/auth";
import {withIronSessionApiRoute} from "iron-session/next";
import {NextApiRequest, NextApiResponse} from "next";
@@ -49,18 +49,24 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
return;
}
const userCodeDocs = await getDocs(query(collection(db, "codes"), where("userId", "==", id)));
const userParticipantGroup = await getDocs(query(collection(db, "groups"), where("participants", "array-contains", id)));
const userGroupAdminDocs = await getDocs(query(collection(db, "groups"), where("admin", "==", id)));
const userStatsDocs = await getDocs(query(collection(db, "stats"), where("user", "==", id)));
await Promise.all([
...userCodeDocs.docs.map(async (x) => await deleteDoc(x.ref)),
...userGroupAdminDocs.docs.map(async (x) => await deleteDoc(x.ref)),
...userStatsDocs.docs.map(async (x) => await deleteDoc(x.ref)),
...userParticipantGroup.docs.map(
async (x) => await setDoc(x.ref, {participants: x.data().participants.filter((y: string) => y !== id)}, {merge: true}),
),
]);
await auth.deleteUser(id);
await deleteDoc(doc(db, "users", id));
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) {