ENCOA-164: Corporate created by another Corporate Should be linked to the Master Corporate of the Creator Accoubnt

This commit is contained in:
Tiago Ribeiro
2024-09-04 11:59:14 +01:00
parent 4654c21d92
commit d4553501b8
2 changed files with 42 additions and 6 deletions

View File

@@ -21,14 +21,18 @@ interface Props {
}
export default function DemographicInformationInput({user, mutateUser}: Props) {
const [country, setCountry] = useState<string>();
const [phone, setPhone] = useState<string>();
const [country, setCountry] = useState(user.demographicInformation?.country);
const [phone, setPhone] = useState(user.demographicInformation?.phone);
const [passport_id, setPassportID] = useState<string | undefined>(user.type === "student" ? user.demographicInformation?.passport_id : undefined);
const [gender, setGender] = useState<Gender>();
const [employment, setEmployment] = useState<EmploymentStatus>();
const [position, setPosition] = useState<string>();
const [timezone, setTimezone] = useState<string>(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<string>();
const [commercialRegistration, setCommercialRegistration] = useState<string>();

View File

@@ -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);