Update hivegrid-cloud reference to latest main

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
davidandClaude Fable 5 committed 2026-07-08 16:15:37 +02:00
1 parent e98d38e716
commit 7b21524c86
3781 files changed
+373484 -225607

No files matched your search

@@ -0,0 +1,115 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const dbMocks = vi.hoisted(() => ({
kontaktFindMany: vi.fn(),
inboxLeadFindMany: vi.fn(),
projektFindMany: vi.fn(),
fileFindMany: vi.fn(),
taskFindMany: vi.fn(),
tenantUserFindMany: vi.fn(),
recentSearchFindMany: vi.fn(),
recentSearchCreate: vi.fn(),
searchIndexEntryUpsert: vi.fn(),
}));
const accessMocks = vi.hoisted(() => ({
permissions: ["search.use"] as string[],
hasModule: true,
}));
vi.mock("~/utils/db.server", () => ({
dbRead: {
kontakt: { findMany: dbMocks.kontaktFindMany },
inboxLead: { findMany: dbMocks.inboxLeadFindMany },
projekt: { findMany: dbMocks.projektFindMany },
file: { findMany: dbMocks.fileFindMany },
task: { findMany: dbMocks.taskFindMany },
tenantUser: { findMany: dbMocks.tenantUserFindMany },
recentSearch: { findMany: dbMocks.recentSearchFindMany },
},
db: {
recentSearch: { create: dbMocks.recentSearchCreate },
searchIndexEntry: { upsert: dbMocks.searchIndexEntryUpsert },
},
}));
vi.mock("~/utils/loaders.middleware", () => ({ requireAuth: vi.fn(async () => undefined) }));
vi.mock("~/utils/services/.server/urlService", () => ({ getTenantIdFromUrl: vi.fn(async () => "tenant_1") }));
vi.mock("~/utils/session.server", () => ({ getUserInfo: vi.fn(async () => ({ userId: "user_1", lng: "de" })) }));
vi.mock("~/utils/connector/tenantConnector.server", () => ({
tenantHasModule: vi.fn(async () => accessMocks.hasModule),
}));
vi.mock("~/utils/db/permissions/userRoles.db.server", () => ({
getPermissionsByUser: vi.fn(async () => accessMocks.permissions),
}));
import { runGlobalSearch } from "./globalSearch.server";
describe("runGlobalSearch", () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.SEARCH_BACKEND = "sql";
accessMocks.permissions = ["search.use"];
accessMocks.hasModule = true;
dbMocks.recentSearchFindMany.mockResolvedValue([]);
dbMocks.recentSearchCreate.mockResolvedValue({});
dbMocks.searchIndexEntryUpsert.mockResolvedValue({});
dbMocks.kontaktFindMany.mockResolvedValue([]);
dbMocks.inboxLeadFindMany.mockResolvedValue([]);
dbMocks.projektFindMany.mockResolvedValue([]);
dbMocks.fileFindMany.mockResolvedValue([]);
dbMocks.taskFindMany.mockResolvedValue([]);
dbMocks.tenantUserFindMany.mockResolvedValue([]);
});
it("requires entity permissions in addition to search.use", async () => {
const result = await runGlobalSearch({
request: new Request("https://app.example.test/acme/search?q=ada"),
params: { tenant: "acme" },
query: "ada",
});
expect(result.results).toEqual([]);
expect(result.hiddenEntityTypes).toContain("contact");
expect(dbMocks.kontaktFindMany).not.toHaveBeenCalled();
});
it("owner-filters CRM contacts when the caller only has crm.read_own", async () => {
accessMocks.permissions = ["search.use", "crm.read_own"];
dbMocks.kontaktFindMany.mockResolvedValueOnce([
{
id: "kontakt_1",
vorname: "Ada",
nachname: "Lovelace",
orgName: null,
primaerEmail: "ada@example.test",
updatedAt: new Date("2026-07-01T10:00:00.000Z"),
},
]);
const result = await runGlobalSearch({
request: new Request("https://app.example.test/acme/search?q=ada"),
params: { tenant: "acme" },
query: "ada",
entityTypes: ["contact"],
});
expect(result.results[0]).toMatchObject({
id: "kontakt_1",
entityType: "contact",
permissionSubject: "crm.read_own",
});
expect(dbMocks.kontaktFindMany).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({
tenantId: "tenant_1",
AND: expect.arrayContaining([
expect.objectContaining({
OR: expect.arrayContaining([expect.objectContaining({ zustaendigerUserId: "user_1" }), expect.objectContaining({ createdByUserId: "user_1" })]),
}),
]),
}),
})
);
});
});
@@ -0,0 +1,655 @@
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<string, string | undefined>;
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<string, string | undefined> }): Promise<SearchContext> {
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<GlobalSearchResponse> {
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<GlobalSearchEntityType, EntityAccess> {
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<GlobalSearchEntityType, EntityAccess>;
query: string;
limit: number;
searchableTypes: GlobalSearchEntityType[];
}): Promise<GlobalSearchResult[]> {
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<Parameters<typeof searchSqlFallback>[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<Parameters<typeof searchSqlFallback>[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<Parameters<typeof searchSqlFallback>[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<Parameters<typeof searchSqlFallback>[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<Parameters<typeof searchSqlFallback>[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<Parameters<typeof searchSqlFallback>[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<GlobalSearchEntityType, EntityAccess>;
query: string;
limit: number;
searchableTypes: GlobalSearchEntityType[];
}): Promise<GlobalSearchResult[]> {
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<GlobalSearchResult[]>[] = [];
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<Parameters<typeof searchMeili>[0], "ctx" | "access">,
entityType: "contact" | "company" | "lead",
hit: SearchHit<SearchDocument>
): 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<Parameters<typeof searchMeili>[0], "ctx" | "access">, hit: SearchHit<SearchDocument>): 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<Parameters<typeof searchMeili>[0], "ctx" | "access">, hit: SearchHit<SearchDocument>): 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<Parameters<typeof searchMeili>[0], "ctx" | "access">, hit: SearchHit<SearchDocument>): 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<Parameters<typeof searchMeili>[0], "ctx" | "access">, hit: SearchHit<SearchDocument>): 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<RecentSearchItem[]> {
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<string>();
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, '\\"');
}
@@ -0,0 +1,427 @@
import { publishOutboxEvent, type OutboxPublisherTransaction } from "@hivegrid/db/outbox";
import type { Prisma } from "@hivegrid/db/prisma-browser";
import {
crmContactsIndex,
filesIndex,
projectsIndex,
publishSearchDeleteEvent,
publishSearchUpsertEvent,
tasksIndex,
usersIndex,
type SearchIndexSchema,
} from "@hivegrid/search";
type SearchProjectionTx = Prisma.TransactionClient & {
searchIndexEntry: {
upsert(args: {
where: { tenantId_entityType_entityId: { tenantId: string; entityType: string; entityId: string } };
create: Prisma.SearchIndexEntryUncheckedCreateInput;
update: Prisma.SearchIndexEntryUncheckedUpdateInput;
}): Promise<unknown>;
updateMany(args: { where: { tenantId: string; entityType: string; entityId: string }; data: Prisma.SearchIndexEntryUncheckedUpdateInput }): Promise<unknown>;
};
};
type SearchEntityType = "contact" | "company" | "lead" | "project" | "file" | "task" | "user";
type SearchProjectionInput = {
tx: SearchProjectionTx;
schema: SearchIndexSchema;
tenantId: string;
entityType: SearchEntityType;
entityId: string;
title: string;
subtitle?: string | null;
bodyText?: string | null;
routePath: string;
permissionSubject: string;
document: Record<string, unknown> & { id: string; tenantId: string };
};
type KontaktSearchRow = {
id: string;
tenantId: string;
kontaktTyp: "privatperson" | "organisation";
kontaktStatus: string;
vorname: string | null;
nachname: string | null;
orgName: string | null;
primaerEmail: string | null;
zustaendigerUserId: string | null;
createdByUserId: string | null;
createdAt: Date | string;
updatedAt: Date | string;
};
type LeadSearchRow = {
id: string;
tenantId: string;
createdByUserId: string | null;
zustaendigerUserId: string | null;
quelle: string;
sourceOrigin: string | null;
status: string;
vorname: string | null;
nachname: string | null;
email: string | null;
telefon: string | null;
message: string | null;
createdAt: Date | string;
updatedAt: Date | string;
};
type ProjectSearchRow = {
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;
};
type FileSearchRow = {
id: string;
tenantId: string;
displayName: string;
notes: string | null;
tags: string[];
mimeType: string;
createdByUserId: string;
assignedToUserId: string | null;
archivedAt: Date | string | null;
createdAt: Date | string;
updatedAt: Date | string;
sizeBytes: number;
};
type TaskSearchRow = {
id: string;
tenantId: string;
createdByUserId: string;
title: string;
description: string | null;
status: string;
priority: string;
deadline: Date | string | null;
assignedToUserId: string | null;
assignedToGroupId: string | null;
linkedEntityType: string | null;
linkedEntityId: string | null;
archivedAt: Date | string | null;
createdAt: Date | string;
updatedAt: Date | string;
};
type TenantUserSearchRow = {
tenantId: string;
type: number;
status: number;
user: {
id: string;
createdAt: Date | string;
email: string;
firstName: string;
lastName: string;
active: boolean;
admin?: { userId: string } | null;
logs?: Array<{ createdAt: Date | string }>;
};
};
const TENANT_USER_STATUS_ACTIVE = 2;
export async function publishKontaktSearchUpsert(tx: Prisma.TransactionClient, row: KontaktSearchRow): Promise<void> {
const entityType = row.kontaktTyp === "organisation" ? "company" : "contact";
const title = entityType === "company" ? (row.orgName ?? row.primaerEmail ?? row.id) : [row.vorname, row.nachname].filter(Boolean).join(" ") || row.primaerEmail || row.id;
await publishSearchProjectionUpsert({
tx: tx as SearchProjectionTx,
schema: crmContactsIndex,
tenantId: row.tenantId,
entityType,
entityId: row.id,
title,
subtitle: row.primaerEmail,
bodyText: [title, row.primaerEmail].filter(Boolean).join(" "),
routePath: `/app/${row.tenantId}/${entityType === "company" ? "contacts/companies" : "contacts"}/${row.id}`,
permissionSubject: "crm.read_own",
document: {
id: row.id,
tenantId: row.tenantId,
entityType,
firstName: row.vorname ?? "",
lastName: row.nachname ?? "",
email: row.primaerEmail ?? "",
company: row.orgName ?? "",
jobTitle: "",
tags: [],
status: row.kontaktStatus,
ownerId: row.zustaendigerUserId ?? row.createdByUserId ?? "",
createdAt: serializeDate(row.createdAt),
updatedAt: serializeDate(row.updatedAt),
lastInteractionAt: null,
},
});
}
export async function publishLeadSearchUpsert(tx: Prisma.TransactionClient, row: LeadSearchRow): Promise<void> {
const title = [row.vorname, row.nachname].filter(Boolean).join(" ") || row.email || row.telefon || row.id;
await publishSearchProjectionUpsert({
tx: tx as SearchProjectionTx,
schema: crmContactsIndex,
tenantId: row.tenantId,
entityType: "lead",
entityId: row.id,
title,
subtitle: row.email ?? row.telefon ?? row.sourceOrigin ?? row.quelle,
bodyText: [title, row.email, row.telefon, row.message].filter(Boolean).join(" "),
routePath: `/app/${row.tenantId}/leads/${row.id}`,
permissionSubject: "leads.read_own",
document: {
id: row.id,
tenantId: row.tenantId,
entityType: "lead",
firstName: row.vorname ?? "",
lastName: row.nachname ?? "",
email: row.email ?? "",
company: row.sourceOrigin ?? "",
jobTitle: "",
tags: [],
status: row.status,
ownerId: row.zustaendigerUserId ?? row.createdByUserId ?? "",
createdAt: serializeDate(row.createdAt),
updatedAt: serializeDate(row.updatedAt),
lastInteractionAt: null,
},
});
}
export async function publishProjectSearchUpsert(tx: Prisma.TransactionClient, row: ProjectSearchRow): Promise<void> {
await publishSearchProjectionUpsert({
tx: tx as SearchProjectionTx,
schema: projectsIndex,
tenantId: row.tenantId,
entityType: "project",
entityId: row.id,
title: row.projektname || row.projektNummer || row.id,
subtitle: row.projektNummer,
bodyText: [row.projektname, row.projektNummer, row.beschreibung, row.projekttyp, row.quelle].filter(Boolean).join(" "),
routePath: `/app/${row.tenantId}/projects/${row.id}`,
permissionSubject: "projects.read_own",
document: {
id: row.id,
tenantId: row.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),
},
});
}
export async function publishFileSearchUpsert(tx: Prisma.TransactionClient, row: FileSearchRow): Promise<void> {
await publishSearchProjectionUpsert({
tx: tx as SearchProjectionTx,
schema: filesIndex,
tenantId: row.tenantId,
entityType: "file",
entityId: row.id,
title: row.displayName || row.id,
subtitle: row.mimeType,
bodyText: [row.displayName, row.notes, ...(row.tags ?? [])].filter(Boolean).join(" "),
routePath: `/app/${row.tenantId}/files/${row.id}`,
permissionSubject: "files.read_own",
document: {
id: row.id,
tenantId: row.tenantId,
displayName: row.displayName,
notes: row.notes ?? "",
tags: row.tags ?? [],
mimeType: row.mimeType,
createdByUserId: row.createdByUserId,
assignedToUserId: row.assignedToUserId ?? "",
archived: row.archivedAt !== null,
createdAt: serializeDate(row.createdAt),
updatedAt: serializeDate(row.updatedAt),
sizeBytes: row.sizeBytes,
},
});
}
export async function publishTaskSearchUpsert(tx: Prisma.TransactionClient, row: TaskSearchRow): Promise<void> {
await publishSearchProjectionUpsert({
tx: tx as SearchProjectionTx,
schema: tasksIndex,
tenantId: row.tenantId,
entityType: "task",
entityId: row.id,
title: row.title || row.id,
subtitle: row.status,
bodyText: [row.title, row.description].filter(Boolean).join(" "),
routePath: `/app/${row.tenantId}/tasks/${row.id}`,
permissionSubject: "tasks.read_own",
document: {
id: row.id,
tenantId: row.tenantId,
title: row.title,
description: row.description ?? "",
status: row.status,
priority: row.priority,
createdByUserId: row.createdByUserId,
assignedToUserId: row.assignedToUserId ?? "",
assignedToGroupId: row.assignedToGroupId ?? "",
linkedEntityType: row.linkedEntityType ?? "",
linkedEntityId: row.linkedEntityId ?? "",
archived: row.archivedAt !== null,
createdAt: serializeDate(row.createdAt),
updatedAt: serializeDate(row.updatedAt),
deadline: serializeDate(row.deadline),
},
});
}
export async function publishTenantUserSearchUpsert(tx: Prisma.TransactionClient, row: TenantUserSearchRow): Promise<void> {
const title = [row.user.firstName, row.user.lastName].filter(Boolean).join(" ") || row.user.email || row.user.id;
await publishSearchProjectionUpsert({
tx: tx as SearchProjectionTx,
schema: usersIndex,
tenantId: row.tenantId,
entityType: "user",
entityId: row.user.id,
title,
subtitle: row.user.email,
bodyText: [title, row.user.email].filter(Boolean).join(" "),
routePath: `/app/${row.tenantId}/settings/members`,
permissionSubject: "app.settings.members.view",
document: {
id: row.user.id,
tenantId: row.tenantId,
firstName: row.user.firstName,
lastName: row.user.lastName,
email: row.user.email,
type: row.type,
admin: Boolean(row.user.admin),
active: row.user.active && row.status === TENANT_USER_STATUS_ACTIVE,
createdAt: serializeDate(row.user.createdAt),
lastLoginAt: serializeDate(row.user.logs?.[0]?.createdAt ?? null),
},
});
}
export async function publishUserSearchDelete(tx: Prisma.TransactionClient, input: { tenantId: string; userId: string }): Promise<void> {
await publishSearchProjectionDelete(tx, {
schema: usersIndex,
tenantId: input.tenantId,
entityType: "user",
entityId: input.userId,
});
}
export async function publishSearchProjectionDelete(
tx: Prisma.TransactionClient,
input: { schema: SearchIndexSchema; tenantId: string; entityType: SearchEntityType; entityId: string },
): Promise<void> {
await (tx as SearchProjectionTx).searchIndexEntry.updateMany({
where: { tenantId: input.tenantId, entityType: input.entityType, entityId: input.entityId },
data: { isArchived: true, lastIndexedAt: new Date() },
});
await publishSearchDeleteEvent({
publish: (event) =>
publishOutboxEvent(tx as unknown as OutboxPublisherTransaction, {
tenantId: event.tenantId,
type: event.type,
payload: event.payload as Prisma.InputJsonValue,
}),
schema: input.schema,
tenantId: input.tenantId,
entityName: input.entityType,
documentId: input.entityId,
});
}
async function publishSearchProjectionUpsert(input: SearchProjectionInput): Promise<void> {
const now = new Date();
await input.tx.searchIndexEntry.upsert({
where: {
tenantId_entityType_entityId: {
tenantId: input.tenantId,
entityType: input.entityType,
entityId: input.entityId,
},
},
create: {
tenantId: input.tenantId,
entityType: input.entityType,
entityId: input.entityId,
title: input.title,
subtitle: input.subtitle ?? null,
bodyText: input.bodyText ?? input.title,
routePath: input.routePath,
sourceIndex: input.schema.name,
permissionSubject: input.permissionSubject,
lastIndexedAt: now,
},
update: {
title: input.title,
subtitle: input.subtitle ?? null,
bodyText: input.bodyText ?? input.title,
routePath: input.routePath,
sourceIndex: input.schema.name,
permissionSubject: input.permissionSubject,
lastIndexedAt: now,
isArchived: false,
},
});
await publishSearchUpsertEvent({
publish: (event) =>
publishOutboxEvent(input.tx as unknown as OutboxPublisherTransaction, {
tenantId: event.tenantId,
type: event.type,
payload: event.payload as Prisma.InputJsonValue,
}),
schema: input.schema,
tenantId: input.tenantId,
entityName: input.entityType,
document: input.document,
});
}
function serializeDate(value: Date | string | null | undefined): string | null {
if (value === null || value === undefined) return null;
return value instanceof Date ? value.toISOString() : value;
}
function serializeDecimal(value: unknown): number | null {
if (value === null || value === undefined) return null;
if (typeof value === "number") return Number.isFinite(value) ? value : null;
if (typeof value === "string") {
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : null;
}
if (typeof value === "object" && "toString" in value && typeof value.toString === "function") {
const parsed = Number(value.toString());
return Number.isFinite(parsed) ? parsed : null;
}
return null;
}