Merge branch 'develop' of https://bitbucket.org/ecropdev/ielts-ui into feature/level-file-upload
This commit is contained in:
14
src/utils/assignments.be.ts
Normal file
14
src/utils/assignments.be.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
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) => {
|
||||
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[]) => {
|
||||
return (await Promise.all(ids.map(getAssignmentsByAssigner))).flat();
|
||||
};
|
||||
@@ -1,53 +1,51 @@
|
||||
import { app } from "@/firebase";
|
||||
import { CorporateUser, StudentUser, TeacherUser } from "@/interfaces/user";
|
||||
import { doc, getDoc, getFirestore, setDoc } from "firebase/firestore";
|
||||
import {app} from "@/firebase";
|
||||
import {CorporateUser, Group, StudentUser, TeacherUser} from "@/interfaces/user";
|
||||
import {collection, doc, getDoc, getDocs, getFirestore, query, setDoc, where} from "firebase/firestore";
|
||||
import moment from "moment";
|
||||
import {getUser} from "./users.be";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export const updateExpiryDateOnGroup = async (
|
||||
participantID: string,
|
||||
corporateID: string,
|
||||
) => {
|
||||
const corporateRef = await getDoc(doc(db, "users", corporateID));
|
||||
const participantRef = await getDoc(doc(db, "users", participantID));
|
||||
export const updateExpiryDateOnGroup = async (participantID: string, corporateID: string) => {
|
||||
const corporateRef = await getDoc(doc(db, "users", corporateID));
|
||||
const participantRef = await getDoc(doc(db, "users", participantID));
|
||||
|
||||
if (!corporateRef.exists() || !participantRef.exists()) return;
|
||||
if (!corporateRef.exists() || !participantRef.exists()) return;
|
||||
|
||||
const corporate = {
|
||||
...corporateRef.data(),
|
||||
id: corporateRef.id,
|
||||
} as CorporateUser;
|
||||
const participant = { ...participantRef.data(), id: participantRef.id } as
|
||||
| StudentUser
|
||||
| TeacherUser;
|
||||
const corporate = {
|
||||
...corporateRef.data(),
|
||||
id: corporateRef.id,
|
||||
} as CorporateUser;
|
||||
const participant = {...participantRef.data(), id: participantRef.id} as StudentUser | TeacherUser;
|
||||
|
||||
if (
|
||||
corporate.type !== "corporate" ||
|
||||
(participant.type !== "student" && participant.type !== "teacher")
|
||||
)
|
||||
return;
|
||||
if (corporate.type !== "corporate" || (participant.type !== "student" && participant.type !== "teacher")) return;
|
||||
|
||||
if (
|
||||
!corporate.subscriptionExpirationDate ||
|
||||
!participant.subscriptionExpirationDate
|
||||
) {
|
||||
return await setDoc(
|
||||
doc(db, "users", participant.id),
|
||||
{ subscriptionExpirationDate: null },
|
||||
{ merge: true },
|
||||
);
|
||||
}
|
||||
if (!corporate.subscriptionExpirationDate || !participant.subscriptionExpirationDate) {
|
||||
return await setDoc(doc(db, "users", participant.id), {subscriptionExpirationDate: null}, {merge: true});
|
||||
}
|
||||
|
||||
const corporateDate = moment(corporate.subscriptionExpirationDate);
|
||||
const participantDate = moment(participant.subscriptionExpirationDate);
|
||||
const corporateDate = moment(corporate.subscriptionExpirationDate);
|
||||
const participantDate = moment(participant.subscriptionExpirationDate);
|
||||
|
||||
if (corporateDate.isAfter(participantDate))
|
||||
return await setDoc(
|
||||
doc(db, "users", participant.id),
|
||||
{ subscriptionExpirationDate: corporateDate.toISOString() },
|
||||
{ merge: true },
|
||||
);
|
||||
if (corporateDate.isAfter(participantDate))
|
||||
return await setDoc(doc(db, "users", participant.id), {subscriptionExpirationDate: corporateDate.toISOString()}, {merge: true});
|
||||
|
||||
return;
|
||||
return;
|
||||
};
|
||||
|
||||
export const getUserGroups = async (id: string): Promise<Group[]> => {
|
||||
const groupDocs = await getDocs(query(collection(db, "groups"), where("admin", "==", id)));
|
||||
return groupDocs.docs.map((x) => ({...x.data(), id})) as Group[];
|
||||
};
|
||||
|
||||
export const getAllAssignersByCorporate = async (corporateID: string): Promise<string[]> => {
|
||||
const groups = await getUserGroups(corporateID);
|
||||
const groupUsers = (await Promise.all(groups.map(async (g) => await Promise.all(g.participants.map(getUser))))).flat();
|
||||
const teacherPromises = await Promise.all(
|
||||
groupUsers.map(async (u) =>
|
||||
u.type === "teacher" ? u.id : u.type === "corporate" ? [...(await getAllAssignersByCorporate(u.id)), u.id] : undefined,
|
||||
),
|
||||
);
|
||||
|
||||
return teacherPromises.filter((x) => !!x).flat() as string[];
|
||||
};
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,45 +1,42 @@
|
||||
import { PermissionType } from "@/interfaces/permissions";
|
||||
import { User, Type, userTypes } from "@/interfaces/user";
|
||||
import {PermissionType} from "@/interfaces/permissions";
|
||||
import {User, Type, userTypes} from "@/interfaces/user";
|
||||
import axios from "axios";
|
||||
|
||||
export function checkAccess(
|
||||
user: User,
|
||||
types: Type[],
|
||||
permission?: PermissionType
|
||||
) {
|
||||
if (!user) {
|
||||
return false;
|
||||
}
|
||||
export function checkAccess(user: User, types: Type[], permissions?: PermissionType[], permission?: PermissionType) {
|
||||
if (!user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if(user.type === '') {
|
||||
if (!user.type) {
|
||||
console.warn("User type is empty");
|
||||
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.length === 0) {
|
||||
console.warn("No types provided");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!types.includes(user.type)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
// 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 (!(permissions || []).includes(permission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
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);
|
||||
})
|
||||
// basicly generate a list of all types except the excluded ones
|
||||
return userTypes.filter((userType) => {
|
||||
return !types.includes(userType);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {Module} from "@/interfaces";
|
||||
import {LevelScore} from "@/constants/ielts";
|
||||
import {Stat, User} from "@/interfaces/user";
|
||||
|
||||
type Type = "academic" | "general";
|
||||
|
||||
@@ -178,3 +179,28 @@ export const getLevelLabel = (level: number) => {
|
||||
|
||||
return ["Proficiency", "C2"];
|
||||
};
|
||||
|
||||
export const averageLevelCalculator = (users: User[], studentStats: Stat[]) => {
|
||||
const formattedStats = studentStats
|
||||
.map((s) => ({
|
||||
focus: users.find((u) => u.id === s.user)?.focus,
|
||||
score: s.score,
|
||||
module: s.module,
|
||||
}))
|
||||
.filter((f) => !!f.focus);
|
||||
const bandScores = formattedStats.map((s) => ({
|
||||
module: s.module,
|
||||
level: calculateBandScore(s.score.correct, s.score.total, s.module, s.focus!),
|
||||
}));
|
||||
|
||||
const levels: {[key in Module]: number} = {
|
||||
reading: 0,
|
||||
listening: 0,
|
||||
writing: 0,
|
||||
speaking: 0,
|
||||
level: 0,
|
||||
};
|
||||
bandScores.forEach((b) => (levels[b.module] += b.level));
|
||||
|
||||
return calculateAverageLevel(levels);
|
||||
};
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
import { app } from "@/firebase";
|
||||
import {app} from "@/firebase";
|
||||
|
||||
import { collection, getDocs, getFirestore } from "firebase/firestore";
|
||||
import { User } from "@/interfaces/user";
|
||||
import {collection, doc, getDoc, 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"));
|
||||
const snapshot = await getDocs(collection(db, "users"));
|
||||
|
||||
return snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})) as User[];
|
||||
return snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})) as User[];
|
||||
}
|
||||
|
||||
export async function getUser(id: string) {
|
||||
const userDoc = await getDoc(doc(db, "users", id));
|
||||
|
||||
return {...userDoc.data(), id} as User;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,10 @@ 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.type === "mastercorporate" ? 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",
|
||||
}));
|
||||
@@ -34,3 +37,9 @@ export const exportListToExcel = (rowUsers: User[], users: User[], groups: Group
|
||||
|
||||
return `${header}\n${rowsString}`;
|
||||
};
|
||||
|
||||
export const getUserName = (user?: User) => {
|
||||
if (!user) return "N/A";
|
||||
if (user.type === "corporate" || user.type === "mastercorporate") return user.corporateInformation?.companyInformation?.name || user.name;
|
||||
return user.name;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user