Created a new system for the Groups that will persist after having entities

This commit is contained in:
Tiago Ribeiro
2024-09-25 16:18:43 +01:00
parent 8c392f8b49
commit dd94228672
18 changed files with 823 additions and 136 deletions

View File

@@ -1,6 +1,6 @@
import {app} from "@/firebase";
import {Assignment} from "@/interfaces/results";
import {CorporateUser, Group, MasterCorporateUser, StudentUser, TeacherUser, Type, User} from "@/interfaces/user";
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";
@@ -71,6 +71,12 @@ export const getUsersGroups = async (ids: string[]) => {
.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");

View File

@@ -3,11 +3,20 @@
['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;
};
@@ -16,6 +25,11 @@ export const search = (text: string, fields: string[][], rows: any[]) => {
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);
}

View File

@@ -8,11 +8,14 @@ import client from "@/lib/mongodb";
const db = client.db(process.env.MONGODB_DB);
export async function getUsers() {
return await db.collection("users").find<User>({}, { projection: { _id: 0 } }).toArray();
return await db
.collection("users")
.find<User>({}, {projection: {_id: 0}})
.toArray();
}
export async function getUser(id: string): Promise<User | undefined> {
const user = await db.collection("users").findOne<User>({id: id}, { projection: { _id: 0 } });
const user = await db.collection("users").findOne<User>({id: id}, {projection: {_id: 0}});
return !!user ? user : undefined;
}
@@ -21,7 +24,7 @@ export async function getSpecificUsers(ids: string[]) {
return await db
.collection("users")
.find<User>({id: {$in: ids}}, { projection: { _id: 0 } })
.find<User>({id: {$in: ids}}, {projection: {_id: 0}})
.toArray();
}
@@ -46,6 +49,7 @@ export async function getLinkedUsers(
.skip(page && size ? page * size : 0)
.limit(size || 0)
.toArray();
const total = await db.collection("users").countDocuments(filters);
return {users, total};
}