import { Collection, ObjectId } from "mongodb"; import { getDb, Session } from "../index"; import type { Result } from "./results.types"; export const getResultsCollection = (): Collection => getDb().collection("results"); export const createRunId = (): string => { return `run_${new ObjectId().toString()}`; }; export const initializeResultDocument = async ( clientId: string, session?: Session, ): Promise => { const { insertedId } = await getResultsCollection().insertOne({ runId: createRunId(), date: new Date(), calls: [], sessionId: session ? session._id!.toString() : "incomingCall", sessionName: session ? session.session_name : "incomingCall", clientId, }); const result = await getResultsCollection().findOne({ _id: insertedId }); if (!result) throw new Error("Failed to initialized result"); return result; }; export const findResultById = async ( resultId: string | ObjectId, ): Promise => { return getResultsCollection().findOne({ _id: new ObjectId(resultId) }); }; export const findResultByCallSid = async ( callSid: string, ): Promise => { return getResultsCollection().findOne({ "calls.callSid": callSid }); };