Improved some of the querying for the assignments
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
import client from "@/lib/mongodb";
|
||||
import { Assignment } from "@/interfaces/results";
|
||||
import { getAllAssignersByCorporate } from "@/utils/groups.be";
|
||||
import {Assignment} from "@/interfaces/results";
|
||||
import {getAllAssignersByCorporate} from "@/utils/groups.be";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getAssignmentsByAssigner = async (id: string, startDate?: Date, endDate?: Date) => {
|
||||
let query: any = { assigner: id };
|
||||
let query: any = {assigner: id};
|
||||
|
||||
if (startDate) {
|
||||
query.startDate = { $gte: startDate.toISOString() };
|
||||
query.startDate = {$gte: startDate.toISOString()};
|
||||
}
|
||||
|
||||
if (endDate) {
|
||||
query.endDate = { $lte: endDate.toISOString() };
|
||||
query.endDate = {$lte: endDate.toISOString()};
|
||||
}
|
||||
|
||||
return await db.collection("assignments").find<Assignment>(query).toArray();
|
||||
@@ -26,7 +26,14 @@ export const getAssignmentsByAssignerBetweenDates = async (id: string, startDate
|
||||
};
|
||||
|
||||
export const getAssignmentsByAssigners = async (ids: string[], startDate?: Date, endDate?: Date) => {
|
||||
return (await Promise.all(ids.map((id) => getAssignmentsByAssigner(id, startDate, endDate)))).flat();
|
||||
return await db
|
||||
.collection("assignments")
|
||||
.find<Assignment>({
|
||||
assigner: {$in: ids},
|
||||
...(!!startDate ? {startDate: {$gte: startDate.toISOString()}} : {}),
|
||||
...(!!endDate ? {endDate: {$lte: endDate.toISOString()}} : {}),
|
||||
})
|
||||
.toArray();
|
||||
};
|
||||
|
||||
export const getAssignmentsForCorporates = async (idsList: string[], startDate?: Date, endDate?: Date) => {
|
||||
@@ -57,4 +64,4 @@ export const getAssignmentsForCorporates = async (idsList: string[], startDate?:
|
||||
);
|
||||
|
||||
return assignments.flat();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user