Merge with develop
This commit is contained in:
16
src/utils/api.ts
Normal file
16
src/utils/api.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { User } from "@/interfaces/user";
|
||||
import { IncomingMessage, ServerResponse } from "http";
|
||||
import { IronSession } from "iron-session";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getUser } from "./users.be";
|
||||
|
||||
|
||||
export async function requestUser(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse): Promise<User | undefined> {
|
||||
if (!req.session.user) return undefined
|
||||
const user = await getUser(req.session.user.id)
|
||||
|
||||
req.session.user = user
|
||||
req.session.save()
|
||||
|
||||
return user
|
||||
}
|
||||
@@ -18,10 +18,22 @@ export const getAssignmentsByAssigner = async (id: string, startDate?: Date, end
|
||||
|
||||
return await db.collection("assignments").find<Assignment>(query).toArray();
|
||||
};
|
||||
|
||||
export const getAssignments = async () => {
|
||||
return await db.collection("assignments").find<Assignment>({}).toArray();
|
||||
};
|
||||
|
||||
export const getAssignment = async (id: string) => {
|
||||
return await db.collection("assignments").findOne<Assignment>({id});
|
||||
};
|
||||
|
||||
export const getAssignmentsByAssignee = async (id: string, filter?: {[key in keyof Partial<Assignment>]: any}) => {
|
||||
return await db
|
||||
.collection("assignments")
|
||||
.find<Assignment>({assignees: [id], ...(!filter ? {} : filter)})
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const getAssignmentsByAssignerBetweenDates = async (id: string, startDate: Date, endDate: Date) => {
|
||||
return await db.collection("assignments").find<Assignment>({assigner: id}).toArray();
|
||||
};
|
||||
@@ -37,6 +49,17 @@ export const getAssignmentsByAssigners = async (ids: string[], startDate?: Date,
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const getEntityAssignments = async (id: string) => {
|
||||
return await db.collection("assignments").find<Assignment>({entity: id}).toArray();
|
||||
};
|
||||
|
||||
export const getEntitiesAssignments = async (ids: string[]) => {
|
||||
return await db
|
||||
.collection("assignments")
|
||||
.find<Assignment>({entity: {$in: ids}})
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const getAssignmentsForCorporates = async (userType: Type, idsList: string[], startDate?: Date, endDate?: Date) => {
|
||||
const assigners = await Promise.all(
|
||||
idsList.map(async (id) => {
|
||||
|
||||
98
src/utils/entities.be.ts
Normal file
98
src/utils/entities.be.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import {Entity, EntityWithRoles, Role} from "@/interfaces/entity";
|
||||
import client from "@/lib/mongodb";
|
||||
import { ADMIN_PERMISSIONS, DEFAULT_PERMISSIONS, RolePermission } from "@/resources/entityPermissions";
|
||||
import { v4 } from "uuid";
|
||||
import {getRolesByEntities, getRolesByEntity} from "./roles.be";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getEntityWithRoles = async (id: string): Promise<EntityWithRoles | undefined> => {
|
||||
const entity = await getEntity(id);
|
||||
if (!entity) return undefined;
|
||||
|
||||
const roles = await getRolesByEntity(id);
|
||||
return {...entity, roles};
|
||||
};
|
||||
|
||||
export const getEntity = async (id: string) => {
|
||||
return (await db.collection("entities").findOne<Entity>({id})) ?? undefined;
|
||||
};
|
||||
|
||||
export const getEntitiesWithRoles = async (ids?: string[]): Promise<EntityWithRoles[]> => {
|
||||
const entities = await db
|
||||
.collection("entities")
|
||||
.find<Entity>(ids ? {id: {$in: ids}} : {})
|
||||
.toArray();
|
||||
|
||||
const roles = await getRolesByEntities(entities.map((x) => x.id));
|
||||
|
||||
return entities.map((x) => ({...x, roles: roles.filter((y) => y.entityID === x.id) || []}));
|
||||
};
|
||||
|
||||
export const getEntities = async (ids?: string[]) => {
|
||||
return await db
|
||||
.collection("entities")
|
||||
.find<Entity>(ids ? {id: {$in: ids}} : {})
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const createEntity = async (entity: Entity) => {
|
||||
await db.collection("entities").insertOne(entity)
|
||||
|
||||
const defaultRole = {
|
||||
id: v4(),
|
||||
label: "Default",
|
||||
permissions: DEFAULT_PERMISSIONS,
|
||||
isDefault: true,
|
||||
entityID: entity.id
|
||||
}
|
||||
|
||||
const adminRole = {
|
||||
id: v4(),
|
||||
label: "Admin",
|
||||
permissions: ADMIN_PERMISSIONS,
|
||||
entityID: entity.id
|
||||
}
|
||||
|
||||
await db.collection("roles").insertOne(defaultRole)
|
||||
await db.collection("roles").insertOne(adminRole)
|
||||
|
||||
return {default: defaultRole, admin: adminRole}
|
||||
}
|
||||
|
||||
export const addUserToEntity = async (user: string, entity: string, role: string) =>
|
||||
await db.collection("users").updateOne(
|
||||
{id: user},
|
||||
{
|
||||
// @ts-expect-error
|
||||
$push: {
|
||||
entities: {id: entity, role},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
export const addUsersToEntity = async (users: string[], entity: string, role: string) =>
|
||||
await db.collection("users").updateMany(
|
||||
{id: {$in: users}},
|
||||
{
|
||||
// @ts-expect-error
|
||||
$push: {
|
||||
entities: {id: entity, role},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
export const deleteEntity = async (entity: Entity) => {
|
||||
await db.collection("entities").deleteOne({id: entity.id})
|
||||
await db.collection("roles").deleteMany({entityID: entity.id})
|
||||
|
||||
await db.collection("users").updateMany(
|
||||
{"entities.id": entity.id},
|
||||
{
|
||||
// @ts-expect-error
|
||||
$pull: {
|
||||
entities: {id: entity.id},
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import {collection, getDocs, query, where, setDoc, doc, Firestore, getDoc, and} from "firebase/firestore";
|
||||
import {shuffle} from "lodash";
|
||||
import {groupBy, shuffle} from "lodash";
|
||||
import {Difficulty, Exam, InstructorGender, SpeakingExam, Variant, WritingExam} from "@/interfaces/exam";
|
||||
import {DeveloperUser, Stat, StudentUser, User} from "@/interfaces/user";
|
||||
import {Module} from "@/interfaces";
|
||||
@@ -8,6 +8,7 @@ import {getUserCorporate} from "./groups.be";
|
||||
import {Db, ObjectId} from "mongodb";
|
||||
import client from "@/lib/mongodb";
|
||||
import {MODULE_ARRAY} from "./moduleUtils";
|
||||
import { mapBy } from ".";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
@@ -29,6 +30,23 @@ export async function getSpecificExams(ids: string[]) {
|
||||
return exams;
|
||||
}
|
||||
|
||||
export const getExamsByIds = async (ids: {module: Module; id: string}[]) => {
|
||||
const groupedByModule = groupBy(ids, "module");
|
||||
const exams: Exam[] = (
|
||||
await Promise.all(
|
||||
Object.keys(groupedByModule).map(
|
||||
async (m) =>
|
||||
await db
|
||||
.collection(m)
|
||||
.find<Exam>({id: {$in: mapBy(groupedByModule[m], 'id')}})
|
||||
.toArray(),
|
||||
),
|
||||
)
|
||||
).flat();
|
||||
|
||||
return exams;
|
||||
};
|
||||
|
||||
export const getExams = async (
|
||||
db: Db,
|
||||
module: Module,
|
||||
|
||||
@@ -20,3 +20,6 @@ export const getGradingSystem = async (user: User): Promise<Grading> => {
|
||||
|
||||
return {steps: CEFR_STEPS, user: user.id};
|
||||
};
|
||||
|
||||
export const getGradingSystemByEntity = async (id: string) =>
|
||||
(await db.collection("grading").findOne<Grading>({entity: id})) || {steps: CEFR_STEPS, user: ""};
|
||||
|
||||
@@ -1,112 +1,175 @@
|
||||
import {app} from "@/firebase";
|
||||
import {Assignment} from "@/interfaces/results";
|
||||
import {CorporateUser, Group, MasterCorporateUser, StudentUser, TeacherUser, Type, User} from "@/interfaces/user";
|
||||
import { app } from "@/firebase";
|
||||
import { WithEntity } from "@/interfaces/entity";
|
||||
import { Assignment } from "@/interfaces/results";
|
||||
import { CorporateUser, Group, GroupWithUsers, MasterCorporateUser, StudentUser, TeacherUser, Type, User } from "@/interfaces/user";
|
||||
import client from "@/lib/mongodb";
|
||||
import moment from "moment";
|
||||
import {getLinkedUsers, getUser} from "./users.be";
|
||||
import {getSpecificUsers} from "./users.be";
|
||||
import { getLinkedUsers, getUser } from "./users.be";
|
||||
import { getSpecificUsers } from "./users.be";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
const addEntityToGroupPipeline = [
|
||||
{
|
||||
$lookup: {
|
||||
from: "entities",
|
||||
localField: "entity",
|
||||
foreignField: "id",
|
||||
as: "entity"
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
entity: { $arrayElemAt: ["$entity", 0] }
|
||||
}
|
||||
},
|
||||
{
|
||||
$addFields: {
|
||||
entity: {
|
||||
$cond: {
|
||||
if: { $isArray: "$entity" },
|
||||
then: undefined,
|
||||
else: "$entity"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
export const updateExpiryDateOnGroup = async (participantID: string, corporateID: string) => {
|
||||
const corporate = await db.collection("users").findOne<User>({id: corporateID});
|
||||
const participant = await db.collection("users").findOne<User>({id: participantID});
|
||||
const corporate = await db.collection("users").findOne<User>({ id: corporateID });
|
||||
const participant = await db.collection("users").findOne<User>({ id: participantID });
|
||||
|
||||
if (!corporate || !participant) return;
|
||||
if (corporate.type !== "corporate" || (participant.type !== "student" && participant.type !== "teacher")) return;
|
||||
if (!corporate || !participant) return;
|
||||
if (corporate.type !== "corporate" || (participant.type !== "student" && participant.type !== "teacher")) return;
|
||||
|
||||
if (!corporate.subscriptionExpirationDate || !participant.subscriptionExpirationDate)
|
||||
return await db.collection("users").updateOne({id: participant.id}, {$set: {subscriptionExpirationDate: null}});
|
||||
if (!corporate.subscriptionExpirationDate || !participant.subscriptionExpirationDate)
|
||||
return await db.collection("users").updateOne({ id: participant.id }, { $set: { subscriptionExpirationDate: null } });
|
||||
|
||||
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 db.collection("users").updateOne({id: participant.id}, {$set: {subscriptionExpirationDate: corporateDate.toISOString()}});
|
||||
if (corporateDate.isAfter(participantDate))
|
||||
return await db.collection("users").updateOne({ id: participant.id }, { $set: { subscriptionExpirationDate: corporateDate.toISOString() } });
|
||||
|
||||
return;
|
||||
return;
|
||||
};
|
||||
|
||||
export const getUserCorporate = async (id: string) => {
|
||||
const user = await getUser(id);
|
||||
if (!user) return undefined;
|
||||
const user = await getUser(id);
|
||||
if (!user) return undefined;
|
||||
|
||||
if (["admin", "developer"].includes(user.type)) return undefined;
|
||||
if (user.type === "mastercorporate") return user;
|
||||
if (["admin", "developer"].includes(user.type)) return undefined;
|
||||
if (user.type === "mastercorporate") return user;
|
||||
|
||||
const groups = await getParticipantGroups(id);
|
||||
const admins = await Promise.all(groups.map((x) => x.admin).map(getUser));
|
||||
const corporates = admins
|
||||
.filter((x) => (user.type === "corporate" ? x?.type === "mastercorporate" : x?.type === "corporate"))
|
||||
.filter((x) => !!x) as User[];
|
||||
const groups = await getParticipantGroups(id);
|
||||
const admins = await Promise.all(groups.map((x) => x.admin).map(getUser));
|
||||
const corporates = admins
|
||||
.filter((x) => (user.type === "corporate" ? x?.type === "mastercorporate" : x?.type === "corporate"))
|
||||
.filter((x) => !!x) as User[];
|
||||
|
||||
if (corporates.length === 0) return undefined;
|
||||
return corporates.shift() as CorporateUser | MasterCorporateUser;
|
||||
if (corporates.length === 0) return undefined;
|
||||
return corporates.shift() as CorporateUser | MasterCorporateUser;
|
||||
};
|
||||
|
||||
export const getGroup = async (id: string) => {
|
||||
return await db.collection("groups").findOne<Group>({id});
|
||||
return await db.collection("groups").findOne<Group>({ id });
|
||||
};
|
||||
|
||||
export const getGroups = async () => {
|
||||
return await db.collection("groups").find<Group>({}).toArray();
|
||||
export const getGroups = async (): Promise<WithEntity<Group>[]> => {
|
||||
return await db.collection("groups")
|
||||
.aggregate<WithEntity<Group>>(addEntityToGroupPipeline).toArray()
|
||||
};
|
||||
|
||||
export const getParticipantGroups = async (id: string) => {
|
||||
return await db.collection("groups").find<Group>({participants: id}).toArray();
|
||||
return await db.collection("groups").find<Group>({ participants: id }).toArray();
|
||||
};
|
||||
|
||||
export const getParticipantsGroups = async (ids: string[]) => {
|
||||
return await db.collection("groups").find<Group>({ participants: { $in: ids } }).toArray();
|
||||
};
|
||||
|
||||
export const getUserGroups = async (id: string): Promise<Group[]> => {
|
||||
return await db.collection("groups").find<Group>({admin: id}).toArray();
|
||||
return await db.collection("groups").find<Group>({ admin: id }).toArray();
|
||||
};
|
||||
|
||||
export const getUserNamedGroup = async (id: string, name: string) => {
|
||||
return await db.collection("groups").findOne<Group>({admin: id, name});
|
||||
return await db.collection("groups").findOne<Group>({ admin: id, name });
|
||||
};
|
||||
|
||||
export const removeParticipantFromGroup = async (id: string, user: string) => {
|
||||
return await db.collection("groups").updateOne({id}, {
|
||||
// @ts-expect-error
|
||||
$pull: {
|
||||
participants: user
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const getUsersGroups = async (ids: string[]) => {
|
||||
return await db
|
||||
.collection("groups")
|
||||
.find<Group>({admin: {$in: ids}})
|
||||
.toArray();
|
||||
return await db
|
||||
.collection("groups")
|
||||
.find<Group>({ admin: { $in: ids } })
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const convertToUsers = (group: Group, users: User[]): GroupWithUsers =>
|
||||
Object.assign(group, {
|
||||
admin: users.find((u) => u.id === group.admin),
|
||||
participants: group.participants.map((p) => users.find((u) => u.id === p)).filter((x) => !!x) as User[],
|
||||
});
|
||||
|
||||
export const getAllAssignersByCorporate = async (corporateID: string, type: Type): Promise<string[]> => {
|
||||
const linkedTeachers = await getLinkedUsers(corporateID, type, "teacher");
|
||||
const linkedCorporates = await getLinkedUsers(corporateID, type, "corporate");
|
||||
const linkedTeachers = await getLinkedUsers(corporateID, type, "teacher");
|
||||
const linkedCorporates = await getLinkedUsers(corporateID, type, "corporate");
|
||||
|
||||
return [...linkedTeachers.users.map((x) => x.id), ...linkedCorporates.users.map((x) => x.id)];
|
||||
return [...linkedTeachers.users.map((x) => x.id), ...linkedCorporates.users.map((x) => x.id)];
|
||||
};
|
||||
|
||||
export const getGroupsForEntities = async (ids: string[]) =>
|
||||
await db
|
||||
.collection("groups")
|
||||
.find<Group>({ entity: { $in: ids } })
|
||||
.toArray();
|
||||
|
||||
export const getGroupsForUser = async (admin?: string, participant?: string) => {
|
||||
if (admin && participant) return await db.collection("groups").find<Group>({admin, participant}).toArray();
|
||||
if (admin && participant) return await db.collection("groups").find<Group>({ admin, participant }).toArray();
|
||||
|
||||
if (admin) return await getUserGroups(admin);
|
||||
if (participant) return await getParticipantGroups(participant);
|
||||
if (admin) return await getUserGroups(admin);
|
||||
if (participant) return await getParticipantGroups(participant);
|
||||
|
||||
return await getGroups();
|
||||
return await getGroups();
|
||||
};
|
||||
|
||||
export const getStudentGroupsForUsersWithoutAdmin = async (admin: string, participants: string[]) => {
|
||||
return await db
|
||||
.collection("groups")
|
||||
.find<Group>({...(admin ? {admin: {$ne: admin}} : {}), ...(participants ? {participants} : {})})
|
||||
.toArray();
|
||||
return await db
|
||||
.collection("groups")
|
||||
.find<Group>({ ...(admin ? { admin: { $ne: admin } } : {}), ...(participants ? { participants } : {}) })
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const getCorporateNameForStudent = async (studentID: string) => {
|
||||
const groups = await getStudentGroupsForUsersWithoutAdmin("", [studentID]);
|
||||
if (groups.length === 0) return "";
|
||||
const groups = await getStudentGroupsForUsersWithoutAdmin("", [studentID]);
|
||||
if (groups.length === 0) return "";
|
||||
|
||||
const adminUserIds = [...new Set(groups.map((g) => g.admin))];
|
||||
const adminUsersData = (await getSpecificUsers(adminUserIds)).filter((x) => !!x) as User[];
|
||||
const adminUserIds = [...new Set(groups.map((g) => g.admin))];
|
||||
const adminUsersData = (await getSpecificUsers(adminUserIds)).filter((x) => !!x) as User[];
|
||||
|
||||
if (adminUsersData.length === 0) return "";
|
||||
const admins = adminUsersData.filter((x) => x.type === "corporate");
|
||||
if (adminUsersData.length === 0) return "";
|
||||
const admins = adminUsersData.filter((x) => x.type === "corporate");
|
||||
|
||||
if (admins.length > 0) {
|
||||
return (admins[0] as CorporateUser).corporateInformation.companyInformation.name;
|
||||
}
|
||||
if (admins.length > 0) {
|
||||
return (admins[0] as CorporateUser).corporateInformation.companyInformation.name;
|
||||
}
|
||||
|
||||
return "";
|
||||
return "";
|
||||
};
|
||||
|
||||
export const getGroupsByEntity = async (id: string) => await db.collection("groups").find<Group>({ entity: id }).toArray();
|
||||
|
||||
export const getGroupsByEntities = async (ids: string[]): Promise<WithEntity<Group>[]> =>
|
||||
await db.collection("groups")
|
||||
.aggregate<WithEntity<Group>>([
|
||||
{ $match: { entity: { $in: ids } } },
|
||||
...addEntityToGroupPipeline
|
||||
]).toArray()
|
||||
|
||||
@@ -43,3 +43,16 @@ export const convertBase64 = (file: File) => {
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export const redirect = (destination: string) => ({
|
||||
redirect: {
|
||||
destination: destination,
|
||||
permanent: false,
|
||||
},
|
||||
})
|
||||
|
||||
export const mapBy = <T, K extends keyof T>(obj: T[] | undefined, key: K) => (obj || []).map((i) => i[key] as T[K]);
|
||||
export const filterBy = <T>(obj: T[], key: keyof T, value: any) => obj.filter((i) => i[key] === value);
|
||||
export const findBy = <T>(obj: T[], key: keyof T, value: any) => obj.find((i) => i[key] === value);
|
||||
|
||||
export const serialize = <T>(obj: T): T => JSON.parse(JSON.stringify(obj));
|
||||
|
||||
19
src/utils/invites.be.ts
Normal file
19
src/utils/invites.be.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import {Session} from "@/hooks/useSessions";
|
||||
import { Entity } from "@/interfaces/entity";
|
||||
import {Invite, InviteWithEntity} from "@/interfaces/invite";
|
||||
import {User} from "@/interfaces/user";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getInvitesByInvitee = async (id: string, limit?: number) =>
|
||||
await db
|
||||
.collection("invites")
|
||||
.find<Invite>({to: id})
|
||||
.limit(limit || 0)
|
||||
.toArray();
|
||||
|
||||
export const convertInvitersToEntity = async (invite: Invite): Promise<InviteWithEntity> => ({
|
||||
...invite,
|
||||
entity: (await db.collection("entities").findOne<Entity>({id: invite.entity })) ?? undefined,
|
||||
});
|
||||
@@ -9,8 +9,8 @@ export const preventNavigation = (navDisabled: boolean, focusMode: boolean): boo
|
||||
|
||||
export const shouldRedirectHome = (user: User) => {
|
||||
if (user.status === "disabled") return true;
|
||||
if (user.isFirstLogin && user.type === "student") return true;
|
||||
if (!user.demographicInformation) return true;
|
||||
// if (user.isFirstLogin && user.type === "student") return true;
|
||||
// if (!user.demographicInformation) return true;
|
||||
if (user.subscriptionExpirationDate && moment(new Date()).isAfter(user.subscriptionExpirationDate)) return true;
|
||||
|
||||
return false;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { EntityWithRoles, Role } from "@/interfaces/entity";
|
||||
import {PermissionType} from "@/interfaces/permissions";
|
||||
import {User, Type, userTypes} from "@/interfaces/user";
|
||||
import { RolePermission } from "@/resources/entityPermissions";
|
||||
import axios from "axios";
|
||||
import { findBy, mapBy } from ".";
|
||||
|
||||
export function checkAccess(user: User, types: Type[], permissions?: PermissionType[], permission?: PermissionType) {
|
||||
if (!user) {
|
||||
@@ -8,7 +11,7 @@ export function checkAccess(user: User, types: Type[], permissions?: PermissionT
|
||||
}
|
||||
|
||||
// if(user.type === '') {
|
||||
if (!user.type) {
|
||||
if (!user?.type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -32,6 +35,32 @@ export function checkAccess(user: User, types: Type[], permissions?: PermissionT
|
||||
return true;
|
||||
}
|
||||
|
||||
export function findAllowedEntities(user: User, entities: EntityWithRoles[], permission: RolePermission) {
|
||||
if (["admin", "developer"].includes(user?.type)) return entities
|
||||
|
||||
const allowedEntities = entities.filter((e) => doesEntityAllow(user, e, permission))
|
||||
return allowedEntities
|
||||
}
|
||||
|
||||
export function findAllowedEntitiesSomePermissions(user: User, entities: EntityWithRoles[], permissions: RolePermission[]) {
|
||||
if (["admin", "developer"].includes(user?.type)) return entities
|
||||
|
||||
const allowedEntities = entities.filter((e) => permissions.some((p) => doesEntityAllow(user, e, p)))
|
||||
return allowedEntities
|
||||
}
|
||||
|
||||
export function doesEntityAllow(user: User, entity: EntityWithRoles, permission: RolePermission) {
|
||||
if (["admin", "developer"].includes(user?.type)) return true
|
||||
|
||||
const userEntity = findBy(user.entities, 'id', entity?.id)
|
||||
if (!userEntity) return false
|
||||
|
||||
const role = findBy(entity.roles, 'id', userEntity.role)
|
||||
if (!role) return false
|
||||
|
||||
return role.permissions.includes(permission)
|
||||
}
|
||||
|
||||
export function getTypesOfUser(types: Type[]) {
|
||||
// basicly generate a list of all types except the excluded ones
|
||||
return userTypes.filter((userType) => {
|
||||
|
||||
34
src/utils/roles.be.ts
Normal file
34
src/utils/roles.be.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import {Role} from "@/interfaces/entity";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getRolesByEntities = async (entityIDs: string[]) =>
|
||||
await db
|
||||
.collection("roles")
|
||||
.find<Role>({entityID: {$in: entityIDs}})
|
||||
.toArray();
|
||||
|
||||
export const getRolesByEntity = async (entityID: string) => await db.collection("roles").find<Role>({entityID}).toArray();
|
||||
|
||||
export const getRoles = async (ids?: string[]) => await db.collection("roles").find<Role>(!ids ? {} : {id: {$in: ids}}).toArray();
|
||||
export const getRole = async (id: string) => (await db.collection("roles").findOne<Role>({id})) ?? undefined;
|
||||
|
||||
export const createRole = async (role: Role) => await db.collection("roles").insertOne(role)
|
||||
export const deleteRole = async (id: string) => await db.collection("roles").deleteOne({id})
|
||||
|
||||
export const transferRole = async (previousRole: string, newRole: string) =>
|
||||
await db.collection("users")
|
||||
.updateMany(
|
||||
{ "entities.role": previousRole },
|
||||
{ $set: { 'entities.$[elem].role': newRole } },
|
||||
{ arrayFilters: [{ 'elem.role': previousRole }] }
|
||||
);
|
||||
|
||||
export const assignRoleToUsers = async (users: string[], entity: string, newRole: string) =>
|
||||
await db.collection("users")
|
||||
.updateMany(
|
||||
{ id: { $in: users } },
|
||||
{ $set: { 'entities.$[elem].role': newRole } },
|
||||
{ arrayFilters: [{ 'elem.id': entity }] }
|
||||
);
|
||||
@@ -3,19 +3,33 @@
|
||||
['companyInformation', 'companyInformation', 'name']
|
||||
]*/
|
||||
|
||||
const getFieldValue = (fields: string[], data: any): string => {
|
||||
const getFieldValue = (fields: string[], data: any): string | string[] => {
|
||||
if (fields.length === 0) return data;
|
||||
const [key, ...otherFields] = fields;
|
||||
|
||||
if (data[key]) return getFieldValue(otherFields, data[key]);
|
||||
if (Array.isArray(data[key])) {
|
||||
// If the key points to an array, like "participants", iterate through each item in the array
|
||||
return data[key]
|
||||
.map((item: any) => getFieldValue(otherFields, item)) // Get the value for each item
|
||||
.filter(Boolean); // Filter out undefined or null values
|
||||
} else if (data[key] !== undefined) {
|
||||
// If it's not an array, just go deeper in the object
|
||||
return getFieldValue(otherFields, data[key]);
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const search = (text: string, fields: string[][], rows: any[]) => {
|
||||
export const search = <T>(text: string, fields: string[][], rows: T[]) => {
|
||||
const searchText = text.toLowerCase();
|
||||
return rows.filter((row) => {
|
||||
return fields.some((fieldsKeys) => {
|
||||
const value = getFieldValue(fieldsKeys, row);
|
||||
if (Array.isArray(value)) {
|
||||
// If it's an array (e.g., participants' names), check each value in the array
|
||||
return value.some((v) => v && typeof v === "string" && v.toLowerCase().includes(searchText));
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
return value.toLowerCase().includes(searchText);
|
||||
}
|
||||
|
||||
16
src/utils/sessions.be.ts
Normal file
16
src/utils/sessions.be.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import {Session} from "@/hooks/useSessions";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getSessionsByUser = async (id: string, limit?: number) =>
|
||||
await db
|
||||
.collection("sessions")
|
||||
.find<Session>({user: id})
|
||||
.limit(limit || 0)
|
||||
.toArray();
|
||||
|
||||
export const getSessionByAssignment = async (assignmentID: string) =>
|
||||
await db
|
||||
.collection("sessions")
|
||||
.findOne<Session>({"assignment.id": assignmentID})
|
||||
12
src/utils/stats.be.ts
Normal file
12
src/utils/stats.be.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import {Stat} from "@/interfaces/user";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getStatsByUser = async (id: string) => await db.collection("stats").find<Stat>({user: id}).toArray();
|
||||
|
||||
export const getStatsByUsers = async (ids: string[]) =>
|
||||
await db
|
||||
.collection("stats")
|
||||
.find<Stat>({user: {$in: ids}})
|
||||
.toArray();
|
||||
@@ -1,94 +1,156 @@
|
||||
import {CorporateUser, Group, Type, User} from "@/interfaces/user";
|
||||
import {getGroupsForUser, getParticipantGroups, getUserGroups, getUsersGroups} from "./groups.be";
|
||||
import {last, uniq, uniqBy} from "lodash";
|
||||
import {getUserCodes} from "./codes.be";
|
||||
import moment from "moment";
|
||||
import { CorporateUser, Type, User } from "@/interfaces/user";
|
||||
import { getGroupsForUser, getParticipantGroups, getUserGroups, getUsersGroups } from "./groups.be";
|
||||
import { uniq } from "lodash";
|
||||
import { getUserCodes } from "./codes.be";
|
||||
import client from "@/lib/mongodb";
|
||||
import { EntityWithRoles, WithEntities } from "@/interfaces/entity";
|
||||
import { getEntity } from "./entities.be";
|
||||
import { getRole } from "./roles.be";
|
||||
import { findAllowedEntities } from "./permissions";
|
||||
import { mapBy } from ".";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export async function getUsers() {
|
||||
return await db.collection("users").find<User>({}, { projection: { _id: 0 } }).toArray();
|
||||
export async function getUsers(filter?: object) {
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>(filter || {}, { projection: { _id: 0 } })
|
||||
.toArray();
|
||||
}
|
||||
|
||||
export async function getUserWithEntity(id: string): Promise<WithEntities<User> | undefined> {
|
||||
const user = await db.collection("users").findOne<User>({ id: id }, { projection: { _id: 0 } });
|
||||
if (!user) return undefined;
|
||||
|
||||
const entities = await Promise.all(
|
||||
user.entities.map(async (e) => {
|
||||
const entity = await getEntity(e.id);
|
||||
const role = await getRole(e.role);
|
||||
|
||||
return { entity, role };
|
||||
}),
|
||||
);
|
||||
|
||||
return { ...user, entities };
|
||||
}
|
||||
|
||||
export async function getUser(id: string): Promise<User | undefined> {
|
||||
const user = await db.collection("users").findOne<User>({id: id}, { projection: { _id: 0 } });
|
||||
return !!user ? user : undefined;
|
||||
const user = await db.collection("users").findOne<User>({ id: id }, { projection: { _id: 0 } });
|
||||
return !!user ? user : undefined;
|
||||
}
|
||||
|
||||
export async function getSpecificUsers(ids: string[]) {
|
||||
if (ids.length === 0) return [];
|
||||
if (ids.length === 0) return [];
|
||||
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>({id: {$in: ids}}, { projection: { _id: 0 } })
|
||||
.toArray();
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>({ id: { $in: ids } }, { projection: { _id: 0 } })
|
||||
.toArray();
|
||||
}
|
||||
|
||||
export async function getEntityUsers(id: string, limit?: number, filter?: object) {
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>({ "entities.id": id, ...(filter || {}) })
|
||||
.limit(limit || 0)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
export async function countEntityUsers(id: string, filter?: object) {
|
||||
return await db.collection("users").countDocuments({ "entities.id": id, ...(filter || {}) });
|
||||
}
|
||||
|
||||
export async function getEntitiesUsers(ids: string[], filter?: object, limit?: number) {
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>({ "entities.id": { $in: ids }, ...(filter || {}) })
|
||||
.limit(limit || 0)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
export async function countEntitiesUsers(ids: string[]) {
|
||||
return await db.collection("users").countDocuments({ "entities.id": { $in: ids } });
|
||||
}
|
||||
|
||||
export async function getLinkedUsers(
|
||||
userID?: string,
|
||||
userType?: Type,
|
||||
type?: Type,
|
||||
page?: number,
|
||||
size?: number,
|
||||
sort?: string,
|
||||
direction?: "asc" | "desc",
|
||||
userID?: string,
|
||||
userType?: Type,
|
||||
type?: Type,
|
||||
page?: number,
|
||||
size?: number,
|
||||
sort?: string,
|
||||
direction?: "asc" | "desc",
|
||||
) {
|
||||
const filters = {
|
||||
...(!!type ? {type} : {}),
|
||||
};
|
||||
const filters = {
|
||||
...(!!type ? { type } : {}),
|
||||
};
|
||||
|
||||
if (!userID || userType === "admin" || userType === "developer") {
|
||||
const users = await db
|
||||
.collection("users")
|
||||
.find<User>(filters)
|
||||
.sort(sort ? {[sort]: direction === "desc" ? -1 : 1} : {})
|
||||
.skip(page && size ? page * size : 0)
|
||||
.limit(size || 0)
|
||||
.toArray();
|
||||
const total = await db.collection("users").countDocuments(filters);
|
||||
return {users, total};
|
||||
}
|
||||
if (!userID || userType === "admin" || userType === "developer") {
|
||||
const users = await db
|
||||
.collection("users")
|
||||
.find<User>(filters)
|
||||
.sort(sort ? { [sort]: direction === "desc" ? -1 : 1 } : {})
|
||||
.skip(page && size ? page * size : 0)
|
||||
.limit(size || 0)
|
||||
.toArray();
|
||||
|
||||
const adminGroups = await getUserGroups(userID);
|
||||
const groups = await getUsersGroups(adminGroups.flatMap((x) => x.participants));
|
||||
const belongingGroups = await getParticipantGroups(userID);
|
||||
const total = await db.collection("users").countDocuments(filters);
|
||||
return { users, total };
|
||||
}
|
||||
|
||||
const participants = uniq([
|
||||
...adminGroups.flatMap((x) => x.participants),
|
||||
...(userType === "mastercorporate" ? groups.flat().flatMap((x) => x.participants) : []),
|
||||
...(userType === "teacher" ? belongingGroups.flatMap((x) => x.participants) : []),
|
||||
]);
|
||||
const adminGroups = await getUserGroups(userID);
|
||||
const groups = await getUsersGroups(adminGroups.flatMap((x) => x.participants));
|
||||
const belongingGroups = await getParticipantGroups(userID);
|
||||
|
||||
// ⨯ [FirebaseError: Invalid Query. A non-empty array is required for 'in' filters.] {
|
||||
if (participants.length === 0) return {users: [], total: 0};
|
||||
const participants = uniq([
|
||||
...adminGroups.flatMap((x) => x.participants),
|
||||
...(userType === "mastercorporate" ? groups.flat().flatMap((x) => x.participants) : []),
|
||||
...(userType === "teacher" ? belongingGroups.flatMap((x) => x.participants) : []),
|
||||
]);
|
||||
|
||||
const users = await db
|
||||
.collection("users")
|
||||
.find<User>({...filters, id: {$in: participants}})
|
||||
.skip(page && size ? page * size : 0)
|
||||
.limit(size || 0)
|
||||
.toArray();
|
||||
const total = await db.collection("users").countDocuments({...filters, id: {$in: participants}});
|
||||
// ⨯ [FirebaseError: Invalid Query. A non-empty array is required for 'in' filters.] {
|
||||
if (participants.length === 0) return { users: [], total: 0 };
|
||||
|
||||
return {users, total};
|
||||
const users = await db
|
||||
.collection("users")
|
||||
.find<User>({ ...filters, id: { $in: participants } })
|
||||
.skip(page && size ? page * size : 0)
|
||||
.limit(size || 0)
|
||||
.toArray();
|
||||
const total = await db.collection("users").countDocuments({ ...filters, id: { $in: participants } });
|
||||
|
||||
return { users, total };
|
||||
}
|
||||
|
||||
export async function getUserBalance(user: User) {
|
||||
const codes = await getUserCodes(user.id);
|
||||
if (user.type !== "corporate" && user.type !== "mastercorporate") return codes.length;
|
||||
const codes = await getUserCodes(user.id);
|
||||
if (user.type !== "corporate" && user.type !== "mastercorporate") return codes.length;
|
||||
|
||||
const groups = await getGroupsForUser(user.id);
|
||||
const participants = uniq(groups.flatMap((x) => x.participants));
|
||||
const groups = await getGroupsForUser(user.id);
|
||||
const participants = uniq(groups.flatMap((x) => x.participants));
|
||||
|
||||
if (user.type === "corporate") return participants.length + codes.filter((x) => !participants.includes(x.userId || "")).length;
|
||||
if (user.type === "corporate") return participants.length + codes.filter((x) => !participants.includes(x.userId || "")).length;
|
||||
|
||||
const participantUsers = await Promise.all(participants.map(getUser));
|
||||
const corporateUsers = participantUsers.filter((x) => x?.type === "corporate") as CorporateUser[];
|
||||
const participantUsers = await Promise.all(participants.map(getUser));
|
||||
const corporateUsers = participantUsers.filter((x) => x?.type === "corporate") as CorporateUser[];
|
||||
|
||||
return (
|
||||
corporateUsers.reduce((acc, curr) => acc + curr.corporateInformation?.companyInformation?.userAmount || 0, 0) +
|
||||
corporateUsers.length +
|
||||
codes.filter((x) => !participants.includes(x.userId || "") && !corporateUsers.map((u) => u.id).includes(x.userId || "")).length
|
||||
);
|
||||
return (
|
||||
corporateUsers.reduce((acc, curr) => acc + curr.corporateInformation?.companyInformation?.userAmount || 0, 0) +
|
||||
corporateUsers.length +
|
||||
codes.filter((x) => !participants.includes(x.userId || "") && !corporateUsers.map((u) => u.id).includes(x.userId || "")).length
|
||||
);
|
||||
}
|
||||
|
||||
export const filterAllowedUsers = async (user: User, entities: EntityWithRoles[]) => {
|
||||
const studentsAllowedEntities = findAllowedEntities(user, entities, 'view_students')
|
||||
const teachersAllowedEntities = findAllowedEntities(user, entities, 'view_teachers')
|
||||
const corporateAllowedEntities = findAllowedEntities(user, entities, 'view_corporates')
|
||||
const masterCorporateAllowedEntities = findAllowedEntities(user, entities, 'view_mastercorporates')
|
||||
|
||||
const students = await getEntitiesUsers(mapBy(studentsAllowedEntities, 'id'), {type: "student"})
|
||||
const teachers = await getEntitiesUsers(mapBy(teachersAllowedEntities, 'id'), {type: "teacher"})
|
||||
const corporates = await getEntitiesUsers(mapBy(corporateAllowedEntities, 'id'), {type: "corporate"})
|
||||
const masterCorporates = await getEntitiesUsers(mapBy(masterCorporateAllowedEntities, 'id'), {type: "mastercorporate"})
|
||||
|
||||
return [...students, ...teachers, ...corporates, ...masterCorporates]
|
||||
}
|
||||
|
||||
@@ -1,45 +1,51 @@
|
||||
import {Group, User} from "@/interfaces/user";
|
||||
import {getUserCompanyName, USER_TYPE_LABELS} from "@/resources/user";
|
||||
import {capitalize} from "lodash";
|
||||
import { EntityWithRoles, WithLabeledEntities } from "@/interfaces/entity";
|
||||
import { Group, User } from "@/interfaces/user";
|
||||
import { getUserCompanyName, USER_TYPE_LABELS } from "@/resources/user";
|
||||
import { capitalize } from "lodash";
|
||||
import moment from "moment";
|
||||
import { mapBy } from ".";
|
||||
import { findAllowedEntities } from "./permissions";
|
||||
import { getEntitiesUsers } from "./users.be";
|
||||
|
||||
export interface UserListRow {
|
||||
name: string;
|
||||
email: string;
|
||||
type: string;
|
||||
companyName: string;
|
||||
expiryDate: string;
|
||||
verified: string;
|
||||
country: string;
|
||||
phone: string;
|
||||
employmentPosition: string;
|
||||
gender: string;
|
||||
name: string;
|
||||
email: string;
|
||||
type: string;
|
||||
entities: string;
|
||||
expiryDate: string;
|
||||
verified: string;
|
||||
country: string;
|
||||
phone: string;
|
||||
employmentPosition: string;
|
||||
gender: string;
|
||||
}
|
||||
|
||||
export const exportListToExcel = (rowUsers: User[], users: User[], groups: Group[]) => {
|
||||
const rows: UserListRow[] = rowUsers.map((user) => ({
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
type: USER_TYPE_LABELS[user.type],
|
||||
companyName: getUserCompanyName(user, users, groups),
|
||||
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",
|
||||
gender: user.demographicInformation?.gender ? capitalize(user.demographicInformation.gender) : "N/A",
|
||||
verified: user.isVerified?.toString() || "FALSE",
|
||||
}));
|
||||
const header = "Name,Email,Type,Company Name,Expiry Date,Country,Phone,Employment/Department,Gender,Verification";
|
||||
const rowsString = rows.map((x) => Object.values(x).join(",")).join("\n");
|
||||
export const exportListToExcel = (rowUsers: WithLabeledEntities<User>[]) => {
|
||||
const rows: UserListRow[] = rowUsers.map((user) => ({
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
type: USER_TYPE_LABELS[user.type],
|
||||
entities: user.entities.map((e) => e.label).join(', '),
|
||||
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",
|
||||
gender: user.demographicInformation?.gender ? capitalize(user.demographicInformation.gender) : "N/A",
|
||||
verified: user.isVerified?.toString() || "FALSE",
|
||||
}));
|
||||
const header = "Name,Email,Type,Entities,Expiry Date,Country,Phone,Employment/Department,Gender,Verification";
|
||||
const rowsString = rows.map((x) => Object.values(x).join(",")).join("\n");
|
||||
|
||||
return `${header}\n${rowsString}`;
|
||||
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;
|
||||
if (!user) return "N/A";
|
||||
if (user.type === "corporate" || user.type === "mastercorporate") return user.corporateInformation?.companyInformation?.name || user.name;
|
||||
return user.name;
|
||||
};
|
||||
|
||||
export const isAdmin = (user: User) => ["admin", "developer"].includes(user.type)
|
||||
|
||||
Reference in New Issue
Block a user