ENCOA-98: Change the template on the Excel import Function
This commit is contained in:
@@ -104,7 +104,7 @@ export default function BatchCreateUser({user}: {user: User}) {
|
|||||||
const information = uniqBy(
|
const information = uniqBy(
|
||||||
rows
|
rows
|
||||||
.map((row) => {
|
.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 =
|
const countryItem =
|
||||||
countryCodes.findOne("countryCode" as any, country.toUpperCase()) ||
|
countryCodes.findOne("countryCode" as any, country.toUpperCase()) ||
|
||||||
countryCodes.all().find((x) => x.countryNameEn.toLowerCase() === country.toLowerCase());
|
countryCodes.all().find((x) => x.countryNameEn.toLowerCase() === country.toLowerCase());
|
||||||
@@ -116,6 +116,7 @@ export default function BatchCreateUser({user}: {user: User}) {
|
|||||||
type: type,
|
type: type,
|
||||||
passport_id: passport_id?.toString().trim() || undefined,
|
passport_id: passport_id?.toString().trim() || undefined,
|
||||||
groupName: group,
|
groupName: group,
|
||||||
|
corporate,
|
||||||
studentID,
|
studentID,
|
||||||
demographicInformation: {
|
demographicInformation: {
|
||||||
country: countryItem?.countryCode,
|
country: countryItem?.countryCode,
|
||||||
@@ -184,6 +185,7 @@ export default function BatchCreateUser({user}: {user: User}) {
|
|||||||
<th className="border border-neutral-200 px-2 py-1">Phone Number</th>
|
<th className="border border-neutral-200 px-2 py-1">Phone Number</th>
|
||||||
<th className="border border-neutral-200 px-2 py-1">Group Name</th>
|
<th className="border border-neutral-200 px-2 py-1">Group Name</th>
|
||||||
<th className="border border-neutral-200 px-2 py-1">Student ID</th>
|
<th className="border border-neutral-200 px-2 py-1">Student ID</th>
|
||||||
|
{user?.type !== "corporate" && <th className="border border-neutral-200 px-2 py-1">Corporate (e-mail)</th>}
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ 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 {Group} from "@/interfaces/user";
|
import {CorporateUser, Group} from "@/interfaces/user";
|
||||||
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
||||||
|
|
||||||
const DEFAULT_DESIRED_LEVELS = {
|
const DEFAULT_DESIRED_LEVELS = {
|
||||||
@@ -37,13 +37,14 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
if (!maker) {
|
if (!maker) {
|
||||||
return res.status(401).json({ok: false, reason: "You must be logged in to make user!"});
|
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;
|
email: string;
|
||||||
password?: string;
|
password?: string;
|
||||||
passport_id: string;
|
passport_id: string;
|
||||||
type: string;
|
type: string;
|
||||||
groupName?: string;
|
groupName?: string;
|
||||||
groupID?: string;
|
groupID?: string;
|
||||||
|
corporate?: string;
|
||||||
expiryDate: null | Date;
|
expiryDate: null | Date;
|
||||||
};
|
};
|
||||||
// cleaning data
|
// cleaning data
|
||||||
@@ -52,6 +53,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
delete req.body.groupID;
|
delete req.body.groupID;
|
||||||
delete req.body.expiryDate;
|
delete req.body.expiryDate;
|
||||||
delete req.body.password;
|
delete req.body.password;
|
||||||
|
delete req.body.corporate;
|
||||||
|
|
||||||
await createUserWithEmailAndPassword(auth, email.toLowerCase(), !!password ? password : passport_id)
|
await createUserWithEmailAndPassword(auth, email.toLowerCase(), !!password ? password : passport_id)
|
||||||
.then(async (userCredentials) => {
|
.then(async (userCredentials) => {
|
||||||
@@ -102,6 +104,34 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
await setDoc(doc(db, "groups", defaultCorporateGroup.id), defaultCorporateGroup);
|
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) {
|
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 q = query(collection(db, "groups"), where("admin", "==", maker.id), where("name", "==", groupName.trim()), limit(1));
|
||||||
const snapshot = await getDocs(q);
|
const snapshot = await getDocs(q);
|
||||||
|
|||||||
Reference in New Issue
Block a user