diff --git a/src/pages/(admin)/BatchCreateUser.tsx b/src/pages/(admin)/BatchCreateUser.tsx
index d470c2e8..b484fd12 100644
--- a/src/pages/(admin)/BatchCreateUser.tsx
+++ b/src/pages/(admin)/BatchCreateUser.tsx
@@ -74,7 +74,8 @@ export default function BatchCreateUser({ user }: { user: User }) {
country,
passport_id,
email,
- phone
+ phone,
+ group
] = row as string[];
return EMAIL_REGEX.test(email.toString().trim())
? {
@@ -82,6 +83,7 @@ export default function BatchCreateUser({ user }: { user: User }) {
name: `${firstName ?? ""} ${lastName ?? ""}`.trim().toLowerCase(),
type: type,
passport_id: passport_id?.toString().trim() || undefined,
+ groupName: group,
demographicInformation: {
country: country,
passport_id: passport_id?.toString().trim() || undefined,
@@ -122,13 +124,15 @@ export default function BatchCreateUser({ user }: { user: User }) {
)
if (!confirmed)
return;
-
if (newUsers.length > 0)
{
setIsLoading(true);
- Promise.all(newUsers.map((user) => {
- return axios.post("/api/make_user", user)
- })).finally(() => {
+ Promise.all(newUsers.map(async (user) => {
+ await axios.post("/api/make_user", user)
+ })).then((res) =>{
+ toast.success(
+ `Successfully added ${newUsers.length} user(s)!`
+ )}).finally(() => {
return clear();
})
}
@@ -163,6 +167,9 @@ export default function BatchCreateUser({ user }: { user: User }) {
Phone Number
|
+
+ Group Name
+ |
diff --git a/src/pages/api/make_user.ts b/src/pages/api/make_user.ts
index 5a30cf55..1629e184 100644
--- a/src/pages/api/make_user.ts
+++ b/src/pages/api/make_user.ts
@@ -10,6 +10,8 @@ import {
getDocs,
getDoc,
deleteDoc,
+ limit,
+ updateDoc,
} from "firebase/firestore";
import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "@/lib/session";
@@ -44,18 +46,23 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
}
async function post(req: NextApiRequest, res: NextApiResponse) {
- if (!req.session.user) {
+ const maker = req.session.user;
+ if (!maker) {
return res
.status(401)
.json({ ok: false, reason: "You must be logged in to make user!" });
}
-
- const { email, passport_id, type } = req.body as {
+ const { email, passport_id, type, groupName } = req.body as {
email: string;
passport_id: string;
- type: string
+ type: string,
+ groupName: string
};
- createUserWithEmailAndPassword(auth, email.toLowerCase(), passport_id)
+ // cleaning data
+ delete req.body.passport_id;
+ delete req.body.groupName;
+
+ await createUserWithEmailAndPassword(auth, email.toLowerCase(), passport_id)
.then(async (userCredentials) => {
const userId = userCredentials.user.uid;
@@ -101,10 +108,45 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
await setDoc(doc(db, "groups", defaultStudentsGroup.id), defaultStudentsGroup);
await setDoc(doc(db, "groups", defaultCorporateGroup.id), defaultCorporateGroup);
}
- res.status(200).json({ ok: true });
+
+ 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)
+
+ if(snapshot.empty){
+ const values = {
+ id: v4(),
+ admin: maker.id,
+ name: groupName.trim(),
+ participants: [userId],
+ disableEditing: false,
+ }
+
+ await setDoc(doc(db, "groups", values.id) , values)
+
+
+ }else{
+
+
+ const doc = snapshot.docs[0]
+ const participants : string[] = doc.get('participants');
+
+
+ if(!participants.includes(userId)){
+
+
+ updateDoc(doc.ref, {
+ participants: [...participants, userId]
+ })
+ }
+ }
+ }
})
.catch((error) => {
console.log(error);
- res.status(401).json({error});
+ return res.status(401).json({error});
});
+ return res.status(200).json({ ok: true });
+
}
diff --git a/src/pages/settings.tsx b/src/pages/settings.tsx
index e3488cd9..51bc0ea9 100644
--- a/src/pages/settings.tsx
+++ b/src/pages/settings.tsx
@@ -17,7 +17,6 @@ import BatchCreateUser from "./(admin)/BatchCreateUser";
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
const user = req.session.user;
-
if (!user || !user.isVerified) {
return {
redirect: {