59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
import { app } from "@/firebase";
|
|
import { Assignment } from "@/interfaces/results";
|
|
import {
|
|
collection,
|
|
getDocs,
|
|
getFirestore,
|
|
query,
|
|
where,
|
|
} from "firebase/firestore";
|
|
|
|
const db = getFirestore(app);
|
|
|
|
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[];
|
|
}
|
|
return docs.map((x) => ({ ...x.data(), id: x.id })) as Assignment[];
|
|
};
|
|
|
|
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[];
|
|
};
|
|
|
|
export const getAssignmentsByAssigners = async (
|
|
ids: string[],
|
|
startDate?: Date,
|
|
endDate?: Date
|
|
) => {
|
|
return (
|
|
await Promise.all(
|
|
ids.map((id) => getAssignmentsByAssigner(id, startDate, endDate))
|
|
)
|
|
).flat();
|
|
};
|