import { CatalogError, foundationErrors, requirePermission } from "@hivegrid/foundation"; import { createSearchClient, crmContactsIndex, filesIndex, projectsIndex, searchWithFallback, tasksIndex, usersIndex, type SearchDocument, type SearchHit, } from "@hivegrid/search"; import { appPath } from "~/utils/app/CanonicalUrlUtils"; import { tenantHasModule } from "~/utils/connector/tenantConnector.server"; import { db, dbRead } from "~/utils/db.server"; import { getPermissionsByUser } from "~/utils/db/permissions/userRoles.db.server"; import { requireAuth } from "~/utils/loaders.middleware"; import { getTenantIdFromUrl } from "~/utils/services/.server/urlService"; import { getUserInfo } from "~/utils/session.server"; export const SEARCH_MODULE_KEY = "search"; export const SEARCH_USE_PERMISSION = "search.use"; export type GlobalSearchEntityType = "contact" | "company" | "lead" | "project" | "file" | "task" | "user"; export type GlobalSearchResult = { id: string; entityType: GlobalSearchEntityType; title: string; subtitle: string; href: string; permissionSubject: string; updatedAt: string | null; }; export type RecentSearchItem = { id: string; queryText: string; resultCount: number; createdAt: string; }; export type GlobalSearchResponse = { query: string; results: GlobalSearchResult[]; recentSearches: RecentSearchItem[]; hiddenEntityTypes: GlobalSearchEntityType[]; latencyHint: string; }; type SearchContext = { tenantId: string; tenantSlug: string; userId: string; permissions: string[]; }; type SearchInput = { request: Request; params: Record; query: string; limit?: number; entityTypes?: GlobalSearchEntityType[]; recordRecent?: boolean; }; type EntityAccess = { allowed: boolean; permissionSubject: string; all?: boolean; own?: boolean; }; const LATENCY_HINT = "Suchindex-Aktualisierungen koennen bis zu 10 Sekunden dauern."; const DEFAULT_LIMIT = 20; const RECENT_LIMIT = 6; export async function loadSearchContext({ request, params }: { request: Request; params: Record }): Promise { await requireAuth({ request, params }); const [tenantId, userInfo] = await Promise.all([getTenantIdFromUrl(params), getUserInfo(request)]); if (!tenantId || !userInfo.userId) { throw new CatalogError(foundationErrors.notFound); } const [hasModule, permissions] = await Promise.all([ tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: SEARCH_MODULE_KEY }), getPermissionsByUser(userInfo.userId, tenantId), ]); if (!hasModule) { throw new CatalogError(foundationErrors.notFound, { module: SEARCH_MODULE_KEY }); } requirePermission({ tenantId, userId: userInfo.userId, permissions }, SEARCH_USE_PERMISSION); return { tenantId, tenantSlug: params.tenant ?? tenantId, userId: userInfo.userId, permissions, }; } export async function runGlobalSearch(input: SearchInput): Promise { const ctx = await loadSearchContext({ request: input.request, params: input.params }); const query = input.query.trim(); const limit = clampLimit(input.limit); const access = buildAccessMap(ctx.permissions); const requestedTypes = input.entityTypes?.length ? new Set(input.entityTypes) : null; const searchableTypes = (Object.keys(access) as GlobalSearchEntityType[]).filter((type) => access[type].allowed && (!requestedTypes || requestedTypes.has(type))); const hiddenEntityTypes = (Object.keys(access) as GlobalSearchEntityType[]).filter((type) => !access[type].allowed); const recentSearches = await loadRecentSearches(ctx); if (!query) { return { query, results: [], recentSearches, hiddenEntityTypes, latencyHint: LATENCY_HINT }; } const [sqlResults, meiliResults] = await Promise.all([ searchSqlFallback({ ctx, access, query, limit, searchableTypes }), searchMeili({ ctx, access, query, limit, searchableTypes }), ]); const results = mergeResults([...meiliResults, ...sqlResults]).slice(0, limit); if (input.recordRecent !== false) { await recordRecentSearch({ ctx, query, resultCount: results.length, entityTypes: searchableTypes }); await mirrorSearchIndexEntries({ ctx, results }); } return { query, results, recentSearches, hiddenEntityTypes, latencyHint: LATENCY_HINT }; } function buildAccessMap(permissions: string[]): Record { const granted = new Set(permissions); return { contact: { allowed: granted.has("crm.read_all") || granted.has("crm.read_own"), all: granted.has("crm.read_all"), own: granted.has("crm.read_own"), permissionSubject: granted.has("crm.read_all") ? "crm.read_all" : "crm.read_own", }, company: { allowed: granted.has("crm.read_all") || granted.has("crm.read_own"), all: granted.has("crm.read_all"), own: granted.has("crm.read_own"), permissionSubject: granted.has("crm.read_all") ? "crm.read_all" : "crm.read_own", }, lead: { allowed: granted.has("leads.read_all") || granted.has("leads.read_own"), all: granted.has("leads.read_all"), own: granted.has("leads.read_own"), permissionSubject: granted.has("leads.read_all") ? "leads.read_all" : "leads.read_own", }, project: { allowed: granted.has("projects.read_all") || granted.has("projects.read_own") || granted.has("projects.read_own_member") || granted.has("projects.read_own_pl"), all: granted.has("projects.read_all"), own: granted.has("projects.read_own") || granted.has("projects.read_own_member") || granted.has("projects.read_own_pl"), permissionSubject: granted.has("projects.read_all") ? "projects.read_all" : "projects.read_own", }, file: { allowed: granted.has("files.read_all") || granted.has("files.read_own"), all: granted.has("files.read_all"), own: granted.has("files.read_own"), permissionSubject: granted.has("files.read_all") ? "files.read_all" : "files.read_own", }, task: { allowed: granted.has("tasks.read_all") || granted.has("tasks.read_own"), all: granted.has("tasks.read_all"), own: granted.has("tasks.read_own"), permissionSubject: granted.has("tasks.read_all") ? "tasks.read_all" : "tasks.read_own", }, user: { allowed: granted.has("app.settings.members.view"), all: granted.has("app.settings.members.view"), permissionSubject: "app.settings.members.view", }, }; } async function searchSqlFallback(input: { ctx: SearchContext; access: Record; query: string; limit: number; searchableTypes: GlobalSearchEntityType[]; }): Promise { const jobs = input.searchableTypes.map((entityType) => { switch (entityType) { case "contact": return searchContacts(input, "contact"); case "company": return searchContacts(input, "company"); case "lead": return searchLeads(input); case "project": return searchProjects(input); case "file": return searchFiles(input); case "task": return searchTasks(input); case "user": return searchUsers(input); } }); const chunks = await Promise.all(jobs); return chunks.flat().sort(compareUpdatedDesc).slice(0, input.limit); } async function searchContacts( { ctx, access, query, limit }: Pick[0], "ctx" | "access" | "query" | "limit">, entityType: "contact" | "company" ) { const entityAccess = access[entityType]; const rows = await dbRead.kontakt.findMany({ where: { tenantId: ctx.tenantId, archivedAt: null, ...(entityType === "company" ? { kontaktTyp: "organisation" as const } : { kontaktTyp: "privatperson" as const }), AND: [ entityAccess.all ? {} : { OR: [{ zustaendigerUserId: ctx.userId }, { createdByUserId: ctx.userId }] }, { OR: [ { vorname: { contains: query, mode: "insensitive" } }, { nachname: { contains: query, mode: "insensitive" } }, { orgName: { contains: query, mode: "insensitive" } }, { primaerEmail: { contains: query, mode: "insensitive" } }, { primaerTelefon: { contains: query, mode: "insensitive" } }, ], }, ], }, orderBy: { updatedAt: "desc" }, take: limit, select: { id: true, vorname: true, nachname: true, orgName: true, primaerEmail: true, updatedAt: true }, }); return rows.map((row) => ({ id: row.id, entityType, title: entityType === "company" ? (row.orgName ?? row.primaerEmail ?? row.id) : [row.vorname, row.nachname].filter(Boolean).join(" ") || row.primaerEmail || row.id, subtitle: row.primaerEmail ?? "", href: appPath({ tenantSlug: ctx.tenantSlug, path: entityType === "company" ? `contacts/companies/${row.id}` : `contacts/${row.id}`, mode: "host-aware" }), permissionSubject: entityAccess.permissionSubject, updatedAt: serializeDate(row.updatedAt), })); } async function searchLeads({ ctx, access, query, limit }: Pick[0], "ctx" | "access" | "query" | "limit">) { const entityAccess = access.lead; const rows = await dbRead.inboxLead.findMany({ where: { tenantId: ctx.tenantId, archivedAt: null, AND: [ entityAccess.all ? {} : { OR: [{ zustaendigerUserId: ctx.userId }, { createdByUserId: ctx.userId }] }, { OR: [ { vorname: { contains: query, mode: "insensitive" } }, { nachname: { contains: query, mode: "insensitive" } }, { email: { contains: query, mode: "insensitive" } }, { telefon: { contains: query, mode: "insensitive" } }, { message: { contains: query, mode: "insensitive" } }, ], }, ], }, orderBy: { updatedAt: "desc" }, take: limit, select: { id: true, vorname: true, nachname: true, email: true, message: true, updatedAt: true }, }); return rows.map((row) => ({ id: row.id, entityType: "lead" as const, title: [row.vorname, row.nachname].filter(Boolean).join(" ") || row.email || row.id, subtitle: row.email ?? row.message ?? "", href: appPath({ tenantSlug: ctx.tenantSlug, path: `leads/${row.id}`, mode: "host-aware" }), permissionSubject: entityAccess.permissionSubject, updatedAt: serializeDate(row.updatedAt), })); } async function searchProjects({ ctx, access, query, limit }: Pick[0], "ctx" | "access" | "query" | "limit">) { const entityAccess = access.project; const rows = await dbRead.projekt.findMany({ where: { tenantId: ctx.tenantId, archivedAt: null, AND: [ entityAccess.all ? {} : { OR: [{ zustaendigePersonId: ctx.userId }, { projektleiterId: ctx.userId }, { createdByUserId: ctx.userId }] }, { OR: [ { projektNummer: { contains: query, mode: "insensitive" } }, { projektname: { contains: query, mode: "insensitive" } }, { beschreibung: { contains: query, mode: "insensitive" } }, { projekttyp: { contains: query, mode: "insensitive" } }, { quelle: { contains: query, mode: "insensitive" } }, ], }, ], }, orderBy: { updatedAt: "desc" }, take: limit, select: { id: true, projektNummer: true, projektname: true, beschreibung: true, updatedAt: true }, }); return rows.map((row) => ({ id: row.id, entityType: "project" as const, title: row.projektname, subtitle: row.projektNummer || row.beschreibung || "", href: appPath({ tenantSlug: ctx.tenantSlug, path: `projects/${row.id}`, mode: "host-aware" }), permissionSubject: entityAccess.permissionSubject, updatedAt: serializeDate(row.updatedAt), })); } async function searchFiles({ ctx, access, query, limit }: Pick[0], "ctx" | "access" | "query" | "limit">) { const entityAccess = access.file; const rows = await dbRead.file.findMany({ where: { tenantId: ctx.tenantId, archivedAt: null, AND: [ entityAccess.all ? {} : { OR: [{ assignedToUserId: ctx.userId }, { createdByUserId: ctx.userId }] }, { OR: [ { displayName: { contains: query, mode: "insensitive" } }, { notes: { contains: query, mode: "insensitive" } }, { tags: { has: query } }, ], }, ], }, orderBy: { updatedAt: "desc" }, take: limit, select: { id: true, displayName: true, mimeType: true, updatedAt: true }, }); return rows.map((row) => ({ id: row.id, entityType: "file" as const, title: row.displayName, subtitle: row.mimeType, href: appPath({ tenantSlug: ctx.tenantSlug, path: `files/${row.id}`, mode: "host-aware" }), permissionSubject: entityAccess.permissionSubject, updatedAt: serializeDate(row.updatedAt), })); } async function searchTasks({ ctx, access, query, limit }: Pick[0], "ctx" | "access" | "query" | "limit">) { const entityAccess = access.task; const rows = await dbRead.task.findMany({ where: { tenantId: ctx.tenantId, archivedAt: null, AND: [ entityAccess.all ? {} : { OR: [{ assignedToUserId: ctx.userId }, { createdByUserId: ctx.userId }, { watchers: { some: { userId: ctx.userId } } }] }, { OR: [{ title: { contains: query, mode: "insensitive" } }, { description: { contains: query, mode: "insensitive" } }] }, ], }, orderBy: { updatedAt: "desc" }, take: limit, select: { id: true, title: true, status: true, updatedAt: true }, }); return rows.map((row) => ({ id: row.id, entityType: "task" as const, title: row.title, subtitle: row.status, href: appPath({ tenantSlug: ctx.tenantSlug, path: `tasks/${row.id}`, mode: "host-aware" }), permissionSubject: entityAccess.permissionSubject, updatedAt: serializeDate(row.updatedAt), })); } async function searchUsers({ ctx, access, query, limit }: Pick[0], "ctx" | "access" | "query" | "limit">) { const entityAccess = access.user; const rows = await dbRead.tenantUser.findMany({ where: { tenantId: ctx.tenantId, user: { OR: [ { firstName: { contains: query, mode: "insensitive" } }, { lastName: { contains: query, mode: "insensitive" } }, { email: { contains: query, mode: "insensitive" } }, ], }, }, orderBy: { user: { email: "asc" } }, take: limit, select: { id: true, userId: true, user: { select: { firstName: true, lastName: true, email: true, updatedAt: true } } }, }); return rows.map((row) => ({ id: row.userId, entityType: "user" as const, title: [row.user.firstName, row.user.lastName].filter(Boolean).join(" ") || row.user.email, subtitle: row.user.email, href: appPath({ tenantSlug: ctx.tenantSlug, path: "settings/members", mode: "host-aware" }), permissionSubject: entityAccess.permissionSubject, updatedAt: serializeDate(row.user.updatedAt), })); } async function searchMeili(input: { ctx: SearchContext; access: Record; query: string; limit: number; searchableTypes: GlobalSearchEntityType[]; }): Promise { if (!process.env.MEILI_URL || process.env.SEARCH_BACKEND === "sql") return []; const client = createSearchClient({ url: process.env.MEILI_URL, apiKey: process.env.MEILI_API_KEY }); const jobs: Promise[] = []; for (const entityType of ["contact", "company", "lead"] as const) { if (input.searchableTypes.includes(entityType)) { jobs.push( searchWithFallback({ client, schema: crmContactsIndex, tenantId: input.ctx.tenantId, query: input.query, options: { limit: input.limit, extraFilters: [`entityType = "${entityType}"`, ...ownerMeiliFilters(input.access[entityType], input.ctx.userId, ["ownerId"])] }, fallback: async () => ({ hits: [], estimatedTotalHits: 0, processingTimeMs: 0 }), }).then((result) => result.hits.map((hit) => meiliCrmHit(input, entityType, hit))) ); } } if (input.searchableTypes.includes("project")) { jobs.push( searchWithFallback({ client, schema: projectsIndex, tenantId: input.ctx.tenantId, query: input.query, options: { limit: input.limit, extraFilters: ownerMeiliFilters(input.access.project, input.ctx.userId, ["ownerId", "projectLeadId"]) }, fallback: async () => ({ hits: [], estimatedTotalHits: 0, processingTimeMs: 0 }), }).then((result) => result.hits.map((hit) => meiliProjectHit(input, hit))) ); } if (input.searchableTypes.includes("file")) { jobs.push( searchWithFallback({ client, schema: filesIndex, tenantId: input.ctx.tenantId, query: input.query, options: { limit: input.limit, extraFilters: ownerMeiliFilters(input.access.file, input.ctx.userId, ["createdByUserId", "assignedToUserId"]) }, fallback: async () => ({ hits: [], estimatedTotalHits: 0, processingTimeMs: 0 }), }).then((result) => result.hits.map((hit) => meiliFileHit(input, hit))) ); } if (input.searchableTypes.includes("task")) { jobs.push( searchWithFallback({ client, schema: tasksIndex, tenantId: input.ctx.tenantId, query: input.query, options: { limit: input.limit, extraFilters: ownerMeiliFilters(input.access.task, input.ctx.userId, ["createdByUserId", "assignedToUserId"]) }, fallback: async () => ({ hits: [], estimatedTotalHits: 0, processingTimeMs: 0 }), }).then((result) => result.hits.map((hit) => meiliTaskHit(input, hit))) ); } if (input.searchableTypes.includes("user")) { jobs.push( searchWithFallback({ client, schema: usersIndex, tenantId: input.ctx.tenantId, query: input.query, options: { limit: input.limit }, fallback: async () => ({ hits: [], estimatedTotalHits: 0, processingTimeMs: 0 }), }).then((result) => result.hits.map((hit) => meiliUserHit(input, hit))) ); } try { return (await Promise.all(jobs)).flat(); } catch { return []; } } function meiliCrmHit( input: Pick[0], "ctx" | "access">, entityType: "contact" | "company" | "lead", hit: SearchHit ): GlobalSearchResult { const doc = hit.document; const title = entityType === "company" ? String(doc.company ?? doc.email ?? doc.id) : [doc.firstName, doc.lastName].filter(Boolean).join(" ") || String(doc.email ?? doc.id); const path = entityType === "company" ? `contacts/companies/${doc.id}` : entityType === "lead" ? `leads/${doc.id}` : `contacts/${doc.id}`; return { id: String(doc.id), entityType, title, subtitle: String(doc.email ?? doc.status ?? ""), href: appPath({ tenantSlug: input.ctx.tenantSlug, path, mode: "host-aware" }), permissionSubject: input.access[entityType].permissionSubject, updatedAt: stringOrNull(doc.updatedAt), }; } function meiliProjectHit(input: Pick[0], "ctx" | "access">, hit: SearchHit): GlobalSearchResult { const doc = hit.document; return { id: String(doc.id), entityType: "project", title: String(doc.name ?? doc.projectNumber ?? doc.id), subtitle: String(doc.projectNumber ?? doc.type ?? ""), href: appPath({ tenantSlug: input.ctx.tenantSlug, path: `projects/${doc.id}`, mode: "host-aware" }), permissionSubject: input.access.project.permissionSubject, updatedAt: stringOrNull(doc.updatedAt), }; } function ownerMeiliFilters(access: EntityAccess, userId: string, fields: readonly string[]): string[] { if (access.all) return []; const escaped = escapeMeiliFilterValue(userId); return [`${fields.map((field) => `${field} = "${escaped}"`).join(" OR ")}`]; } function meiliFileHit(input: Pick[0], "ctx" | "access">, hit: SearchHit): GlobalSearchResult { const doc = hit.document; return { id: String(doc.id), entityType: "file", title: String(doc.displayName ?? doc.id), subtitle: String(doc.mimeType ?? ""), href: appPath({ tenantSlug: input.ctx.tenantSlug, path: `files/${doc.id}`, mode: "host-aware" }), permissionSubject: input.access.file.permissionSubject, updatedAt: stringOrNull(doc.updatedAt), }; } function meiliTaskHit(input: Pick[0], "ctx" | "access">, hit: SearchHit): GlobalSearchResult { const doc = hit.document; return { id: String(doc.id), entityType: "task", title: String(doc.title ?? doc.id), subtitle: String(doc.status ?? ""), href: appPath({ tenantSlug: input.ctx.tenantSlug, path: `tasks/${doc.id}`, mode: "host-aware" }), permissionSubject: input.access.task.permissionSubject, updatedAt: stringOrNull(doc.updatedAt), }; } function meiliUserHit(input: Pick[0], "ctx" | "access">, hit: SearchHit): GlobalSearchResult { const doc = hit.document; const title = [doc.firstName, doc.lastName].filter(Boolean).join(" ") || String(doc.email ?? doc.id); return { id: String(doc.id), entityType: "user", title, subtitle: String(doc.email ?? ""), href: appPath({ tenantSlug: input.ctx.tenantSlug, path: "settings/members", mode: "host-aware" }), permissionSubject: input.access.user.permissionSubject, updatedAt: stringOrNull(doc.lastLoginAt), }; } async function loadRecentSearches(ctx: SearchContext): Promise { const rows = await dbRead.recentSearch.findMany({ where: { tenantId: ctx.tenantId, userId: ctx.userId }, orderBy: { createdAt: "desc" }, take: RECENT_LIMIT, select: { id: true, queryText: true, resultCount: true, createdAt: true }, }); return rows.map((row) => ({ id: row.id, queryText: row.queryText, resultCount: row.resultCount, createdAt: serializeDate(row.createdAt) ?? "", })); } async function recordRecentSearch(input: { ctx: SearchContext; query: string; resultCount: number; entityTypes: readonly string[] }) { try { await db.recentSearch.create({ data: { tenantId: input.ctx.tenantId, userId: input.ctx.userId, queryText: input.query, resultCount: input.resultCount, appliedFilters: { entityTypes: input.entityTypes }, }, }); } catch { // Recent searches are helpful state, not a reason to fail the search request. } } async function mirrorSearchIndexEntries(input: { ctx: SearchContext; results: GlobalSearchResult[] }) { await Promise.all( input.results.map((result) => db.searchIndexEntry .upsert({ where: { tenantId_entityType_entityId: { tenantId: input.ctx.tenantId, entityType: result.entityType, entityId: result.id, }, }, create: { tenantId: input.ctx.tenantId, entityType: result.entityType, entityId: result.id, title: result.title, subtitle: result.subtitle, bodyText: `${result.title} ${result.subtitle}`, routePath: result.href, sourceIndex: result.entityType, permissionSubject: result.permissionSubject, }, update: { title: result.title, subtitle: result.subtitle, bodyText: `${result.title} ${result.subtitle}`, routePath: result.href, permissionSubject: result.permissionSubject, lastIndexedAt: new Date(), isArchived: false, }, }) .catch(() => undefined) ) ); } function mergeResults(results: GlobalSearchResult[]) { const seen = new Set(); return results.filter((result) => { const key = `${result.entityType}:${result.id}`; if (seen.has(key)) return false; seen.add(key); return true; }); } function compareUpdatedDesc(left: GlobalSearchResult, right: GlobalSearchResult) { return Date.parse(right.updatedAt ?? "") - Date.parse(left.updatedAt ?? ""); } function clampLimit(limit: number | undefined) { if (!limit || Number.isNaN(limit)) return DEFAULT_LIMIT; return Math.min(Math.max(Math.floor(limit), 1), 50); } function serializeDate(value: Date | string | null): string | null { if (value === null) return null; return value instanceof Date ? value.toISOString() : value; } function stringOrNull(value: unknown): string | null { return typeof value === "string" ? value : null; } function escapeMeiliFilterValue(value: string): string { return value.replace(/\\/gu, "\\\\").replace(/"/gu, '\\"'); }