import { Filter, MongoClient } from "mongodb"; import type { Report, SnapshotContent, User } from "revolt-api"; let client: MongoClient; function mongo() { if (!client) { client = new MongoClient(process.env.MONGODB!); } return client; } export default mongo; export function fetchUserById(id: string) { return mongo().db("revolt").collection("users").findOne({ _id: id }); } export function fetchUsersById(ids: string[]) { return mongo() .db("revolt") .collection("users") .find({ _id: { $in: ids } }) .toArray(); } export function fetchReports(query: Filter = { status: "Created" }) { return mongo() .db("revolt") .collection("safety_reports") .find(query as never) .toArray(); } export function fetchReportById(id: string) { return mongo() .db("revolt") .collection("safety_reports") .findOne({ _id: id }); } export function fetchSnapshotsByReport(reportId: string) { return mongo() .db("revolt") .collection<{ content: SnapshotContent }>("safety_snapshots") .find({ report_id: reportId }) .toArray() .then((snapshots) => snapshots.map((snapshot) => snapshot!.content)); }