diff --git a/src/components/DemographicInformationInput.tsx b/src/components/DemographicInformationInput.tsx index 2505b919..1ba81b01 100644 --- a/src/components/DemographicInformationInput.tsx +++ b/src/components/DemographicInformationInput.tsx @@ -21,14 +21,18 @@ interface Props { } export default function DemographicInformationInput({user, mutateUser}: Props) { - const [country, setCountry] = useState(); - const [phone, setPhone] = useState(); + const [country, setCountry] = useState(user.demographicInformation?.country); + const [phone, setPhone] = useState(user.demographicInformation?.phone); const [passport_id, setPassportID] = useState(user.type === "student" ? user.demographicInformation?.passport_id : undefined); const [gender, setGender] = useState(); const [employment, setEmployment] = useState(); - const [position, setPosition] = useState(); const [timezone, setTimezone] = useState(moment.tz.guess()); const [isLoading, setIsLoading] = useState(false); + const [position, setPosition] = useState( + user.type === "corporate" || user.type === "mastercorporate" + ? user.demographicInformation?.position + : user.demographicInformation?.employment, + ); const [companyName, setCompanyName] = useState(); const [commercialRegistration, setCommercialRegistration] = useState(); diff --git a/src/pages/api/make_user.ts b/src/pages/api/make_user.ts index 3c9223e9..271eea8a 100644 --- a/src/pages/api/make_user.ts +++ b/src/pages/api/make_user.ts @@ -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);