import type { Collection, ObjectId, OptionalId, WithId } from "mongodb"; import { getDb } from "../index"; import type { Flow, FlowUpdateParams } from "./flows.types"; export const getFlowsCollection = (): Collection => { return getDb().collection("flows"); }; export const findFlowById = ( flowId: ObjectId, clientId: string, ): Promise | null> => { return getFlowsCollection().findOne({ _id: flowId, clientId }); }; export const createFlow = async (flow: Flow): Promise> => { const { insertedId } = await getFlowsCollection().insertOne( flow as OptionalId, ); return { ...flow, _id: insertedId }; }; /** Returns false when no flow matches the id for this client. */ export const updateFlowById = async ( flowId: ObjectId, clientId: string, update: FlowUpdateParams, ): Promise => { const { matchedCount } = await getFlowsCollection().updateOne( { _id: flowId, clientId }, { $set: { ...update, updatedAt: new Date() } }, ); return matchedCount > 0; };