Solved a problem related to not showing corporates if they are not in the correct group
This commit is contained in:
@@ -7,7 +7,8 @@ import {sessionOptions} from "@/lib/session";
|
|||||||
import {Group} from "@/interfaces/user";
|
import {Group} from "@/interfaces/user";
|
||||||
import {v4} from "uuid";
|
import {v4} from "uuid";
|
||||||
import {updateExpiryDateOnGroup, getGroupsForUser} from "@/utils/groups.be";
|
import {updateExpiryDateOnGroup, getGroupsForUser} from "@/utils/groups.be";
|
||||||
import {uniqBy} from "lodash";
|
import {uniq, uniqBy} from "lodash";
|
||||||
|
import {getUser} from "@/utils/users.be";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
|
|
||||||
@@ -32,11 +33,12 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
if (req.session?.user?.type === "mastercorporate") {
|
if (req.session?.user?.type === "mastercorporate") {
|
||||||
try {
|
try {
|
||||||
const masterCorporateGroups = await getGroupsForUser(admin, participant);
|
const masterCorporateGroups = await getGroupsForUser(admin, participant);
|
||||||
const corporatesFromMaster = masterCorporateGroups.filter((g) => g.name.trim() === "Corporate").flatMap((g) => g.participants);
|
const participants = uniq(masterCorporateGroups.flatMap((g) => g.participants));
|
||||||
|
const corporatesFromMaster = (await Promise.all(participants.map(getUser))).filter((x) => x.type === "corporate");
|
||||||
|
|
||||||
if (corporatesFromMaster.length === 0) return res.status(200).json(masterCorporateGroups);
|
if (corporatesFromMaster.length === 0) return res.status(200).json(masterCorporateGroups);
|
||||||
|
|
||||||
const groups = await Promise.all(corporatesFromMaster.map((c) => getGroupsForUser(c, participant)));
|
const groups = await Promise.all(corporatesFromMaster.map((c) => getGroupsForUser(c.id, participant)));
|
||||||
return res.status(200).json([...masterCorporateGroups, ...uniqBy(groups.flat(), "id")]);
|
return res.status(200).json([...masterCorporateGroups, ...uniqBy(groups.flat(), "id")]);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|||||||
@@ -174,6 +174,39 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (maker.type === "corporate") {
|
||||||
|
await setDoc(doc(db, "codes", code), {creator: maker.id}, {merge: true});
|
||||||
|
|
||||||
|
const q = query(
|
||||||
|
collection(db, "groups"),
|
||||||
|
where("admin", "==", maker.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)) {
|
||||||
|
await updateDoc(doc.ref, {
|
||||||
|
participants: [...participants, userId],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const defaultGroup: Group = {
|
||||||
|
admin: maker.id,
|
||||||
|
id: v4(),
|
||||||
|
name: type === "student" ? "Students" : "Teachers",
|
||||||
|
participants: [userId],
|
||||||
|
disableEditing: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
await setDoc(doc(db, "groups", defaultGroup.id), defaultGroup);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!!corporateCorporate && corporateCorporate.type === "mastercorporate" && type === "corporate") {
|
if (!!corporateCorporate && corporateCorporate.type === "mastercorporate" && type === "corporate") {
|
||||||
const q = query(collection(db, "groups"), where("admin", "==", corporateCorporate.id), where("name", "==", "corporate"), limit(1));
|
const q = query(collection(db, "groups"), where("admin", "==", corporateCorporate.id), where("name", "==", "corporate"), limit(1));
|
||||||
const snapshot = await getDocs(q);
|
const snapshot = await getDocs(q);
|
||||||
|
|||||||
Reference in New Issue
Block a user