Finished refactoring
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
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 client from "@/lib/mongodb";
|
||||
import {Permission, PermissionType, permissions, PermissionTopic} from "@/interfaces/permissions";
|
||||
import {v4} from "uuid";
|
||||
|
||||
const db = getFirestore(app);
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
async function createPermission(topic: string, type: string) {
|
||||
const permData = doc(db, "permissions", v4());
|
||||
/*const permData = doc(db, "permissions", v4());
|
||||
const permDoc = await getDoc(permData);
|
||||
|
||||
if (permDoc.exists()) {
|
||||
return true;
|
||||
}
|
||||
}*/
|
||||
|
||||
await setDoc(permData, {
|
||||
await db.collection("permissions").insertOne({
|
||||
id: v4(),
|
||||
type,
|
||||
topic,
|
||||
users: [],
|
||||
@@ -48,7 +47,7 @@ export async function getUserPermissions(id: string) {
|
||||
}
|
||||
|
||||
export async function bootstrap() {
|
||||
await permissions
|
||||
permissions
|
||||
.reduce((accm: PermissionsHelperList[], permissionData) => {
|
||||
return [
|
||||
...accm,
|
||||
@@ -64,26 +63,18 @@ export async function bootstrap() {
|
||||
}
|
||||
|
||||
export async function getPermissionDoc(id: string) {
|
||||
const docRef = doc(db, "permissions", id);
|
||||
const docSnap = await getDoc(docRef);
|
||||
const perm = await db.collection("permissions").findOne<Permission>({ id: id });
|
||||
|
||||
if (docSnap.exists()) {
|
||||
return docSnap.data() as Permission;
|
||||
if (perm) {
|
||||
return perm;
|
||||
}
|
||||
|
||||
throw new Error("Permission not found");
|
||||
}
|
||||
|
||||
export async function getPermissionDocs() {
|
||||
const q = query(collection(db, "permissions"));
|
||||
// TODO: Don't know if this was finished
|
||||
// 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;
|
||||
return await db.collection("permissions").find<Permission>({}).toArray();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user