Corrected a problem related to getting the corporate of a user
This commit is contained in:
@@ -10,7 +10,7 @@ import {v4} from "uuid";
|
|||||||
import {checkAccess} from "@/utils/permissions";
|
import {checkAccess} from "@/utils/permissions";
|
||||||
import {CEFR_STEPS} from "@/resources/grading";
|
import {CEFR_STEPS} from "@/resources/grading";
|
||||||
import {getCorporateUser} from "@/resources/user";
|
import {getCorporateUser} from "@/resources/user";
|
||||||
import {getUserCorporate} from "@/utils/groups";
|
import {getUserCorporate} from "@/utils/groups.be";
|
||||||
import {Grading} from "@/interfaces";
|
import {Grading} from "@/interfaces";
|
||||||
import {getGroupsForUser} from "@/utils/groups.be";
|
import {getGroupsForUser} from "@/utils/groups.be";
|
||||||
import {uniq} from "lodash";
|
import {uniq} from "lodash";
|
||||||
|
|||||||
@@ -33,11 +33,34 @@ export const updateExpiryDateOnGroup = async (participantID: string, corporateID
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getUserCorporate = async (id: string) => {
|
||||||
|
const user = await getUser(id);
|
||||||
|
if (user.type === "corporate" || user.type === "mastercorporate") return user;
|
||||||
|
|
||||||
|
const groups = await getParticipantGroups(id);
|
||||||
|
const admins = await Promise.all(groups.map((x) => x.admin).map(getUser));
|
||||||
|
const corporates = admins.filter((x) => x.type === "corporate");
|
||||||
|
|
||||||
|
if (corporates.length === 0) return undefined;
|
||||||
|
return corporates.shift() as CorporateUser;
|
||||||
|
};
|
||||||
|
|
||||||
export const getGroups = async () => {
|
export const getGroups = async () => {
|
||||||
const groupDocs = await getDocs(collection(db, "groups"));
|
const groupDocs = await getDocs(collection(db, "groups"));
|
||||||
return groupDocs.docs.map((x) => ({...x.data(), id: x.id})) as Group[];
|
return groupDocs.docs.map((x) => ({...x.data(), id: x.id})) as Group[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getParticipantGroups = async (id: string) => {
|
||||||
|
const snapshot = await getDocs(query(collection(db, "groups"), where("participants", "array-contains", id)));
|
||||||
|
|
||||||
|
const groups = snapshot.docs.map((doc) => ({
|
||||||
|
id: doc.id,
|
||||||
|
...doc.data(),
|
||||||
|
})) as Group[];
|
||||||
|
|
||||||
|
return groups;
|
||||||
|
};
|
||||||
|
|
||||||
export const getUserGroups = async (id: string): Promise<Group[]> => {
|
export const getUserGroups = async (id: string): Promise<Group[]> => {
|
||||||
const groupDocs = await getDocs(query(collection(db, "groups"), where("admin", "==", id)));
|
const groupDocs = await getDocs(query(collection(db, "groups"), where("admin", "==", id)));
|
||||||
return groupDocs.docs.map((x) => ({...x.data(), id})) as Group[];
|
return groupDocs.docs.map((x) => ({...x.data(), id})) as Group[];
|
||||||
|
|||||||
@@ -1,36 +1,30 @@
|
|||||||
import { CorporateUser, Group, User, Type } from "@/interfaces/user";
|
import {CorporateUser, Group, User, Type} from "@/interfaces/user";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
export const isUserFromCorporate = async (userID: string) => {
|
export const isUserFromCorporate = async (userID: string) => {
|
||||||
const groups = (await axios.get<Group[]>(`/api/groups?participant=${userID}`))
|
const groups = (await axios.get<Group[]>(`/api/groups?participant=${userID}`)).data;
|
||||||
.data;
|
|
||||||
const users = (await axios.get<User[]>("/api/users/list")).data;
|
const users = (await axios.get<User[]>("/api/users/list")).data;
|
||||||
|
|
||||||
const adminTypes = groups.map(
|
const adminTypes = groups.map((g) => users.find((u) => u.id === g.admin)?.type);
|
||||||
(g) => users.find((u) => u.id === g.admin)?.type
|
|
||||||
);
|
|
||||||
return adminTypes.includes("corporate");
|
return adminTypes.includes("corporate");
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAdminForGroup = async (userID: string, role: Type) => {
|
const getAdminForGroup = async (userID: string, role: Type) => {
|
||||||
const groups = (await axios.get<Group[]>(`/api/groups?participant=${userID}`))
|
const groups = (await axios.get<Group[]>(`/api/groups?participant=${userID}`)).data;
|
||||||
.data;
|
|
||||||
|
|
||||||
const adminRequests = await Promise.all(
|
const adminRequests = await Promise.all(
|
||||||
groups.map(async (g) => {
|
groups.map(async (g) => {
|
||||||
const userRequest = await axios.get<User>(`/api/users/${g.admin}`);
|
const userRequest = await axios.get<User>(`/api/users/${g.admin}`);
|
||||||
if (userRequest.status === 200) return userRequest.data;
|
if (userRequest.status === 200) return userRequest.data;
|
||||||
return undefined;
|
return undefined;
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const admins = adminRequests.filter((x) => x?.type === role);
|
const admins = adminRequests.filter((x) => x?.type === role);
|
||||||
return admins.length > 0 ? (admins[0] as CorporateUser) : undefined;
|
return admins.length > 0 ? (admins[0] as CorporateUser) : undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getUserCorporate = async (
|
export const getUserCorporate = async (userID: string): Promise<CorporateUser | undefined> => {
|
||||||
userID: string
|
|
||||||
): Promise<CorporateUser | undefined> => {
|
|
||||||
const userRequest = await axios.get<User>(`/api/users/${userID}`);
|
const userRequest = await axios.get<User>(`/api/users/${userID}`);
|
||||||
if (userRequest.status === 200) {
|
if (userRequest.status === 200) {
|
||||||
const user = userRequest.data;
|
const user = userRequest.data;
|
||||||
|
|||||||
Reference in New Issue
Block a user