/** * Helpers for domain services to publish search-indexing events through * the outbox pattern. Domain code calls one of these helpers immediately * after the canonical DB write — inside the same Prisma transaction so * the event row is atomic with the source mutation. * * Event type convention: `search.index..` so the outbox * event router (apps/worker/src/outbox/event-router.ts) maps every * search event onto the `outbox.search-index` BullMQ queue. */ import type { SearchIndexSchema } from "./schemas/index.js"; import { sanitizeDocument } from "./sanitize.js"; /** * Loose alias matching the @hivegrid/db OutboxPublisherTransaction shape. * We avoid the direct import to keep packages/search free of a Prisma / * @hivegrid/db dependency — the helper here is a thin wrapper around the * caller-supplied publish function. */ export interface SearchOutboxPublisher { (event: { tenantId: string; type: string; payload: Record; dispatchKey?: string | null; }): Promise; } export type SearchOutboxVerb = "upsert" | "delete" | "bulk-reindex"; export interface PublishSearchUpsertEventInput { readonly publish: SearchOutboxPublisher; readonly schema: SearchIndexSchema; readonly tenantId: string; readonly entityName: string; readonly document: Record & { id: string | number; tenantId: string }; } export interface PublishSearchDeleteEventInput { readonly publish: SearchOutboxPublisher; readonly schema: SearchIndexSchema; readonly tenantId: string; readonly entityName: string; readonly documentId: string | number; } export interface PublishSearchBulkReindexEventInput { readonly publish: SearchOutboxPublisher; readonly schema: SearchIndexSchema; readonly tenantId: string; readonly entityName: string; } export function searchEventType(entityName: string, verb: SearchOutboxVerb): string { const normalized = entityName.toLowerCase().replace(/[^a-z0-9]/gu, ""); // The outbox event-type regex requires at least three lowercase // segments; we emit four: `search.index..`. Hyphens are // not allowed in any segment so the verb becomes `bulkreindex` rather // than `bulk-reindex`. const verbSlug = verb === "bulk-reindex" ? "bulkreindex" : verb; return `search.index.${normalized}.${verbSlug}`; } export async function publishSearchUpsertEvent(input: PublishSearchUpsertEventInput): Promise { const sanitized = sanitizeDocument(input.schema, input.document); await input.publish({ tenantId: input.tenantId, type: searchEventType(input.entityName, "upsert"), payload: { action: "upsert", indexName: input.schema.name, tenantId: input.tenantId, document: sanitized, }, }); } export async function publishSearchDeleteEvent(input: PublishSearchDeleteEventInput): Promise { await input.publish({ tenantId: input.tenantId, type: searchEventType(input.entityName, "delete"), payload: { action: "delete", indexName: input.schema.name, tenantId: input.tenantId, documentId: input.documentId, }, }); } export async function publishSearchBulkReindexEvent(input: PublishSearchBulkReindexEventInput): Promise { await input.publish({ tenantId: input.tenantId, type: searchEventType(input.entityName, "bulk-reindex"), payload: { action: "bulk-reindex", indexName: input.schema.name, tenantId: input.tenantId, }, }); }