Updated the make_user to use MongoDB
This commit is contained in:
@@ -4,12 +4,13 @@ import {getFirestore, setDoc, doc, query, collection, where, getDocs, getDoc, de
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {v4} from "uuid";
|
||||
import {CorporateUser, Group, Type} from "@/interfaces/user";
|
||||
import {CorporateUser, Group, Type, User} from "@/interfaces/user";
|
||||
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
import {getUserCorporate, getUserGroups} from "@/utils/groups.be";
|
||||
import {getGroup, getGroups, getUserCorporate, getUserGroups, getUserNamedGroup} from "@/utils/groups.be";
|
||||
import {uniq} from "lodash";
|
||||
import {getSpecificUsers, getUser} from "@/utils/users.be";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const DEFAULT_DESIRED_LEVELS = {
|
||||
reading: 9,
|
||||
@@ -26,7 +27,7 @@ const DEFAULT_LEVELS = {
|
||||
};
|
||||
|
||||
const auth = getAuth(app);
|
||||
const db = getFirestore(app);
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
@@ -77,6 +78,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const user = {
|
||||
...req.body,
|
||||
bio: "",
|
||||
id: userId,
|
||||
type: type,
|
||||
focus: "academic",
|
||||
status: "active",
|
||||
@@ -102,8 +104,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const uid = new ShortUniqueId();
|
||||
const code = uid.randomUUID(6);
|
||||
|
||||
await setDoc(doc(db, "users", userId), user);
|
||||
await setDoc(doc(db, "codes", code), {
|
||||
await db.collection("users").insertOne(user);
|
||||
await db.collection("codes").insertOne({
|
||||
code,
|
||||
creator: maker.id,
|
||||
expiryDate,
|
||||
@@ -135,34 +137,21 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
await setDoc(doc(db, "groups", defaultTeachersGroup.id), defaultTeachersGroup);
|
||||
await setDoc(doc(db, "groups", defaultStudentsGroup.id), defaultStudentsGroup);
|
||||
await db.collection("groups").insertMany([defaultStudentsGroup, defaultTeachersGroup]);
|
||||
}
|
||||
|
||||
if (!!corporate) {
|
||||
const corporateQ = query(collection(db, "users"), where("email", "==", corporate.trim().toLowerCase()));
|
||||
const corporateSnapshot = await getDocs(corporateQ);
|
||||
const corporateUser = await db.collection("users").findOne<CorporateUser>({email: corporate.trim().toLowerCase()});
|
||||
|
||||
if (!corporateSnapshot.empty) {
|
||||
const corporateUser = {...corporateSnapshot.docs[0].data(), id: corporateSnapshot.docs[0].id} as CorporateUser;
|
||||
await setDoc(doc(db, "codes", code), {creator: corporateUser.id}, {merge: true});
|
||||
if (!!corporateUser) {
|
||||
await db.collection("codes").updateOne({code}, {$set: {creator: corporateUser.id}});
|
||||
const typeGroup = await db
|
||||
.collection("groups")
|
||||
.findOne<Group>({creator: corporateUser.id, name: type === "student" ? "Students" : "Teachers"});
|
||||
|
||||
const q = query(
|
||||
collection(db, "groups"),
|
||||
where("admin", "==", corporateUser.id),
|
||||
where("name", "==", type === "student" ? "Students" : "Teachers"),
|
||||
limit(1),
|
||||
);
|
||||
const snapshot = await getDocs(q);
|
||||
|
||||
if (!snapshot.empty) {
|
||||
const doc = snapshot.docs[0];
|
||||
const participants: string[] = doc.get("participants");
|
||||
|
||||
if (!participants.includes(userId)) {
|
||||
await updateDoc(doc.ref, {
|
||||
participants: [...participants, userId],
|
||||
});
|
||||
if (!!typeGroup) {
|
||||
if (!typeGroup.participants.includes(userId)) {
|
||||
await db.collection("groups").updateOne({id: typeGroup.id}, {$set: {participants: [...typeGroup.participants, userId]}});
|
||||
}
|
||||
} else {
|
||||
const defaultGroup: Group = {
|
||||
@@ -173,30 +162,18 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
await setDoc(doc(db, "groups", defaultGroup.id), defaultGroup);
|
||||
await db.collection("groups").insertOne(defaultGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (maker.type === "corporate") {
|
||||
await setDoc(doc(db, "codes", code), {creator: maker.id}, {merge: true});
|
||||
await db.collection("codes").updateOne({code}, {$set: {creator: maker.id}});
|
||||
const typeGroup = await getUserNamedGroup(maker.id, type === "student" ? "Students" : "Teachers");
|
||||
|
||||
const q = query(
|
||||
collection(db, "groups"),
|
||||
where("admin", "==", maker.id),
|
||||
where("name", "==", type === "student" ? "Students" : "Teachers"),
|
||||
limit(1),
|
||||
);
|
||||
const snapshot = await getDocs(q);
|
||||
|
||||
if (!snapshot.empty) {
|
||||
const doc = snapshot.docs[0];
|
||||
const participants: string[] = doc.get("participants");
|
||||
|
||||
if (!participants.includes(userId)) {
|
||||
await updateDoc(doc.ref, {
|
||||
participants: [...participants, userId],
|
||||
});
|
||||
if (!!typeGroup) {
|
||||
if (!typeGroup.participants.includes(userId)) {
|
||||
await db.collection("groups").updateOne({id: typeGroup.id}, {$set: {participants: [...typeGroup.participants, userId]}});
|
||||
}
|
||||
} else {
|
||||
const defaultGroup: Group = {
|
||||
@@ -207,22 +184,18 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
await setDoc(doc(db, "groups", defaultGroup.id), defaultGroup);
|
||||
await db.collection("groups").insertOne(defaultGroup);
|
||||
}
|
||||
}
|
||||
|
||||
if (!!corporateCorporate && corporateCorporate.type === "mastercorporate" && type === "corporate") {
|
||||
const q = query(collection(db, "groups"), where("admin", "==", corporateCorporate.id), where("name", "==", "corporate"), limit(1));
|
||||
const snapshot = await getDocs(q);
|
||||
const corporateGroup = await getUserNamedGroup(corporateCorporate.id, "Corporate");
|
||||
|
||||
if (!snapshot.empty) {
|
||||
const doc = snapshot.docs[0];
|
||||
const participants: string[] = doc.get("participants");
|
||||
|
||||
if (!participants.includes(userId)) {
|
||||
await updateDoc(doc.ref, {
|
||||
participants: [...participants, userId],
|
||||
});
|
||||
if (!!corporateGroup) {
|
||||
if (!corporateGroup.participants.includes(userId)) {
|
||||
await db
|
||||
.collection("groups")
|
||||
.updateOne({id: corporateGroup.id}, {$set: {participants: [...corporateGroup.participants, userId]}});
|
||||
}
|
||||
} else {
|
||||
const defaultGroup: Group = {
|
||||
@@ -233,13 +206,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
await setDoc(doc(db, "groups", defaultGroup.id), defaultGroup);
|
||||
await db.collection("groups").insertOne(defaultGroup);
|
||||
}
|
||||
}
|
||||
|
||||
if (!!groupID) {
|
||||
const groupSnapshot = await getDoc(doc(db, "groups", groupID));
|
||||
await setDoc(groupSnapshot.ref, {participants: [...groupSnapshot.data()!.participants, userId]}, {merge: true});
|
||||
const group = await getGroup(groupID);
|
||||
if (!!group) await db.collection("groups").updateOne({id: group.id}, {$set: {participants: [...group.participants, userId]}});
|
||||
}
|
||||
|
||||
console.log(`Returning - ${email}`);
|
||||
|
||||
Reference in New Issue
Block a user