Continued creating the entity system
This commit is contained in:
@@ -18,10 +18,18 @@ 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 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 +45,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) => {
|
||||
|
||||
35
src/utils/entities.be.ts
Normal file
35
src/utils/entities.be.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import {Entity, EntityWithRoles, Role} from "@/interfaces/entity";
|
||||
import client from "@/lib/mongodb";
|
||||
import {getRolesByEntities, getRolesByEntity} from "./roles.be";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getEntityWithRoles = async (id: string): Promise<{entity: Entity; roles: Role[]} | 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();
|
||||
};
|
||||
@@ -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";
|
||||
@@ -29,6 +29,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: groupedByModule[m]}})
|
||||
.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: ""};
|
||||
|
||||
@@ -116,3 +116,11 @@ export const getCorporateNameForStudent = async (studentID: string) => {
|
||||
|
||||
return "";
|
||||
};
|
||||
|
||||
export const getGroupsByEntity = async (id: string) => await db.collection("groups").find<Group>({entity: id}).toArray();
|
||||
|
||||
export const getGroupsByEntities = async (ids: string[]) =>
|
||||
await db
|
||||
.collection("groups")
|
||||
.find<Group>({entity: {$in: ids}})
|
||||
.toArray();
|
||||
|
||||
@@ -43,3 +43,11 @@ export const convertBase64 = (file: File) => {
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export const mapBy = <T>(obj: T[], key: keyof T) => {
|
||||
if (!obj) return undefined;
|
||||
return obj.map((i) => i[key]);
|
||||
};
|
||||
export const filterBy = <T>(obj: T[], key: keyof T, value: any) => obj.filter((i) => i[key] === value);
|
||||
|
||||
export const serialize = <T>(obj: T): T => JSON.parse(JSON.stringify(obj));
|
||||
|
||||
18
src/utils/invites.be.ts
Normal file
18
src/utils/invites.be.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import {Session} from "@/hooks/useSessions";
|
||||
import {Invite, InviteWithUsers} 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 convertInvitersToUsers = async (invite: Invite): Promise<InviteWithUsers> => ({
|
||||
...invite,
|
||||
from: (await db.collection("users").findOne<User>({id: invite.from})) ?? undefined,
|
||||
});
|
||||
14
src/utils/roles.be.ts
Normal file
14
src/utils/roles.be.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
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 getRole = async (id: string) => (await db.collection("roles").findOne<Role>({id})) ?? undefined;
|
||||
11
src/utils/sessions.be.ts
Normal file
11
src/utils/sessions.be.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
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();
|
||||
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,9 +1,11 @@
|
||||
import {CorporateUser, Group, Type, User} from "@/interfaces/user";
|
||||
import {CorporateUser, Type, User} from "@/interfaces/user";
|
||||
import {getGroupsForUser, getParticipantGroups, getUserGroups, getUsersGroups} from "./groups.be";
|
||||
import {last, uniq, uniqBy} from "lodash";
|
||||
import {uniq} from "lodash";
|
||||
import {getUserCodes} from "./codes.be";
|
||||
import moment from "moment";
|
||||
import client from "@/lib/mongodb";
|
||||
import {WithEntity} from "@/interfaces/entity";
|
||||
import {getEntity} from "./entities.be";
|
||||
import {getRole} from "./roles.be";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
@@ -14,6 +16,22 @@ export async function getUsers() {
|
||||
.toArray();
|
||||
}
|
||||
|
||||
export async function getUserWithEntity(id: string): Promise<WithEntity<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;
|
||||
@@ -28,6 +46,30 @@ export async function getSpecificUsers(ids: string[]) {
|
||||
.toArray();
|
||||
}
|
||||
|
||||
export async function getEntityUsers(id: string, limit?: number) {
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>({"entities.id": id})
|
||||
.limit(limit || 0)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
export async function countEntityUsers(id: string) {
|
||||
return await db.collection("users").countDocuments({"entities.id": id});
|
||||
}
|
||||
|
||||
export async function getEntitiesUsers(ids: string[], limit?: number) {
|
||||
return await db
|
||||
.collection("users")
|
||||
.find<User>({"entities.id": {$in: ids}})
|
||||
.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,
|
||||
|
||||
Reference in New Issue
Block a user