Updated permissions to have a key to group them

This commit is contained in:
Joao Ramos
2024-08-14 18:44:35 +01:00
parent 859d9283a7
commit cb75ba6056
4 changed files with 197 additions and 109 deletions

View File

@@ -16,12 +16,13 @@ import {
Permission,
PermissionType,
permissions,
PermissionTopic,
} from "@/interfaces/permissions";
import {v4} from "uuid";
import { v4 } from "uuid";
const db = getFirestore(app);
async function createPermission(type: string) {
async function createPermission(topic: string, type: string) {
const permData = doc(db, "permissions", v4());
const permDoc = await getDoc(permData);
if (permDoc.exists()) {
@@ -30,9 +31,14 @@ async function createPermission(type: string) {
await setDoc(permData, {
type,
topic,
users: [],
});
}
interface PermissionsHelperList {
topic: string;
type: string;
}
export function getPermissions(userId: string | undefined, docs: Permission[]) {
if (!userId) {
return [];
@@ -52,9 +58,19 @@ export function getPermissions(userId: string | undefined, docs: Permission[]) {
}
export async function bootstrap() {
await permissions.forEach(async (type) => {
await createPermission(type);
});
await permissions
.reduce((accm: PermissionsHelperList[], permissionData) => {
return [
...accm,
...permissionData.list.map((type) => ({
topic: permissionData.topic,
type,
})),
];
}, [])
.forEach(async ({ topic, type }) => {
await createPermission(topic, type);
});
}
export async function getPermissionDoc(id: string) {