import { Collection, Filter, ObjectId as MongoObjectId } from "mongodb"; import { getDb, ObjectId } from "../index"; import type { CreateWebsiteUrlInput, WebsiteUrl, WebsiteUrlDoc, } from "./websiteUrls.types"; export const getWebsiteUrlsCollection = (): Collection => { return getDb().collection("websiteUrls"); }; export const getWebsiteUrlsByClient = async ( clientId: string, ): Promise => { return getWebsiteUrlsCollection() .find({ clientId }) .sort({ createdAt: 1 }) .toArray(); }; export const getWebsiteUrlById = async ( id: string, ): Promise => { return getWebsiteUrlsCollection().findOne({ _id: new ObjectId(id) }); }; export const findWebsiteUrls = async ( filter: Filter = {}, ): Promise => { return getWebsiteUrlsCollection().find(filter).toArray(); }; export const createWebsiteUrl = async ( input: CreateWebsiteUrlInput, ): Promise => { const now = new Date(); const { insertedId } = await getWebsiteUrlsCollection().insertOne({ clientId: input.clientId, name: input.name, baseUrl: input.baseUrl, createdAt: now, updatedAt: now, }); return insertedId; }; export const updateWebsiteUrl = async ( id: string, updates: Partial>, ): Promise => { return getWebsiteUrlsCollection().findOneAndUpdate( { _id: new ObjectId(id) }, { $set: { ...updates, updatedAt: new Date() } }, { returnDocument: "after" }, ); }; export const deleteWebsiteUrl = async (id: string): Promise => { const result = await getWebsiteUrlsCollection().deleteOne({ _id: new ObjectId(id), }); return result.deletedCount === 1; };