import { BULK_REINDEX_SUPPORTED_INDEX_NAMES, crmContactsIndex, kbArticlesIndex, projectsIndex, usersIndex } from "@hivegrid/search"; import type { SearchIndexerBulkPager, SearchIndexerDependencies } from "./search-indexer.js"; type KnowledgeBaseArticleRow = { id: string; knowledgeBaseId: string; slug: string; title: string; description: string; contentPublishedAsText: string; language: string; publishedAt: Date | string | null; createdAt: Date | string; updatedAt: Date | string | null; knowledgeBase: { tenantId: string | null }; }; // CRM-contact search rows now come from the typed `Kontakt` table (R-P6 / R-P6B-β, // AD-2) instead of the removed generic entity-builder `Row` engine. type KontaktSearchRow = { id: string; tenantId: string; kontaktTyp: string; vorname: string | null; nachname: string | null; orgName: string | null; primaerEmail: string | null; kontaktStatus: string; zustaendigerUserId: string | null; createdByUserId: string | null; createdAt: Date | string; updatedAt: Date | string; }; const TENANT_USER_STATUS_ACTIVE = 2; type TenantUserSearchRow = { id: string; tenantId: string; userId: string; type: number; status: number; user: { id: string; createdAt: Date | string; email: string; firstName: string; lastName: string; active: boolean; admin: { userId: string } | null; logs: { createdAt: Date | string }[]; }; }; type ProjektSearchRow = { id: string; tenantId: string; projektNummer: string; projektname: string; beschreibung: string | null; projekttyp: string | null; quelle: string | null; pipelineId: string; stageId: string; zustaendigePersonId: string | null; projektleiterId: string | null; archivedAt: Date | string | null; createdAt: Date | string; updatedAt: Date | string; deadline: Date | string | null; potenziellerAuftragswert: unknown; auftragsvolumen: unknown; }; export type SearchIndexerBulkPagerPrisma = { knowledgeBaseArticle: { findMany(args: { where: { id?: { gt: string }; knowledgeBase: { tenantId: string }; }; orderBy: { id: "asc" }; take: number; select: { id: true; knowledgeBaseId: true; slug: true; title: true; description: true; contentPublishedAsText: true; language: true; publishedAt: true; createdAt: true; updatedAt: true; knowledgeBase: { select: { tenantId: true } }; }; }): Promise; }; kontakt: { findMany(args: { where: { id?: { gt: string }; tenantId: string; archivedAt: null; }; orderBy: { id: "asc" }; take: number; select: { id: true; tenantId: true; kontaktTyp: true; vorname: true; nachname: true; orgName: true; primaerEmail: true; kontaktStatus: true; zustaendigerUserId: true; createdByUserId: true; createdAt: true; updatedAt: true; }; }): Promise; }; tenantUser: { findMany(args: { where: { id?: { gt: string }; tenantId: string; }; orderBy: { id: "asc" }; take: number; select: { id: true; tenantId: true; userId: true; type: true; status: true; user: { select: { id: true; createdAt: true; email: true; firstName: true; lastName: true; active: true; admin: { select: { userId: true } }; logs: { where: { action: "Login" }; orderBy: { createdAt: "desc" }; take: 1; select: { createdAt: true }; }; }; }; }; }): Promise; }; projekt: { findMany(args: { where: { id?: { gt: string }; tenantId: string; archivedAt: null; }; orderBy: { id: "asc" }; take: number; select: { id: true; tenantId: true; projektNummer: true; projektname: true; beschreibung: true; projekttyp: true; quelle: true; pipelineId: true; stageId: true; zustaendigePersonId: true; projektleiterId: true; archivedAt: true; createdAt: true; updatedAt: true; deadline: true; potenziellerAuftragswert: true; auftragsvolumen: true; }; }): Promise; }; }; export function createSearchIndexerBulkPagers(input: { prisma: SearchIndexerBulkPagerPrisma }): NonNullable { return { [kbArticlesIndex.name]: createKbArticlesPager(input.prisma), [crmContactsIndex.name]: createCrmContactsPager(input.prisma), [usersIndex.name]: createUsersPager(input.prisma), [projectsIndex.name]: createProjectsPager(input.prisma), }; } export const registeredSearchIndexerBulkPagerNames: readonly string[] = BULK_REINDEX_SUPPORTED_INDEX_NAMES; function createKbArticlesPager(prisma: SearchIndexerBulkPagerPrisma): SearchIndexerBulkPager { return async ({ tenantId, cursor, pageSize }) => { const rows = await prisma.knowledgeBaseArticle.findMany({ where: { ...(cursor ? { id: { gt: cursor } } : {}), knowledgeBase: { tenantId }, }, orderBy: { id: "asc" }, take: pageSize, select: { id: true, knowledgeBaseId: true, slug: true, title: true, description: true, contentPublishedAsText: true, language: true, publishedAt: true, createdAt: true, updatedAt: true, knowledgeBase: { select: { tenantId: true } }, }, }); return { documents: rows .filter((row) => row.knowledgeBase.tenantId === tenantId) .map((row) => ({ id: row.id, tenantId, knowledgeBaseId: row.knowledgeBaseId, slug: row.slug, title: row.title, summary: row.description, content: row.contentPublishedAsText, tags: [], language: row.language, isPublished: row.publishedAt !== null, createdAt: serializeDate(row.createdAt), updatedAt: serializeDate(row.updatedAt), publishedAt: serializeDate(row.publishedAt), })), nextCursor: rows.length === pageSize ? (rows[rows.length - 1]?.id ?? null) : null, }; }; } function createCrmContactsPager(prisma: SearchIndexerBulkPagerPrisma): SearchIndexerBulkPager { return async ({ tenantId, cursor, pageSize }) => { const rows = await prisma.kontakt.findMany({ where: { ...(cursor ? { id: { gt: cursor } } : {}), tenantId, archivedAt: null, }, orderBy: { id: "asc" }, take: pageSize, select: { id: true, tenantId: true, kontaktTyp: true, vorname: true, nachname: true, orgName: true, primaerEmail: true, kontaktStatus: true, zustaendigerUserId: true, createdByUserId: true, createdAt: true, updatedAt: true, }, }); return { documents: rows.filter((row) => row.tenantId === tenantId).map((row) => kontaktToContactDocument(row, tenantId)), nextCursor: rows.length === pageSize ? (rows[rows.length - 1]?.id ?? null) : null, }; }; } function createUsersPager(prisma: SearchIndexerBulkPagerPrisma): SearchIndexerBulkPager { return async ({ tenantId, cursor, pageSize }) => { const rows = await prisma.tenantUser.findMany({ where: { ...(cursor ? { id: { gt: cursor } } : {}), tenantId, }, orderBy: { id: "asc" }, take: pageSize, select: { id: true, tenantId: true, userId: true, type: true, status: true, user: { select: { id: true, createdAt: true, email: true, firstName: true, lastName: true, active: true, admin: { select: { userId: true } }, logs: { where: { action: "Login" }, orderBy: { createdAt: "desc" }, take: 1, select: { createdAt: true }, }, }, }, }, }); return { documents: rows.filter((row) => row.tenantId === tenantId).map((row) => tenantUserToUserDocument(row, tenantId)), nextCursor: rows.length === pageSize ? (rows[rows.length - 1]?.id ?? null) : null, }; }; } function createProjectsPager(prisma: SearchIndexerBulkPagerPrisma): SearchIndexerBulkPager { return async ({ tenantId, cursor, pageSize }) => { const rows = await prisma.projekt.findMany({ where: { ...(cursor ? { id: { gt: cursor } } : {}), tenantId, archivedAt: null, }, orderBy: { id: "asc" }, take: pageSize, select: { id: true, tenantId: true, projektNummer: true, projektname: true, beschreibung: true, projekttyp: true, quelle: true, pipelineId: true, stageId: true, zustaendigePersonId: true, projektleiterId: true, archivedAt: true, createdAt: true, updatedAt: true, deadline: true, potenziellerAuftragswert: true, auftragsvolumen: true, }, }); return { documents: rows.filter((row) => row.tenantId === tenantId).map((row) => projektToProjectDocument(row, tenantId)), nextCursor: rows.length === pageSize ? (rows[rows.length - 1]?.id ?? null) : null, }; }; } function tenantUserToUserDocument(row: TenantUserSearchRow, tenantId: string): Record & { id: string; tenantId: string } { return { id: row.user.id, tenantId, firstName: row.user.firstName, lastName: row.user.lastName, email: row.user.email, type: row.type, admin: row.user.admin !== null, active: row.user.active && row.status === TENANT_USER_STATUS_ACTIVE, createdAt: serializeDate(row.user.createdAt), lastLoginAt: serializeDate(row.user.logs[0]?.createdAt ?? null), }; } function kontaktToContactDocument(row: KontaktSearchRow, tenantId: string): Record & { id: string; tenantId: string } { // Project the typed Kontakt columns onto the `crm-contacts` Meilisearch document // keys (German column → English document field), mirroring indexedEntities. return { id: row.id, tenantId, entityType: row.kontaktTyp === "organisation" ? "company" : "contact", firstName: row.vorname ?? "", lastName: row.nachname ?? "", email: row.primaerEmail ?? "", company: row.orgName ?? "", jobTitle: "", status: row.kontaktStatus, ownerId: row.zustaendigerUserId ?? row.createdByUserId ?? "", createdAt: serializeDate(row.createdAt), updatedAt: serializeDate(row.updatedAt), }; } function projektToProjectDocument(row: ProjektSearchRow, tenantId: string): Record & { id: string; tenantId: string } { return { id: row.id, tenantId, projectNumber: row.projektNummer, name: row.projektname, description: row.beschreibung ?? "", type: row.projekttyp ?? "", source: row.quelle ?? "", pipelineId: row.pipelineId, stageId: row.stageId, ownerId: row.zustaendigePersonId ?? "", projectLeadId: row.projektleiterId ?? "", archived: row.archivedAt !== null, createdAt: serializeDate(row.createdAt), updatedAt: serializeDate(row.updatedAt), deadline: serializeDate(row.deadline), potentialValue: serializeDecimal(row.potenziellerAuftragswert), orderVolume: serializeDecimal(row.auftragsvolumen), }; } function serializeDecimal(value: unknown): number | null { if (value === null || value === undefined) return null; if (typeof value === "number") return value; if (typeof value === "string") return Number(value); if (typeof value === "object" && "toString" in value && typeof value.toString === "function") { return Number(value.toString()); } return null; } function serializeDate(value: Date | string | null): string | null { if (value === null) return null; return value instanceof Date ? value.toISOString() : value; }