Merged develop into feature/ai-detection

This commit is contained in:
Tiago Ribeiro
2024-07-25 21:00:40 +00:00
35 changed files with 6709 additions and 4067 deletions

View File

@@ -1,4 +1,4 @@
import { CorporateUser, Group, User } from "@/interfaces/user";
import { CorporateUser, Group, User, Type } from "@/interfaces/user";
import axios from "axios";
export const isUserFromCorporate = async (userID: string) => {
@@ -7,20 +7,12 @@ export const isUserFromCorporate = async (userID: string) => {
const users = (await axios.get<User[]>("/api/users/list")).data;
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");
};
export const getUserCorporate = async (
userID: string,
): Promise<CorporateUser | undefined> => {
const userRequest = await axios.get<User>(`/api/users/${userID}`);
if (userRequest.status === 200) {
const user = userRequest.data;
if (user.type === "corporate") return user;
}
const getAdminForGroup = async (userID: string, role: Type) => {
const groups = (await axios.get<Group[]>(`/api/groups?participant=${userID}`))
.data;
@@ -29,9 +21,23 @@ export const getUserCorporate = async (
const userRequest = await axios.get<User>(`/api/users/${g.admin}`);
if (userRequest.status === 200) return userRequest.data;
return undefined;
}),
})
);
const admins = adminRequests.filter((x) => x?.type === "corporate");
const admins = adminRequests.filter((x) => x?.type === role);
return admins.length > 0 ? (admins[0] as CorporateUser) : undefined;
};
export const getUserCorporate = async (
userID: string
): Promise<CorporateUser | undefined> => {
const userRequest = await axios.get<User>(`/api/users/${userID}`);
if (userRequest.status === 200) {
const user = userRequest.data;
if (user.type === "corporate") {
return getAdminForGroup(userID, "mastercorporate");
}
}
return getAdminForGroup(userID, "corporate");
};

View File

@@ -0,0 +1,83 @@
import { app, adminApp } from "@/firebase";
import { getAuth } from "firebase-admin/auth";
import {
collection,
deleteDoc,
doc,
getDoc,
getDocs,
getFirestore,
query,
setDoc,
where,
} from "firebase/firestore";
import {
Permission,
PermissionType,
permissions,
} from "@/interfaces/permissions";
import {v4} from "uuid";
const db = getFirestore(app);
async function createPermission(type: string) {
const permData = doc(db, "permissions", v4());
const permDoc = await getDoc(permData);
if (permDoc.exists()) {
return true;
}
await setDoc(permData, {
type,
users: [],
});
}
export function getPermissions(userId: string | undefined, docs: Permission[]) {
if (!userId) {
return [];
}
// the concept is like a blacklist
// if the user exists in the list, he can't access this permission
// even if his profile allows
const permissions = docs.reduce((acc: PermissionType[], doc: Permission) => {
// typescript was complaining even with the validation on the top
if (doc.users.includes(userId)) {
return acc;
}
return [...acc, doc.type];
}, []) as PermissionType[];
return permissions;
}
export async function bootstrap() {
await permissions.forEach(async (type) => {
await createPermission(type);
});
}
export async function getPermissionDoc(id: string) {
const docRef = doc(db, "permissions", id);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
return docSnap.data() as Permission;
}
throw new Error("Permission not found");
}
export async function getPermissionDocs() {
const q = query(collection(db, "permissions"));
// firebase is missing something like array-not-contains
const snapshot = await getDocs(q);
const docs = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as Permission[];
return docs;
}

45
src/utils/permissions.ts Normal file
View File

@@ -0,0 +1,45 @@
import { PermissionType } from "@/interfaces/permissions";
import { User, Type, userTypes } from "@/interfaces/user";
export function checkAccess(
user: User,
types: Type[],
permission?: PermissionType
) {
if (!user) {
return false;
}
// if(user.type === '') {
if (!user.type) {
console.warn("User type is empty");
return false;
}
if (types.length === 0) {
console.warn("No types provided");
return false;
}
if (!types.includes(user.type)) {
return false;
}
// we may not want a permission check as most screens dont even havr a specific permission
if (permission) {
// this works more like a blacklist
// therefore if we don't find the permission here, he can't do it
if (!(user.permissions || []).includes(permission)) {
return false;
}
}
return true;
}
export function getTypesOfUser(types: Type[]) {
// basicly generate a list of all types except the excluded ones
return userTypes.filter((userType) => {
return !types.includes(userType);
})
}

14
src/utils/users.be.ts Normal file
View File

@@ -0,0 +1,14 @@
import { app } from "@/firebase";
import { collection, getDocs, getFirestore } from "firebase/firestore";
import { User } from "@/interfaces/user";
const db = getFirestore(app);
export async function getUsers() {
const snapshot = await getDocs(collection(db, "users"));
return snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as User[];
}

View File

@@ -25,7 +25,7 @@ export const exportListToExcel = (rowUsers: User[], users: User[], groups: Group
expiryDate: user.subscriptionExpirationDate ? moment(user.subscriptionExpirationDate).format("DD/MM/YYYY") : "Unlimited",
country: user.demographicInformation?.country || "N/A",
phone: user.demographicInformation?.phone || "N/A",
employmentPosition: (user.type === "corporate" ? user.demographicInformation?.position : user.demographicInformation?.employment) || "N/A",
employmentPosition: (user.type === "corporate" || user.type === "mastercorporate" ? user.demographicInformation?.position : user.demographicInformation?.employment) || "N/A",
gender: user.demographicInformation?.gender ? capitalize(user.demographicInformation.gender) : "N/A",
verified: user.isVerified?.toString() || "FALSE",
}));