add group creation.
This commit is contained in:
@@ -74,7 +74,8 @@ export default function BatchCreateUser({ user }: { user: User }) {
|
|||||||
country,
|
country,
|
||||||
passport_id,
|
passport_id,
|
||||||
email,
|
email,
|
||||||
phone
|
phone,
|
||||||
|
group
|
||||||
] = row as string[];
|
] = row as string[];
|
||||||
return EMAIL_REGEX.test(email.toString().trim())
|
return EMAIL_REGEX.test(email.toString().trim())
|
||||||
? {
|
? {
|
||||||
@@ -82,6 +83,7 @@ export default function BatchCreateUser({ user }: { user: User }) {
|
|||||||
name: `${firstName ?? ""} ${lastName ?? ""}`.trim().toLowerCase(),
|
name: `${firstName ?? ""} ${lastName ?? ""}`.trim().toLowerCase(),
|
||||||
type: type,
|
type: type,
|
||||||
passport_id: passport_id?.toString().trim() || undefined,
|
passport_id: passport_id?.toString().trim() || undefined,
|
||||||
|
groupName: group,
|
||||||
demographicInformation: {
|
demographicInformation: {
|
||||||
country: country,
|
country: country,
|
||||||
passport_id: passport_id?.toString().trim() || undefined,
|
passport_id: passport_id?.toString().trim() || undefined,
|
||||||
@@ -122,13 +124,15 @@ export default function BatchCreateUser({ user }: { user: User }) {
|
|||||||
)
|
)
|
||||||
if (!confirmed)
|
if (!confirmed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (newUsers.length > 0)
|
if (newUsers.length > 0)
|
||||||
{
|
{
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
Promise.all(newUsers.map((user) => {
|
Promise.all(newUsers.map(async (user) => {
|
||||||
return axios.post("/api/make_user", user)
|
await axios.post("/api/make_user", user)
|
||||||
})).finally(() => {
|
})).then((res) =>{
|
||||||
|
toast.success(
|
||||||
|
`Successfully added ${newUsers.length} user(s)!`
|
||||||
|
)}).finally(() => {
|
||||||
return clear();
|
return clear();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -163,6 +167,9 @@ export default function BatchCreateUser({ user }: { user: User }) {
|
|||||||
<th className="border border-neutral-200 px-2 py-1">
|
<th className="border border-neutral-200 px-2 py-1">
|
||||||
Phone Number
|
Phone Number
|
||||||
</th>
|
</th>
|
||||||
|
<th className="border border-neutral-200 px-2 py-1">
|
||||||
|
Group Name
|
||||||
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import {
|
|||||||
getDocs,
|
getDocs,
|
||||||
getDoc,
|
getDoc,
|
||||||
deleteDoc,
|
deleteDoc,
|
||||||
|
limit,
|
||||||
|
updateDoc,
|
||||||
} from "firebase/firestore";
|
} from "firebase/firestore";
|
||||||
import { withIronSessionApiRoute } from "iron-session/next";
|
import { withIronSessionApiRoute } from "iron-session/next";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
@@ -44,18 +46,23 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
const maker = req.session.user;
|
||||||
|
if (!maker) {
|
||||||
return res
|
return res
|
||||||
.status(401)
|
.status(401)
|
||||||
.json({ ok: false, reason: "You must be logged in to make user!" });
|
.json({ ok: false, reason: "You must be logged in to make user!" });
|
||||||
}
|
}
|
||||||
|
const { email, passport_id, type, groupName } = req.body as {
|
||||||
const { email, passport_id, type } = req.body as {
|
|
||||||
email: string;
|
email: string;
|
||||||
passport_id: 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) => {
|
.then(async (userCredentials) => {
|
||||||
const userId = userCredentials.user.uid;
|
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", defaultStudentsGroup.id), defaultStudentsGroup);
|
||||||
await setDoc(doc(db, "groups", defaultCorporateGroup.id), defaultCorporateGroup);
|
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) => {
|
.catch((error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(401).json({error});
|
return res.status(401).json({error});
|
||||||
});
|
});
|
||||||
|
return res.status(200).json({ ok: true });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import BatchCreateUser from "./(admin)/BatchCreateUser";
|
|||||||
|
|
||||||
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
|
export const getServerSideProps = withIronSessionSsr(({req, res}) => {
|
||||||
const user = req.session.user;
|
const user = req.session.user;
|
||||||
|
|
||||||
if (!user || !user.isVerified) {
|
if (!user || !user.isVerified) {
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
|
|||||||
Reference in New Issue
Block a user