Finished refactoring
This commit is contained in:
@@ -1,35 +1,28 @@
|
||||
import {app} from "@/firebase";
|
||||
import {Assignment} from "@/interfaces/results";
|
||||
import {collection, getDocs, getFirestore, query, where} from "firebase/firestore";
|
||||
import {getAllAssignersByCorporate} from "@/utils/groups.be";
|
||||
import client from "@/lib/mongodb";
|
||||
import { Assignment } from "@/interfaces/results";
|
||||
import { getAllAssignersByCorporate } from "@/utils/groups.be";
|
||||
|
||||
const db = getFirestore(app);
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getAssignmentsByAssigner = async (id: string, startDate?: Date, endDate?: Date) => {
|
||||
const {docs} = await getDocs(
|
||||
query(
|
||||
collection(db, "assignments"),
|
||||
...[
|
||||
where("assigner", "==", id),
|
||||
...(startDate ? [where("startDate", ">=", startDate.toISOString())] : []),
|
||||
// firebase doesnt accept compound queries so we have to filter on the server
|
||||
// ...endDate ? [where("endDate", "<=", endDate)] : [],
|
||||
],
|
||||
),
|
||||
);
|
||||
if (endDate) {
|
||||
return docs.map((x) => ({...(x.data() as Assignment), id: x.id})).filter((x) => new Date(x.endDate) <= endDate) as Assignment[];
|
||||
let query: any = { assigner: id };
|
||||
|
||||
if (startDate) {
|
||||
query.startDate = { $gte: startDate.toISOString() };
|
||||
}
|
||||
return docs.map((x) => ({...x.data(), id: x.id})) as Assignment[];
|
||||
|
||||
if (endDate) {
|
||||
query.endDate = { $lte: endDate.toISOString() };
|
||||
}
|
||||
|
||||
return await db.collection("assignments").find<Assignment>(query).toArray();
|
||||
};
|
||||
export const getAssignments = async () => {
|
||||
const {docs} = await getDocs(collection(db, "assignments"));
|
||||
return docs.map((x) => ({...x.data(), id: x.id})) as Assignment[];
|
||||
return await db.collection("assignments").find<Assignment>({}).toArray();
|
||||
};
|
||||
|
||||
export const getAssignmentsByAssignerBetweenDates = async (id: string, startDate: Date, endDate: Date) => {
|
||||
const {docs} = await getDocs(query(collection(db, "assignments"), where("assigner", "==", id)));
|
||||
return docs.map((x) => ({...x.data(), id: x.id})) as Assignment[];
|
||||
return await db.collection("assignments").find<Assignment>({assigner: id}).toArray();
|
||||
};
|
||||
|
||||
export const getAssignmentsByAssigners = async (ids: string[], startDate?: Date, endDate?: Date) => {
|
||||
|
||||
Reference in New Issue
Block a user