Merge branch 'develop' into features-21-08-24
This commit is contained in:
@@ -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,19 +37,25 @@ 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, type, groupName, 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;
|
||||
groupName?: string;
|
||||
groupID?: string;
|
||||
corporate?: string;
|
||||
expiryDate: null | Date;
|
||||
};
|
||||
// cleaning data
|
||||
delete req.body.passport_id;
|
||||
delete req.body.groupName;
|
||||
delete req.body.groupID;
|
||||
delete req.body.expiryDate;
|
||||
delete req.body.password;
|
||||
delete req.body.corporate;
|
||||
|
||||
await createUserWithEmailAndPassword(auth, email.toLowerCase(), passport_id)
|
||||
await createUserWithEmailAndPassword(auth, email.toLowerCase(), !!password ? password : passport_id)
|
||||
.then(async (userCredentials) => {
|
||||
const userId = userCredentials.user.uid;
|
||||
|
||||
@@ -66,6 +72,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
registrationDate: new Date(),
|
||||
subscriptionExpirationDate: expiryDate || null,
|
||||
};
|
||||
|
||||
await setDoc(doc(db, "users", userId), user);
|
||||
if (type === "corporate") {
|
||||
const defaultTeachersGroup: Group = {
|
||||
@@ -97,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);
|
||||
@@ -123,6 +158,11 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!!groupID) {
|
||||
const groupSnapshot = await getDoc(doc(db, "groups", groupID));
|
||||
await setDoc(groupSnapshot.ref, {participants: [...groupSnapshot.data()!.participants, userId]}, {merge: true});
|
||||
}
|
||||
|
||||
console.log(`Returning - ${email}`);
|
||||
return res.status(200).json({ok: true});
|
||||
})
|
||||
|
||||
17
src/pages/api/users/balance.ts
Normal file
17
src/pages/api/users/balance.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {getUserBalance} from "@/utils/users.be";
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const balance = await getUserBalance(req.session.user);
|
||||
res.status(200).json({balance});
|
||||
}
|
||||
Reference in New Issue
Block a user