Solved a problem related to not showing corporates if they are not in the correct group

This commit is contained in:
Tiago Ribeiro
2024-09-05 12:02:46 +01:00
parent 6ce81b300a
commit b6b5f3a9f1
2 changed files with 38 additions and 3 deletions

View File

@@ -7,7 +7,8 @@ import {sessionOptions} from "@/lib/session";
import {Group} from "@/interfaces/user";
import {v4} from "uuid";
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);
@@ -32,11 +33,12 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
if (req.session?.user?.type === "mastercorporate") {
try {
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);
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")]);
} catch (e) {
console.error(e);