From 3e21538d023dcd824d5024aa968a20d864015999 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Fri, 23 Aug 2024 16:59:08 +0100 Subject: [PATCH] ENCOA-98: Change the template on the Excel import Function --- src/pages/(admin)/BatchCreateUser.tsx | 4 +++- src/pages/api/make_user.ts | 34 +++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/pages/(admin)/BatchCreateUser.tsx b/src/pages/(admin)/BatchCreateUser.tsx index 409b710a..a1d9b856 100644 --- a/src/pages/(admin)/BatchCreateUser.tsx +++ b/src/pages/(admin)/BatchCreateUser.tsx @@ -104,7 +104,7 @@ export default function BatchCreateUser({user}: {user: User}) { const information = uniqBy( rows .map((row) => { - const [firstName, lastName, country, passport_id, email, phone, group, studentID] = row as string[]; + const [firstName, lastName, country, passport_id, email, phone, group, studentID, corporate] = row as string[]; const countryItem = countryCodes.findOne("countryCode" as any, country.toUpperCase()) || countryCodes.all().find((x) => x.countryNameEn.toLowerCase() === country.toLowerCase()); @@ -116,6 +116,7 @@ export default function BatchCreateUser({user}: {user: User}) { type: type, passport_id: passport_id?.toString().trim() || undefined, groupName: group, + corporate, studentID, demographicInformation: { country: countryItem?.countryCode, @@ -184,6 +185,7 @@ export default function BatchCreateUser({user}: {user: User}) { Phone Number Group Name Student ID + {user?.type !== "corporate" && Corporate (e-mail)} diff --git a/src/pages/api/make_user.ts b/src/pages/api/make_user.ts index 479157bb..a8ceab07 100644 --- a/src/pages/api/make_user.ts +++ b/src/pages/api/make_user.ts @@ -4,7 +4,7 @@ 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 {Group} from "@/interfaces/user"; +import {CorporateUser, Group} from "@/interfaces/user"; import {createUserWithEmailAndPassword, getAuth} from "firebase/auth"; const DEFAULT_DESIRED_LEVELS = { @@ -37,13 +37,14 @@ async function post(req: NextApiRequest, res: NextApiResponse) { if (!maker) { return res.status(401).json({ok: false, reason: "You must be logged in to make user!"}); } - const {email, passport_id, password, type, groupName, groupID, expiryDate} = req.body as { + const {email, passport_id, password, type, groupName, groupID, expiryDate, corporate} = req.body as { email: string; password?: string; passport_id: string; type: string; groupName?: string; groupID?: string; + corporate?: string; expiryDate: null | Date; }; // cleaning data @@ -52,6 +53,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { delete req.body.groupID; delete req.body.expiryDate; delete req.body.password; + delete req.body.corporate; await createUserWithEmailAndPassword(auth, email.toLowerCase(), !!password ? password : passport_id) .then(async (userCredentials) => { @@ -102,6 +104,34 @@ async function post(req: NextApiRequest, res: NextApiResponse) { await setDoc(doc(db, "groups", defaultCorporateGroup.id), defaultCorporateGroup); } + if (!!corporate) { + const corporateQ = query(collection(db, "users"), where("email", "==", corporate)); + const corporateSnapshot = await getDocs(corporateQ); + + if (!corporateSnapshot.empty) { + const corporateUser = corporateSnapshot.docs[0].data() as CorporateUser; + + 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)) { + updateDoc(doc.ref, { + participants: [...participants, userId], + }); + } + } + } + } + if (typeof groupName === "string" && groupName.trim().length > 0) { const q = query(collection(db, "groups"), where("admin", "==", maker.id), where("name", "==", groupName.trim()), limit(1)); const snapshot = await getDocs(q);