spb/note-spboucher-ai
Public
1import { and, eq, isNull } from "drizzle-orm";2import { db, schema } from "../../db/index";3import { indexSource, removeSource } from "../../lib/search/index";4export async function handleIndex(d: { sourceType: "page" | "attachment" | "row"; sourceId: string; workspaceId: string }) {5 if (d.sourceType === "page") {6 const [p] = await db.select().from(schema.pages).where(eq(schema.pages.id, d.sourceId));7 if (!p || p.deletedAt) { await removeSource("page", d.sourceId); return { removed: true }; }8 return indexSource(p.workspaceId, "page", p.id, p.title, { doc: p.content }, p.id);9 }10 if (d.sourceType === "attachment") {11 const [a] = await db.select().from(schema.attachments).where(eq(schema.attachments.id, d.sourceId));12 if (!a || a.deletedAt || !a.extractedText) { await removeSource("attachment", d.sourceId); return { removed: true }; }13 return indexSource(a.workspaceId, "attachment", a.id, a.filename, { text: `${a.filename}\n\n${a.extractedText}` }, a.pageId);14 }15 return { ignored: true };16}17export async function handleReindexAll(d: { workspaceId: string }) {18 const pages = await db.select({ id: schema.pages.id, title: schema.pages.title, content: schema.pages.content }).from(schema.pages).where(and(eq(schema.pages.workspaceId, d.workspaceId), isNull(schema.pages.deletedAt)));19 let n = 0; for (const p of pages) { await indexSource(d.workspaceId, "page", p.id, p.title, { doc: p.content }, p.id); n++; }20 const atts = await db.select().from(schema.attachments).where(and(eq(schema.attachments.workspaceId, d.workspaceId), isNull(schema.attachments.deletedAt)));21 for (const a of atts) if (a.extractedText) { await indexSource(d.workspaceId, "attachment", a.id, a.filename, { text: `${a.filename}\n\n${a.extractedText}` }, a.pageId); n++; }22 return { indexed: n };23}24