ENCOA-164: Corporate created by another Corporate Should be linked to the Master Corporate of the Creator Accoubnt
This commit is contained in:
@@ -7,6 +7,7 @@ import {v4} from "uuid";
|
||||
import {CorporateUser, Group} from "@/interfaces/user";
|
||||
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
import {getUserCorporate} from "@/utils/groups.be";
|
||||
|
||||
const DEFAULT_DESIRED_LEVELS = {
|
||||
reading: 9,
|
||||
@@ -38,6 +39,9 @@ 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 corporateCorporate = await getUserCorporate(maker.id);
|
||||
|
||||
const {email, passport_id, password, type, groupName, groupID, expiryDate, corporate} = req.body as {
|
||||
email: string;
|
||||
password?: string;
|
||||
@@ -60,6 +64,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
.then(async (userCredentials) => {
|
||||
const userId = userCredentials.user.uid;
|
||||
|
||||
const profilePicture = !corporateCorporate ? "/defaultAvatar.png" : corporateCorporate.profilePicture;
|
||||
|
||||
const user = {
|
||||
...req.body,
|
||||
bio: "",
|
||||
@@ -67,17 +73,17 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
focus: "academic",
|
||||
status: "active",
|
||||
desiredLevels: DEFAULT_DESIRED_LEVELS,
|
||||
profilePicture: maker.type === "corporate" || maker.type === "mastercorporate" ? maker.profilePicture : "/defaultAvatar.png",
|
||||
profilePicture,
|
||||
levels: DEFAULT_LEVELS,
|
||||
isFirstLogin: false,
|
||||
isVerified: true,
|
||||
registrationDate: new Date(),
|
||||
subscriptionExpirationDate: expiryDate || null,
|
||||
...(maker.type === "mastercorporate" && type === "corporate"
|
||||
...(!!corporateCorporate && type === "corporate"
|
||||
? {
|
||||
corporateInformation: {
|
||||
companyInformation: {
|
||||
name: maker.corporateInformation?.companyInformation?.name || "N/A",
|
||||
name: corporateCorporate.corporateInformation?.companyInformation?.name || "N/A",
|
||||
userAmount: 0,
|
||||
},
|
||||
},
|
||||
@@ -170,6 +176,32 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!!corporateCorporate && corporateCorporate.type === "mastercorporate" && type === "corporate") {
|
||||
const q = query(collection(db, "groups"), where("admin", "==", corporateCorporate.id), where("name", "==", "corporate"), 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)) {
|
||||
await updateDoc(doc.ref, {
|
||||
participants: [...participants, userId],
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const defaultGroup: Group = {
|
||||
admin: corporateCorporate.id,
|
||||
id: v4(),
|
||||
name: "Corporate",
|
||||
participants: [userId],
|
||||
disableEditing: true,
|
||||
};
|
||||
|
||||
await setDoc(doc(db, "groups", defaultGroup.id), defaultGroup);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user