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

31
src/utils/registration.ts Normal file
View File

@@ -0,0 +1,31 @@
import {collection, doc, getDoc, getDocs, getFirestore, query, setDoc, where} from "firebase/firestore";
import {app} from "@/firebase";
import {Group, Type, User} from "@/interfaces/user";
import {uuidv4} from "@firebase/util";
const db = getFirestore(app);
export const addUserToGroupOnCreation = async (userId: string, type: Type, creatorId: string) => {
const creatorDoc = await getDoc(doc(db, "users", creatorId));
if (!creatorDoc.exists()) return false;
const creator = {...creatorDoc.data(), id: creatorDoc.id} as User;
const creatorGroupsDocs = await getDocs(query(collection(db, "groups"), where("admin", "==", creator.id)));
const typeGroup = creatorGroupsDocs.docs.find((x) => (x.data() as Group).name === (type === "student" ? "Students" : "Teachers"));
if (typeGroup && typeGroup.exists()) {
await setDoc(typeGroup.ref, {participants: [...typeGroup.data().participants, userId]}, {merge: true});
return true;
}
const groupId = uuidv4();
await setDoc(doc(db, "groups", groupId), {
admin: creatorId,
name: type === "student" ? "Students" : "Teachers",
id: groupId,
participants: [userId],
disableEditing: true,
} as Group);
return true;
};