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