29 lines
960 B
TypeScript
29 lines
960 B
TypeScript
import {app} from "@/firebase";
|
|
import {Group, Type, User} from "@/interfaces/user";
|
|
import {uuidv4} from "@firebase/util";
|
|
import client from "@/lib/mongodb";
|
|
|
|
const db = client.db(process.env.MONGODB_DB);
|
|
|
|
export const addUserToGroupOnCreation = async (userId: string, type: Type, creatorId: string) => {
|
|
const creator = await db.collection("users").findOne<User>({id: creatorId});
|
|
if (!creator) return false;
|
|
|
|
const creatorGroup = await db.collection("groups").findOne<Group>({admin: creator.id, name: type === "student" ? "Students" : "Teachers"});
|
|
|
|
if (!!creatorGroup) {
|
|
await db.collection("groups").updateOne({id: creatorGroup.id}, {$set: {participants: [...creatorGroup.participants, userId]}});
|
|
return true;
|
|
}
|
|
|
|
const groupId = uuidv4();
|
|
await db.collection("groups").insertOne({
|
|
admin: creatorId,
|
|
name: type === "student" ? "Students" : "Teachers",
|
|
id: groupId,
|
|
participants: [userId],
|
|
disableEditing: true,
|
|
});
|
|
return true;
|
|
};
|