Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
@@ -39,6 +39,7 @@ import {
|
||||
type WorkflowTriggerBootstrapResult,
|
||||
type WorkflowTriggerWorkerHandle,
|
||||
} from "./jobs/workflow-trigger-bootstrap.js";
|
||||
import type { QueueJsonValue } from "@hivegrid/queue";
|
||||
import {
|
||||
createWorkflowTriggerExecutorFromEnv,
|
||||
parseWorkflowTriggerExecutorMode,
|
||||
@@ -69,25 +70,50 @@ import { registerDsgvoGraceCron } from "./jobs/dsgvo-grace-cron.js";
|
||||
import { registerOutboxDoneRetentionCron } from "./jobs/outbox-done-retention-cron.js";
|
||||
import { registerWorkflowExecutionStalledReconcilerCron } from "./jobs/workflow-execution-stalled-reconciler-cron.js";
|
||||
import { registerTaskArchiveRetentionCron } from "./jobs/task-archive-retention-cron.js";
|
||||
import { registerLeadRetentionCron } from "./jobs/lead-retention-cron.js";
|
||||
import { registerIntegrationsHealthSweepCron, registerIntegrationsTokenRefreshCron } from "./jobs/integrations-maintenance-cron.js";
|
||||
import {
|
||||
registerCalendarInternalWriteSyncCron,
|
||||
registerCalendarTokenRefreshCron,
|
||||
registerCalendarWebhookDrainCron,
|
||||
registerCalendarWebhookRenewalCron,
|
||||
} from "./jobs/calendar-maintenance-cron.js";
|
||||
import {
|
||||
registerEmailInboxEventCleanupCron,
|
||||
registerEmailInboxInitialSyncCron,
|
||||
registerEmailInboxStalePollCron,
|
||||
registerEmailInboxTokenRefreshCron,
|
||||
registerEmailInboxWebhookDrainCron,
|
||||
registerEmailInboxWebhookRenewalCron,
|
||||
} from "./jobs/email-inbox-maintenance-cron.js";
|
||||
import { registerTaskReminderSweepCron } from "./jobs/task-reminder-cron.js";
|
||||
import { registerHrUploadReminderDispatchCron } from "./jobs/hr-upload-reminder-cron.js";
|
||||
import { registerEmailDomainProbeCron } from "./jobs/email-domain-probe-cron.js";
|
||||
import {
|
||||
PLUGIN_AUDIT_RETENTION_QUEUE,
|
||||
registerPluginAuditRetentionCron,
|
||||
resolvePluginAuditRetentionConfig,
|
||||
runPluginAuditRetention,
|
||||
} from "./jobs/plugin-audit-retention.js";
|
||||
import {
|
||||
AUDIT_RETENTION_QUEUE,
|
||||
registerAuditRetentionCron,
|
||||
resolveAuditRetentionPolicy,
|
||||
runAuditRetentionJob,
|
||||
type AuditRetentionTenant,
|
||||
} from "./jobs/audit-retention.js";
|
||||
import {
|
||||
createWorkflowExecutionStalledReconciler,
|
||||
type WorkflowExecutionStalledReconcilerPrisma,
|
||||
} from "./jobs/workflow-execution-stalled-reconciler.js";
|
||||
import type { HandlerDependencies } from "./handlers.js";
|
||||
import { Queue as BullMQQueue, Worker as BullMQWorker } from "bullmq";
|
||||
import { enqueueJob, parseRedisUrl, queueNames } from "@toolctrl/queue";
|
||||
import { createIdempotencyStore } from "@toolctrl/shared-store";
|
||||
import { enqueueJob, parseRedisUrl, queueNames } from "@hivegrid/queue";
|
||||
import { createIdempotencyStore } from "@hivegrid/shared-store";
|
||||
import type { SentryLikeForWorkerTracing } from "./runtime.js";
|
||||
import { shutdownWorkerRuntime, startWorkerRuntime, type WorkerRuntimeStatus } from "./runtime.js";
|
||||
import { createWorker } from "@toolctrl/queue/runtime";
|
||||
import type { Prisma } from "@toolctrl/db/prisma-browser";
|
||||
import { createWorker } from "@hivegrid/queue/runtime";
|
||||
import type { Prisma } from "@hivegrid/db/prisma-browser";
|
||||
import { workerLogger } from "./logger.js";
|
||||
import { getWorkerMetrics } from "./metrics.js";
|
||||
import { workerErrorReporter } from "./sentry.js";
|
||||
@@ -112,6 +138,7 @@ type ShutdownState = {
|
||||
pluginCapabilityUse?: PluginCapabilityUseBootstrapResult | undefined;
|
||||
pluginHookInvokeQueue?: BullMQQueue | undefined;
|
||||
pluginAuditRetentionWorker?: BullMQWorker | undefined;
|
||||
auditRetentionWorker?: BullMQWorker | undefined;
|
||||
};
|
||||
|
||||
function buildWorkerStatus(
|
||||
@@ -175,7 +202,7 @@ function createRedisHealthProbe(env: NodeJS.ProcessEnv): () => Promise<{ ok: tru
|
||||
let queue: import("bullmq").Queue | undefined;
|
||||
try {
|
||||
const { Queue } = await import("bullmq");
|
||||
queue = new Queue("__toolctrl_worker_health__", { connection: parseRedisUrl(redisUrl) });
|
||||
queue = new Queue("__hivegrid_worker_health__", { connection: parseRedisUrl(redisUrl) });
|
||||
await queue.waitUntilReady();
|
||||
return { ok: true };
|
||||
} catch (error) {
|
||||
@@ -189,7 +216,7 @@ function createRedisHealthProbe(env: NodeJS.ProcessEnv): () => Promise<{ ok: tru
|
||||
function createDatabaseHealthProbe(): () => Promise<{ ok: true } | { ok: false; error: string }> {
|
||||
return async () => {
|
||||
try {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const db = getDbClient();
|
||||
await db.$queryRaw`SELECT 1`;
|
||||
return { ok: true };
|
||||
@@ -364,7 +391,7 @@ try {
|
||||
const extraHandlerDependencies: Partial<HandlerDependencies> = {};
|
||||
if (groupResolution?.group === "general") {
|
||||
try {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
extraHandlerDependencies.reconcileWorkflowExecutionStalls = createWorkflowExecutionStalledReconciler({
|
||||
prisma: getDbClient() as unknown as WorkflowExecutionStalledReconcilerPrisma,
|
||||
});
|
||||
@@ -375,13 +402,13 @@ try {
|
||||
}
|
||||
|
||||
// AK-P4a §4 — file-preview generator service on the `general` group
|
||||
// (the group that owns the toolctrl.file-preview queue). Composed with a
|
||||
// Prisma client + STORAGE_* S3 seams; injected via extraHandlerDependencies
|
||||
// exactly like aiCostStripeReporter above. When STORAGE_* is unset the
|
||||
// factory returns undefined and the handler dead-letters preview jobs.
|
||||
// (the group that owns the hivegrid.file-preview queue). Composed with a
|
||||
// Prisma client + STORAGE_* S3 seams; injected via extraHandlerDependencies.
|
||||
// When STORAGE_* is unset the factory returns undefined and the handler
|
||||
// dead-letters preview jobs.
|
||||
if (groupResolution?.group === "general") {
|
||||
try {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const { createWorkerFilePreviewService } = await import("./jobs/file-preview.js");
|
||||
const previewService = createWorkerFilePreviewService(process.env as Parameters<typeof createWorkerFilePreviewService>[0], {
|
||||
prisma: getDbClient() as unknown as Parameters<typeof createWorkerFilePreviewService>[1]["prisma"],
|
||||
@@ -395,7 +422,7 @@ try {
|
||||
}
|
||||
|
||||
// AK-P4a §5 — scan-verdict write-back. The general group owns the
|
||||
// toolctrl.file-scan queue, so wire the File.scanStatus writer here with
|
||||
// hivegrid.file-scan queue, so wire the File.scanStatus writer here with
|
||||
// the same Prisma seam the preview service uses.
|
||||
const { createWorkerFileScanStatusWriter } = await import("./jobs/file-scan-status.js");
|
||||
extraHandlerDependencies.fileScanStatusWriter = createWorkerFileScanStatusWriter({
|
||||
@@ -416,7 +443,7 @@ try {
|
||||
const pluginInvokeRuntimeEnabled = groupResolution?.group === "plugin" ? isPluginInvokeRuntimeEnabled(process.env) : true;
|
||||
if (groupResolution?.group === "plugin" && pluginInvokeRuntimeEnabled) {
|
||||
try {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const { bootstrapPluginInvokeDeps } = await import("./jobs/plugin-invoke-bootstrap.js");
|
||||
const { getSharedStore } = await import("./shared-store.js");
|
||||
const prisma = getDbClient() as unknown as Parameters<typeof bootstrapPluginInvokeDeps>[0]["prisma"];
|
||||
@@ -458,6 +485,24 @@ try {
|
||||
);
|
||||
}
|
||||
|
||||
if (groupResolution?.group === "import" && importQueueRuntimeEnabled) {
|
||||
try {
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const { createWorkerPrivacyGdprExportService } = await import("./jobs/privacy-gdpr-export-bootstrap.js");
|
||||
const privacyGdprExportService = createWorkerPrivacyGdprExportService(process.env, {
|
||||
prisma: getDbClient() as unknown as Parameters<typeof createWorkerPrivacyGdprExportService>[1]["prisma"],
|
||||
});
|
||||
if (privacyGdprExportService) {
|
||||
extraHandlerDependencies.privacyGdprExportService = privacyGdprExportService;
|
||||
workerLogger.info({}, "privacy GDPR export service bootstrapped (import group).");
|
||||
} else {
|
||||
workerLogger.info({}, "privacy GDPR export service skipped: STORAGE_* not configured.");
|
||||
}
|
||||
} catch (error) {
|
||||
workerLogger.error({ error }, "privacy GDPR export bootstrap failed; GDPR export jobs will dead-letter until fixed.");
|
||||
}
|
||||
}
|
||||
|
||||
const runtime = startWorkerRuntime({
|
||||
logger: workerLogger,
|
||||
reporter: workerErrorReporter,
|
||||
@@ -565,7 +610,7 @@ try {
|
||||
const outboxRequiredForGroup = shouldRunOutbox && (isOutboxDispatcherRequired(process.env) || isWorkflowTriggerOutboxRequired(process.env));
|
||||
if (shouldRunOutbox) {
|
||||
try {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const prisma = getDbClient();
|
||||
outbox = await bootstrapOutboxDispatcher(process.env, {
|
||||
prisma,
|
||||
@@ -602,7 +647,7 @@ try {
|
||||
workerLogger.info({ reason: workflowTrigger.reason }, "workflow-trigger worker disabled by configuration.");
|
||||
}
|
||||
} else if (redisUrl) {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const prisma = getDbClient();
|
||||
const executor = createWorkflowTriggerExecutorFromEnv(process.env, {
|
||||
logger: workerLogger,
|
||||
@@ -746,8 +791,38 @@ try {
|
||||
try {
|
||||
await registerTaskArchiveRetentionCron(taskCronQueue, process.env);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "task-archive-retention scheduler registered (24h cadence).");
|
||||
await registerLeadRetentionCron(taskCronQueue, process.env);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "lead-retention-sweep scheduler registered (24h cadence).");
|
||||
await registerTaskReminderSweepCron(taskCronQueue, process.env);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "task-reminder-sweep scheduler registered (1h cadence).");
|
||||
await registerIntegrationsTokenRefreshCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "integrations-token-refresh scheduler registered (5m cadence).");
|
||||
await registerIntegrationsHealthSweepCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "integrations-health-sweep scheduler registered (15m cadence).");
|
||||
await registerCalendarTokenRefreshCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "calendar-token-refresh scheduler registered (5m cadence).");
|
||||
await registerCalendarWebhookRenewalCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "calendar-webhook-renewal scheduler registered (6h cadence).");
|
||||
await registerCalendarWebhookDrainCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "calendar-webhook-drain scheduler registered (1m cadence).");
|
||||
await registerCalendarInternalWriteSyncCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "calendar-internal-write-sync scheduler registered (5m cadence).");
|
||||
await registerEmailInboxTokenRefreshCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "email-inbox-token-refresh scheduler registered (5m cadence).");
|
||||
await registerEmailInboxWebhookRenewalCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "email-inbox-webhook-renewal scheduler registered (6h cadence).");
|
||||
await registerEmailInboxWebhookDrainCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "email-inbox-webhook-drain scheduler registered (1m cadence).");
|
||||
await registerEmailInboxInitialSyncCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "email-inbox-initial-sync scheduler registered (2m cadence).");
|
||||
await registerEmailInboxStalePollCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "email-inbox-stale-poll scheduler registered (30m cadence).");
|
||||
await registerEmailInboxEventCleanupCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "email-inbox-event-cleanup scheduler registered (24h cadence).");
|
||||
await registerHrUploadReminderDispatchCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "hr-upload-reminder-dispatch scheduler registered (24h cadence).");
|
||||
await registerEmailDomainProbeCron(taskCronQueue);
|
||||
workerLogger.info({ queue: queueNames.maintenance }, "email-domain-probe scheduler registered (5m cadence).");
|
||||
} finally {
|
||||
await taskCronQueue.close();
|
||||
}
|
||||
@@ -784,7 +859,7 @@ try {
|
||||
pluginAuditRetentionWorker = new BullMQWorker(
|
||||
PLUGIN_AUDIT_RETENTION_QUEUE,
|
||||
async () => {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const prisma = getDbClient() as unknown as {
|
||||
pluginCapabilityUse: {
|
||||
deleteMany: (args: { where: { id: { in: string[] } } }) => Promise<{ count: number }>;
|
||||
@@ -834,11 +909,110 @@ try {
|
||||
}
|
||||
}
|
||||
|
||||
// AK-P3 AP-2 — AuditEvent retention cron. Nightly at 03:30 UTC
|
||||
// (AUDIT_RETENTION_CRON overridable). Single-instance scheduler + worker on
|
||||
// the `general` group, dedicated queue (AUDIT_RETENTION_QUEUE). Mirrors the
|
||||
// plugin-audit-retention pattern. Compliance-safe: per-tenant regulatory
|
||||
// scopes are resolved so the policy never prunes below the required window.
|
||||
let auditRetentionWorker: import("bullmq").Worker | undefined;
|
||||
if (groupResolution?.group === "general") {
|
||||
try {
|
||||
const redisUrl = process.env.BULLMQ_REDIS_URL ?? process.env.REDIS_URL;
|
||||
if (redisUrl) {
|
||||
const connection = parseRedisUrl(redisUrl);
|
||||
const auditRetentionQueue = new BullMQQueue(AUDIT_RETENTION_QUEUE, { connection });
|
||||
try {
|
||||
const { pattern } = await registerAuditRetentionCron(auditRetentionQueue, process.env);
|
||||
workerLogger.info({ queue: AUDIT_RETENTION_QUEUE, pattern }, "audit-retention scheduler registered.");
|
||||
} finally {
|
||||
await auditRetentionQueue.close();
|
||||
}
|
||||
|
||||
auditRetentionWorker = new BullMQWorker(
|
||||
AUDIT_RETENTION_QUEUE,
|
||||
async () => {
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const { createAuditEvent } = await import("@hivegrid/db/audit-events.server");
|
||||
const prisma = getDbClient() as unknown as {
|
||||
auditEvent: {
|
||||
findMany: (args: Record<string, unknown>) => Promise<Array<{ id?: string; tenantId?: string | null }>>;
|
||||
deleteMany: (args: { where: { id: { in: string[] } } }) => Promise<{ count: number }>;
|
||||
count: (args: { where: Record<string, unknown> }) => Promise<number>;
|
||||
};
|
||||
};
|
||||
return runAuditRetentionJob({
|
||||
logger: workerLogger,
|
||||
// List tenants that have any audit events, with the regulatory
|
||||
// scopes actually used (cheap `has` counts → policy picks the max).
|
||||
tenantLister: async (): Promise<ReadonlyArray<AuditRetentionTenant>> => {
|
||||
const rows = await prisma.auditEvent.findMany({
|
||||
where: { tenantId: { not: null } },
|
||||
distinct: ["tenantId"],
|
||||
select: { tenantId: true },
|
||||
});
|
||||
const tenants: AuditRetentionTenant[] = [];
|
||||
for (const row of rows) {
|
||||
const tenantId = row.tenantId;
|
||||
if (!tenantId) continue;
|
||||
const scopes: string[] = [];
|
||||
for (const scope of ["soc2", "hipaa", "gdpr"]) {
|
||||
const hit = await prisma.auditEvent.count({ where: { tenantId, regulatoryScope: { has: scope } } });
|
||||
if (hit > 0) scopes.push(scope);
|
||||
}
|
||||
tenants.push({ tenantId, regulatoryScopes: scopes });
|
||||
}
|
||||
return tenants;
|
||||
},
|
||||
// Bounded batch delete (Postgres has no LIMIT on DELETE): pick a
|
||||
// capped id-set older than the threshold, then deleteMany by id.
|
||||
eventDeleter: async (tenantId: string, threshold: Date): Promise<number> => {
|
||||
let deleted = 0;
|
||||
for (let batch = 0; batch < 100; batch += 1) {
|
||||
const candidates = await prisma.auditEvent.findMany({
|
||||
where: { tenantId, createdAt: { lt: threshold } },
|
||||
select: { id: true },
|
||||
take: 1000,
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
if (candidates.length === 0) break;
|
||||
const ids = candidates.map((row) => row.id).filter((id): id is string => Boolean(id));
|
||||
const { count } = await prisma.auditEvent.deleteMany({ where: { id: { in: ids } } });
|
||||
deleted += count;
|
||||
if (candidates.length < 1000) break;
|
||||
}
|
||||
return deleted;
|
||||
},
|
||||
// The cleanup is itself audited as a system action (traceable).
|
||||
auditWriter: async ({ tenantId, deletedCount, threshold, policy }) => {
|
||||
await createAuditEvent({
|
||||
tenantId,
|
||||
actorType: "system",
|
||||
action: "audit.retention.pruned",
|
||||
targetType: "AuditEvent",
|
||||
sourceModule: "audit",
|
||||
dataSensitivity: "internal",
|
||||
metadata: { deletedCount, threshold: threshold.toISOString(), retentionDays: policy.retentionDays, policySource: policy.source },
|
||||
});
|
||||
},
|
||||
policyResolver: resolveAuditRetentionPolicy,
|
||||
});
|
||||
},
|
||||
{ connection: parseRedisUrl(redisUrl), concurrency: 1 },
|
||||
);
|
||||
workerLogger.info({ queue: AUDIT_RETENTION_QUEUE }, "audit-retention worker started.");
|
||||
} else {
|
||||
workerLogger.info({}, "audit-retention scheduler skipped: no Redis URL configured.");
|
||||
}
|
||||
} catch (error) {
|
||||
workerLogger.error({ error }, "audit-retention scheduler registration failed; continuing.");
|
||||
}
|
||||
}
|
||||
|
||||
let searchIndexer: SearchIndexerBootstrapResult | undefined;
|
||||
if (groupResolution?.group === "general") {
|
||||
try {
|
||||
const redisUrl = process.env.BULLMQ_REDIS_URL ?? process.env.REDIS_URL;
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const { createSearchIndexerBulkPagers } = await import("./jobs/search-indexer-bulk-pagers.js");
|
||||
const prisma = getDbClient();
|
||||
searchIndexer = bootstrapSearchIndexer(process.env, {
|
||||
@@ -865,28 +1039,63 @@ try {
|
||||
try {
|
||||
const redisUrl = process.env.BULLMQ_REDIS_URL ?? process.env.REDIS_URL;
|
||||
if (redisUrl) {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const prisma = getDbClient() as unknown as {
|
||||
formSubmission: {
|
||||
findUnique: (args: { where: { id: string }; select: { status: true; tenantId: true } }) => Promise<{ status: string; tenantId: string } | null>;
|
||||
updateMany: (args: {
|
||||
where: { id: string; tenantId: string };
|
||||
data: { status: string; result: Record<string, unknown> };
|
||||
data: Record<string, unknown>;
|
||||
}) => Promise<{ count: number }>;
|
||||
};
|
||||
formAction: {
|
||||
findMany: (args: Record<string, unknown>) => Promise<Array<{
|
||||
id: string;
|
||||
actionType: string;
|
||||
name: string;
|
||||
fieldMapping: unknown;
|
||||
actionConfig: unknown;
|
||||
}>>;
|
||||
};
|
||||
formActionLog: {
|
||||
create: (args: Record<string, unknown>) => Promise<{ id: string; attempt: number }>;
|
||||
update: (args: Record<string, unknown>) => Promise<unknown>;
|
||||
};
|
||||
inboxLead: {
|
||||
findFirst: (args: Record<string, unknown>) => Promise<{ id: string } | null>;
|
||||
create: (args: Record<string, unknown>) => Promise<{ id: string }>;
|
||||
};
|
||||
kontakt: {
|
||||
findFirst: (args: Record<string, unknown>) => Promise<{ id: string } | null>;
|
||||
create: (args: Record<string, unknown>) => Promise<{ id: string }>;
|
||||
};
|
||||
pipeline: { findFirst: (args: Record<string, unknown>) => Promise<{ id: string } | null> };
|
||||
pipelineStage: { findFirst: (args: Record<string, unknown>) => Promise<{ id: string } | null> };
|
||||
projekt: { create: (args: Record<string, unknown>) => Promise<{ id: string }> };
|
||||
task: { create: (args: Record<string, unknown>) => Promise<{ id: string }> };
|
||||
};
|
||||
const [
|
||||
{ findLegacyEntityByIdNameOrSlug, findLegacyEntityByName },
|
||||
{ createLegacyEntityRow },
|
||||
{ publishOutboxEvent },
|
||||
{ planFormSubmissionFollowUps },
|
||||
{ buildWorkflowTriggerOutboxEvent },
|
||||
{ applyFieldMapping },
|
||||
] = await Promise.all([
|
||||
import("@toolctrl/db/entities.server"),
|
||||
import("@toolctrl/db/rows.server"),
|
||||
import("@toolctrl/db/outbox"),
|
||||
import("@hivegrid/db/outbox"),
|
||||
import("./jobs/form-submission-followups.js"),
|
||||
import("@hivegrid/queue"),
|
||||
import("@hivegrid/form-widget"),
|
||||
]);
|
||||
const connection = parseRedisUrl(redisUrl);
|
||||
function stringValue(values: Record<string, unknown>, keys: readonly string[]): string | null {
|
||||
for (const key of keys) {
|
||||
const value = values[key];
|
||||
if (typeof value === "string" && value.trim()) return value.trim();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function objectValue(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
|
||||
}
|
||||
formSubmissionRoute = bootstrapFormSubmissionRoute(process.env, {
|
||||
logger: workerLogger,
|
||||
handlerDeps: {
|
||||
@@ -906,28 +1115,6 @@ try {
|
||||
}
|
||||
},
|
||||
},
|
||||
crm: {
|
||||
createContact: async (input) => {
|
||||
const entity = await findLegacyEntityByName({ tenantId: input.tenantId, name: "contact", active: true });
|
||||
const row = await createLegacyEntityRow({
|
||||
entity: entity as Parameters<typeof createLegacyEntityRow>[0]["entity"],
|
||||
tenantId: input.tenantId,
|
||||
data: input.values,
|
||||
});
|
||||
return { id: row.id };
|
||||
},
|
||||
},
|
||||
entity: {
|
||||
createRow: async (input) => {
|
||||
const entity = await findLegacyEntityByIdNameOrSlug({ tenantId: input.tenantId, idNameOrSlug: input.entityId });
|
||||
const row = await createLegacyEntityRow({
|
||||
entity: entity as Parameters<typeof createLegacyEntityRow>[0]["entity"],
|
||||
tenantId: input.tenantId,
|
||||
data: input.values,
|
||||
});
|
||||
return { id: row.id };
|
||||
},
|
||||
},
|
||||
emitFollowUps: async (followUp) => {
|
||||
const events = planFormSubmissionFollowUps(followUp);
|
||||
for (const event of events) {
|
||||
@@ -939,6 +1126,191 @@ try {
|
||||
});
|
||||
}
|
||||
},
|
||||
actionRunner: async ({ submissionId, formId, tenantId, values }) => {
|
||||
const actions = await prisma.formAction.findMany({
|
||||
where: { formId, tenantId, enabled: true },
|
||||
orderBy: { position: "asc" },
|
||||
select: { id: true, actionType: true, name: true, fieldMapping: true, actionConfig: true },
|
||||
});
|
||||
for (const action of actions) {
|
||||
const mapped = applyFieldMapping({
|
||||
payload: values,
|
||||
fieldMapping: objectValue(action.fieldMapping) as Record<string, string>,
|
||||
});
|
||||
const idempotencyKey = `form-action:${submissionId}:${action.id}`;
|
||||
const log = await prisma.formActionLog.create({
|
||||
data: {
|
||||
formActionId: action.id,
|
||||
submissionId,
|
||||
formId,
|
||||
tenantId,
|
||||
actionType: action.actionType,
|
||||
status: "pending",
|
||||
inputSnapshot: { values: mapped },
|
||||
idempotencyKey,
|
||||
},
|
||||
select: { id: true, attempt: true },
|
||||
});
|
||||
try {
|
||||
const config = objectValue(action.actionConfig);
|
||||
let target: { type: string; id: string | null } = { type: action.actionType, id: null };
|
||||
if (action.actionType === "auto_match") {
|
||||
const email = stringValue(mapped, ["email", "primaerEmail", "email_address"]);
|
||||
const phone = stringValue(mapped, ["phone", "telefon", "primaerTelefon"]);
|
||||
const contact = email || phone
|
||||
? await prisma.kontakt.findFirst({
|
||||
where: {
|
||||
tenantId,
|
||||
archivedAt: null,
|
||||
OR: [
|
||||
...(email ? [{ primaerEmail: email.toLowerCase() }] : []),
|
||||
...(phone ? [{ primaerTelefon: phone }] : []),
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
: null;
|
||||
const lead = !contact && (email || phone)
|
||||
? await prisma.inboxLead.findFirst({
|
||||
where: { tenantId, archivedAt: null, OR: [...(email ? [{ email: email.toLowerCase() }] : []), ...(phone ? [{ telefon: phone }] : [])] },
|
||||
select: { id: true },
|
||||
})
|
||||
: null;
|
||||
target = contact ? { type: "contact", id: contact.id } : lead ? { type: "lead", id: lead.id } : { type: "none", id: null };
|
||||
if (target.id) {
|
||||
await prisma.formSubmission.updateMany({
|
||||
where: { id: submissionId, tenantId },
|
||||
data: { linkedEntityType: target.type, linkedEntityId: target.id },
|
||||
});
|
||||
}
|
||||
} else if (action.actionType === "create_lead") {
|
||||
const lead = await prisma.inboxLead.create({
|
||||
data: {
|
||||
tenantId,
|
||||
createdByUserId: typeof config.userId === "string" ? config.userId : null,
|
||||
quelle: "form",
|
||||
quelleDetail: { formId, submissionId },
|
||||
sourceOrigin: `/forms/${formId}`,
|
||||
vorname: stringValue(mapped, ["vorname", "firstName", "first_name"]),
|
||||
nachname: stringValue(mapped, ["nachname", "lastName", "last_name"]),
|
||||
email: stringValue(mapped, ["email", "email_address"])?.toLowerCase() ?? null,
|
||||
telefon: stringValue(mapped, ["telefon", "phone"]),
|
||||
message: stringValue(mapped, ["message", "nachricht", "details"]),
|
||||
rohdaten: { formId, submissionId, values: mapped },
|
||||
weiterverarbeitungFreigabe: "ja",
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
target = { type: "lead", id: lead.id };
|
||||
} else if (action.actionType === "create_contact") {
|
||||
const contact = await prisma.kontakt.create({
|
||||
data: {
|
||||
tenantId,
|
||||
createdByUserId: typeof config.userId === "string" ? config.userId : "system",
|
||||
kontaktTyp: "privatperson",
|
||||
vorname: stringValue(mapped, ["vorname", "firstName", "first_name"]),
|
||||
nachname: stringValue(mapped, ["nachname", "lastName", "last_name"]) ?? stringValue(mapped, ["name"]) ?? "Formular-Kontakt",
|
||||
primaerEmail: stringValue(mapped, ["email", "email_address"])?.toLowerCase() ?? null,
|
||||
primaerTelefon: stringValue(mapped, ["telefon", "phone"]),
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
target = { type: "contact", id: contact.id };
|
||||
} else if (action.actionType === "create_project") {
|
||||
const pipeline = await prisma.pipeline.findFirst({ where: { tenantId, archivedAt: null }, orderBy: { position: "asc" }, select: { id: true } });
|
||||
const stage = pipeline ? await prisma.pipelineStage.findFirst({ where: { tenantId, pipelineId: pipeline.id, archivedAt: null }, orderBy: { position: "asc" }, select: { id: true } }) : null;
|
||||
if (!pipeline || !stage) throw new Error("No project pipeline/stage available for form action");
|
||||
const project = await prisma.projekt.create({
|
||||
data: {
|
||||
tenantId,
|
||||
createdByUserId: typeof config.userId === "string" ? config.userId : null,
|
||||
projektNummer: `FORM-${Date.now()}`,
|
||||
projektname: stringValue(mapped, ["projektname", "projectName", "name"]) ?? "Projekt aus Formular",
|
||||
beschreibung: stringValue(mapped, ["message", "description", "details"]),
|
||||
quelle: "form",
|
||||
pipelineId: pipeline.id,
|
||||
stageId: stage.id,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
target = { type: "project", id: project.id };
|
||||
} else if (action.actionType === "create_task") {
|
||||
const task = await prisma.task.create({
|
||||
data: {
|
||||
tenantId,
|
||||
createdByUserId: typeof config.userId === "string" ? config.userId : "system",
|
||||
title: stringValue(mapped, ["title", "taskTitle", "subject"]) ?? "Aufgabe aus Formular",
|
||||
description: stringValue(mapped, ["message", "description", "details"]),
|
||||
assignedToUserId: typeof config.assignedToUserId === "string" ? config.assignedToUserId : typeof config.userId === "string" ? config.userId : "system",
|
||||
source: "form_submission",
|
||||
sourceRef: submissionId,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
target = { type: "task", id: task.id };
|
||||
} else if (action.actionType === "webhook") {
|
||||
const webhookUrl = typeof config.webhookUrl === "string" ? config.webhookUrl : null;
|
||||
if (!webhookUrl) throw new Error("Webhook action missing webhookUrl");
|
||||
const webhookPayload = { submissionId, formId, tenantId, webhookUrl, values: mapped } as unknown as Prisma.InputJsonValue;
|
||||
await publishOutboxEvent(prisma as never, {
|
||||
tenantId,
|
||||
type: "webhook.form-submission.deliver",
|
||||
payload: webhookPayload,
|
||||
dispatchKey: `form-action-webhook:${submissionId}:${action.id}`,
|
||||
});
|
||||
} else if (action.actionType === "send_email_to_team" || action.actionType === "send_email_to_submitter") {
|
||||
const recipients = action.actionType === "send_email_to_submitter"
|
||||
? [stringValue(mapped, ["email", "email_address"])].filter((email): email is string => Boolean(email))
|
||||
: Array.isArray(config.recipients) ? config.recipients.filter((email): email is string => typeof email === "string" && email.includes("@")) : [];
|
||||
if (recipients.length === 0) {
|
||||
await prisma.formActionLog.update({ where: { id: log.id }, data: { status: "skipped", outputSnapshot: { reason: "no_recipients" }, finishedAt: new Date() } });
|
||||
continue;
|
||||
}
|
||||
const mailPayload = { submissionId, formId, tenantId, recipients, values: mapped } as unknown as Prisma.InputJsonValue;
|
||||
await publishOutboxEvent(prisma as never, {
|
||||
tenantId,
|
||||
type: "mail.transactional.form-submission",
|
||||
payload: mailPayload,
|
||||
dispatchKey: `form-action-mail:${submissionId}:${action.id}`,
|
||||
});
|
||||
} else if (action.actionType === "trigger_automation") {
|
||||
const automationData = { formId, submissionId, actionId: action.id, values: mapped } as unknown as QueueJsonValue;
|
||||
await publishOutboxEvent(
|
||||
prisma as never,
|
||||
buildWorkflowTriggerOutboxEvent({
|
||||
tenantId,
|
||||
event: "form.submitted",
|
||||
data: automationData,
|
||||
userId: typeof config.userId === "string" ? config.userId : null,
|
||||
sourceId: `form:${formId}:submission:${submissionId}:action:${action.id}`,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
await prisma.formActionLog.update({ where: { id: log.id }, data: { status: "skipped", outputSnapshot: { reason: "unsupported_action" }, finishedAt: new Date() } });
|
||||
continue;
|
||||
}
|
||||
await prisma.formActionLog.update({
|
||||
where: { id: log.id },
|
||||
data: {
|
||||
status: "success",
|
||||
targetType: target.type,
|
||||
targetId: target.id,
|
||||
outputSnapshot: { target },
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
await prisma.formActionLog.update({
|
||||
where: { id: log.id },
|
||||
data: {
|
||||
status: "failed",
|
||||
errorMessage: error instanceof Error ? error.message.slice(0, 500) : String(error).slice(0, 500),
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
logger: workerLogger,
|
||||
},
|
||||
createWorkerImpl: (queueName, processor, options) => {
|
||||
@@ -1037,7 +1409,7 @@ try {
|
||||
import("./jobs/email-sequence-scheduler.js"),
|
||||
]);
|
||||
// Production deps (Prisma store + BullMQ scheduleStep producer +
|
||||
// template renderer + @toolctrl/mail Mailjet adapter +
|
||||
// template renderer + @hivegrid/mail Mailjet adapter +
|
||||
// unsubscribe-url builder) are loaded lazily inside the
|
||||
// processors so startup-time optional config issues do not
|
||||
// crash the mail worker. Missing Redis/Mailjet config still
|
||||
@@ -1080,7 +1452,7 @@ try {
|
||||
if (groupResolution?.group === "general") {
|
||||
try {
|
||||
const redisUrl = process.env.BULLMQ_REDIS_URL ?? process.env.REDIS_URL;
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const prisma = getDbClient();
|
||||
auditEventPersist = bootstrapAuditEventPersist(process.env, {
|
||||
logger: workerLogger,
|
||||
@@ -1115,7 +1487,7 @@ try {
|
||||
try {
|
||||
const redisUrl = process.env.BULLMQ_REDIS_URL ?? process.env.REDIS_URL;
|
||||
if (redisUrl) {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const prisma = getDbClient();
|
||||
const connection = parseRedisUrl(redisUrl);
|
||||
pluginHookInvokeQueue = new BullMQQueue(queueNames.pluginInvoke, { connection });
|
||||
@@ -1161,7 +1533,7 @@ try {
|
||||
try {
|
||||
const redisUrl = process.env.BULLMQ_REDIS_URL ?? process.env.REDIS_URL;
|
||||
if (redisUrl) {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
const prisma = getDbClient();
|
||||
const connection = parseRedisUrl(redisUrl);
|
||||
pluginCapabilityUse = bootstrapPluginCapabilityUse(process.env, {
|
||||
@@ -1186,15 +1558,15 @@ try {
|
||||
// Phase 03 #42 — Legacy-event outbox receiver. Subscribes the
|
||||
// `outbox.notification` queue on the `general` group and writes
|
||||
// billing.subscription.* events into the legacy Event table on
|
||||
// behalf of the migrated Stripe webhook (apps/api/src/billing/
|
||||
// stripe-webhook-route.ts). Disabled when the group is non-general
|
||||
// behalf of the migrated Mollie webhook (apps/api/src/billing/
|
||||
// mollie-webhook-route.ts). Disabled when the group is non-general
|
||||
// or no Redis URL is configured.
|
||||
let legacyEventReceiver: LegacyEventReceiverBootstrapResult | undefined;
|
||||
if (groupResolution?.group === "general") {
|
||||
try {
|
||||
const redisUrl = process.env.BULLMQ_REDIS_URL ?? process.env.REDIS_URL;
|
||||
if (redisUrl) {
|
||||
const { createLegacyEvent } = await import("@toolctrl/db/events.server");
|
||||
const { createLegacyEvent } = await import("@hivegrid/db/events.server");
|
||||
legacyEventReceiver = bootstrapLegacyEventReceiver(process.env, {
|
||||
logger: workerLogger,
|
||||
store: {
|
||||
@@ -1237,6 +1609,7 @@ try {
|
||||
pluginCapabilityUse,
|
||||
pluginHookInvokeQueue,
|
||||
pluginAuditRetentionWorker,
|
||||
auditRetentionWorker,
|
||||
};
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
@@ -1272,7 +1645,8 @@ function shutdown(runtimeToClose: WorkerRuntimeStatus, state: ShutdownState, sig
|
||||
state.pluginHookFanout,
|
||||
state.pluginCapabilityUse,
|
||||
state.pluginHookInvokeQueue,
|
||||
state.pluginAuditRetentionWorker
|
||||
state.pluginAuditRetentionWorker,
|
||||
state.auditRetentionWorker
|
||||
);
|
||||
return state.promise;
|
||||
}
|
||||
@@ -1295,7 +1669,8 @@ async function closeRuntime(
|
||||
pluginHookFanout?: PluginHookFanoutBootstrapResult,
|
||||
pluginCapabilityUse?: PluginCapabilityUseBootstrapResult,
|
||||
pluginHookInvokeQueue?: BullMQQueue,
|
||||
pluginAuditRetentionWorker?: BullMQWorker
|
||||
pluginAuditRetentionWorker?: BullMQWorker,
|
||||
auditRetentionWorker?: BullMQWorker
|
||||
): Promise<void> {
|
||||
try {
|
||||
if (outbox?.handle) {
|
||||
@@ -1397,6 +1772,13 @@ async function closeRuntime(
|
||||
workerLogger.error({ error: retentionError, signal }, "plugin-audit-retention worker shutdown failed.");
|
||||
}
|
||||
}
|
||||
if (auditRetentionWorker) {
|
||||
try {
|
||||
await auditRetentionWorker.close();
|
||||
} catch (retentionError) {
|
||||
workerLogger.error({ error: retentionError, signal }, "audit-retention worker shutdown failed.");
|
||||
}
|
||||
}
|
||||
await shutdownWorkerRuntime(runtimeToClose, { logger: workerLogger, reason: signal, reporter: workerErrorReporter });
|
||||
if (adminServer) {
|
||||
await adminServer.close();
|
||||
|
||||
Reference in new issue
Block a user