diff --git a/src/pages/settings.tsx b/src/pages/settings.tsx
index 0276d94d..0e75039d 100644
--- a/src/pages/settings.tsx
+++ b/src/pages/settings.tsx
@@ -24,8 +24,12 @@ import UserCreator from "./(admin)/UserCreator";
import CorporateGradingSystem from "./(admin)/CorporateGradingSystem";
import useGradingSystem from "@/hooks/useGrading";
import {CEFR_STEPS} from "@/resources/grading";
+import {User} from "@/interfaces/user";
+import {getUserPermissions} from "@/utils/permissions.be";
+import {Permission, PermissionType} from "@/interfaces/permissions";
+import {getUsers} from "@/utils/users.be";
-export const getServerSideProps = withIronSessionSsr(({req, res}) => {
+export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
const user = req.session.user;
if (!user || !user.isVerified) {
return {
@@ -45,14 +49,21 @@ export const getServerSideProps = withIronSessionSsr(({req, res}) => {
};
}
+ const permissions = await getUserPermissions(user.id);
+ const users = await getUsers();
+
return {
- props: {user: req.session.user},
+ props: {user, permissions, users},
};
}, sessionOptions);
-export default function Admin() {
- const {user} = useUser({redirectTo: "/login"});
- const {permissions} = usePermissions(user?.id || "");
+interface Props {
+ user: User;
+ users: User[];
+ permissions: PermissionType[];
+}
+
+export default function Admin({user, users, permissions}: Props) {
const {gradingSystem, mutate} = useGradingSystem();
const [modalOpen, setModalOpen] = useState
();
@@ -69,80 +80,78 @@ export default function Admin() {
- {user && (
-
- setModalOpen(undefined)}>
- setModalOpen(undefined)} />
-
- setModalOpen(undefined)}>
- setModalOpen(undefined)} />
-
- setModalOpen(undefined)}>
- setModalOpen(undefined)} />
-
- setModalOpen(undefined)}>
- setModalOpen(undefined)} />
-
- setModalOpen(undefined)}>
- {
- mutate({user: user.id, steps});
- setModalOpen(undefined);
- }}
- />
-
+
+ setModalOpen(undefined)}>
+ setModalOpen(undefined)} />
+
+ setModalOpen(undefined)}>
+ setModalOpen(undefined)} />
+
+ setModalOpen(undefined)}>
+ setModalOpen(undefined)} />
+
+ setModalOpen(undefined)}>
+ setModalOpen(undefined)} />
+
+ setModalOpen(undefined)}>
+ {
+ mutate({user: user.id, steps});
+ setModalOpen(undefined);
+ }}
+ />
+
-
-
- {checkAccess(user, getTypesOfUser(["teacher"]), permissions, "viewCodes") && (
-
+
+
+ {checkAccess(user, getTypesOfUser(["teacher"]), permissions, "viewCodes") && (
+
+ setModalOpen("createCode")}
+ />
+ setModalOpen("batchCreateCode")}
+ />
+ setModalOpen("createUser")}
+ />
+ setModalOpen("batchCreateUser")}
+ />
+ {checkAccess(user, ["admin", "corporate", "developer", "mastercorporate"]) && (
setModalOpen("createCode")}
+ className="w-full h-full col-span-2"
+ onClick={() => setModalOpen("gradingSystem")}
/>
- setModalOpen("batchCreateCode")}
- />
- setModalOpen("createUser")}
- />
- setModalOpen("batchCreateUser")}
- />
- {checkAccess(user, ["admin", "corporate", "developer", "mastercorporate"]) && (
- setModalOpen("gradingSystem")}
- />
- )}
-
- )}
-
-
-
- )}
+ )}
+
+ )}
+
+
+
>
);
}
diff --git a/src/utils/assignments.be.ts b/src/utils/assignments.be.ts
index 7f46718f..03be8da3 100644
--- a/src/utils/assignments.be.ts
+++ b/src/utils/assignments.be.ts
@@ -1,58 +1,36 @@
-import { app } from "@/firebase";
-import { Assignment } from "@/interfaces/results";
-import {
- collection,
- getDocs,
- getFirestore,
- query,
- where,
-} from "firebase/firestore";
+import {app} from "@/firebase";
+import {Assignment} from "@/interfaces/results";
+import {collection, getDocs, getFirestore, query, where} from "firebase/firestore";
const db = getFirestore(app);
-export const getAssignmentsByAssigner = async (
- id: string,
- startDate?: Date,
- endDate?: Date
-) => {
- const { docs } = await getDocs(
- query(
- collection(db, "assignments"),
- ...[
- where("assigner", "==", id),
- ...(startDate ? [where("startDate", ">=", startDate.toISOString())] : []),
- // firebase doesnt accept compound queries so we have to filter on the server
- // ...endDate ? [where("endDate", "<=", endDate)] : [],
- ]
- )
- );
- if (endDate) {
- return docs
- .map((x) => ({ ...(x.data() as Assignment), id: x.id }))
- .filter((x) => new Date(x.endDate) <= endDate) as Assignment[];
- }
- return docs.map((x) => ({ ...x.data(), id: x.id })) as Assignment[];
+export const getAssignmentsByAssigner = async (id: string, startDate?: Date, endDate?: Date) => {
+ const {docs} = await getDocs(
+ query(
+ collection(db, "assignments"),
+ ...[
+ where("assigner", "==", id),
+ ...(startDate ? [where("startDate", ">=", startDate.toISOString())] : []),
+ // firebase doesnt accept compound queries so we have to filter on the server
+ // ...endDate ? [where("endDate", "<=", endDate)] : [],
+ ],
+ ),
+ );
+ if (endDate) {
+ return docs.map((x) => ({...(x.data() as Assignment), id: x.id})).filter((x) => new Date(x.endDate) <= endDate) as Assignment[];
+ }
+ return docs.map((x) => ({...x.data(), id: x.id})) as Assignment[];
+};
+export const getAssignments = async () => {
+ const {docs} = await getDocs(collection(db, "assignments"));
+ return docs.map((x) => ({...x.data(), id: x.id})) as Assignment[];
};
-export const getAssignmentsByAssignerBetweenDates = async (
- id: string,
- startDate: Date,
- endDate: Date
-) => {
- const { docs } = await getDocs(
- query(collection(db, "assignments"), where("assigner", "==", id))
- );
- return docs.map((x) => ({ ...x.data(), id: x.id })) as Assignment[];
+export const getAssignmentsByAssignerBetweenDates = async (id: string, startDate: Date, endDate: Date) => {
+ const {docs} = await getDocs(query(collection(db, "assignments"), where("assigner", "==", id)));
+ return docs.map((x) => ({...x.data(), id: x.id})) as Assignment[];
};
-export const getAssignmentsByAssigners = async (
- ids: string[],
- startDate?: Date,
- endDate?: Date
-) => {
- return (
- await Promise.all(
- ids.map((id) => getAssignmentsByAssigner(id, startDate, endDate))
- )
- ).flat();
+export const getAssignmentsByAssigners = async (ids: string[], startDate?: Date, endDate?: Date) => {
+ return (await Promise.all(ids.map((id) => getAssignmentsByAssigner(id, startDate, endDate)))).flat();
};
diff --git a/src/utils/permissions.be.ts b/src/utils/permissions.be.ts
index 1f03ffcc..93dffcba 100644
--- a/src/utils/permissions.be.ts
+++ b/src/utils/permissions.be.ts
@@ -1,99 +1,89 @@
-import { app, adminApp } from "@/firebase";
-import { getAuth } from "firebase-admin/auth";
+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,
- PermissionTopic,
-} from "@/interfaces/permissions";
-import { v4 } from "uuid";
+import {collection, deleteDoc, doc, getDoc, getDocs, getFirestore, query, setDoc, where} from "firebase/firestore";
+import {Permission, PermissionType, permissions, PermissionTopic} from "@/interfaces/permissions";
+import {v4} from "uuid";
const db = getFirestore(app);
async function createPermission(topic: string, type: string) {
- const permData = doc(db, "permissions", v4());
- const permDoc = await getDoc(permData);
- if (permDoc.exists()) {
- return true;
- }
+ const permData = doc(db, "permissions", v4());
+ const permDoc = await getDoc(permData);
+ if (permDoc.exists()) {
+ return true;
+ }
- await setDoc(permData, {
- type,
- topic,
- users: [],
- });
+ await setDoc(permData, {
+ type,
+ topic,
+ users: [],
+ });
}
interface PermissionsHelperList {
- topic: string;
- type: string;
+ topic: string;
+ type: string;
}
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;
- }
+ 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;
+ return [...acc, doc.type];
+ }, []) as PermissionType[];
+ return permissions;
+}
+
+export async function getUserPermissions(id: string) {
+ const permissions = await getPermissionDocs();
+ return getPermissions(id, permissions);
}
export async function bootstrap() {
- await permissions
- .reduce((accm: PermissionsHelperList[], permissionData) => {
- return [
- ...accm,
- ...permissionData.list.map((type) => ({
- topic: permissionData.topic,
- type,
- })),
- ];
- }, [])
- .forEach(async ({ topic, type }) => {
- await createPermission(topic, 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) {
- const docRef = doc(db, "permissions", id);
- const docSnap = await getDoc(docRef);
+ const docRef = doc(db, "permissions", id);
+ const docSnap = await getDoc(docRef);
- if (docSnap.exists()) {
- return docSnap.data() as Permission;
- }
+ if (docSnap.exists()) {
+ return docSnap.data() as Permission;
+ }
- throw new Error("Permission not found");
+ 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 q = query(collection(db, "permissions"));
+ // firebase is missing something like array-not-contains
- const snapshot = await getDocs(q);
+ const snapshot = await getDocs(q);
- const docs = snapshot.docs.map((doc) => ({
- id: doc.id,
- ...doc.data(),
- })) as Permission[];
+ const docs = snapshot.docs.map((doc) => ({
+ id: doc.id,
+ ...doc.data(),
+ })) as Permission[];
- return docs;
+ return docs;
}
diff --git a/src/utils/users.be.ts b/src/utils/users.be.ts
index b50467fb..be8d1f9b 100644
--- a/src/utils/users.be.ts
+++ b/src/utils/users.be.ts
@@ -5,21 +5,23 @@ import {CorporateUser, Group, User} from "@/interfaces/user";
import {getGroupsForUser} from "./groups.be";
import {uniq, uniqBy} from "lodash";
import {getUserCodes} from "./codes.be";
+import moment from "moment";
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[];
+ id: doc.id,
+ registrationDate: moment(doc.data().registrationDate).toISOString(),
+ })) as unknown as User[];
}
export async function getUser(id: string) {
const userDoc = await getDoc(doc(db, "users", id));
- return {...userDoc.data(), id} as User;
+ return {...userDoc.data(), id, registrationDate: moment(userDoc.data()?.registrationDate).toISOString()} as unknown as User;
}
export async function getSpecificUsers(ids: string[]) {
@@ -28,9 +30,10 @@ export async function getSpecificUsers(ids: string[]) {
const snapshot = await getDocs(query(collection(db, "users"), where("id", "in", ids)));
const groups = snapshot.docs.map((doc) => ({
- id: doc.id,
...doc.data(),
- })) as User[];
+ id: doc.id,
+ registrationDate: moment(doc.data().registrationDate).toISOString(),
+ })) as unknown as User[];
return groups;
}