ENCOA-98: Change the template on the Excel import Function

This commit is contained in:
Tiago Ribeiro
2024-08-23 16:59:08 +01:00
parent 33fd6ddf8f
commit 3e21538d02
2 changed files with 35 additions and 3 deletions

View File

@@ -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);