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
@@ -1,7 +1,7 @@
|
||||
import {
|
||||
updateLegacyEventWebhookAttempt as updateLegacyEventWebhookAttemptInDb,
|
||||
type LegacyEventWebhookAttemptUpdate,
|
||||
} from "@toolctrl/db/event-webhook-attempts.server";
|
||||
} from "@hivegrid/db/event-webhook-attempts.server";
|
||||
import {
|
||||
listExpiredCompletedExportJobs as listExpiredCompletedExportJobsInDb,
|
||||
listSoftDeletedExportJobsForHardDelete as listSoftDeletedExportJobsForHardDeleteInDb,
|
||||
@@ -23,7 +23,15 @@ import {
|
||||
type ExportJobRetentionRecord,
|
||||
type ExportJobRunningInput,
|
||||
type ExportJobSucceededInput,
|
||||
} from "@toolctrl/db/export-jobs.server";
|
||||
} from "@hivegrid/db/export-jobs.server";
|
||||
import {
|
||||
markGdprExportFailed as markGdprExportFailedInDb,
|
||||
markGdprExportProcessing as markGdprExportProcessingInDb,
|
||||
markGdprExportReady as markGdprExportReadyInDb,
|
||||
type MarkGdprExportFailedInput,
|
||||
type MarkGdprExportProcessingInput,
|
||||
type MarkGdprExportReadyInput,
|
||||
} from "@hivegrid/db/privacy-gdpr.server";
|
||||
import {
|
||||
markImportJobFailed as markImportJobFailedInDb,
|
||||
markImportJobRunning as markImportJobRunningInDb,
|
||||
@@ -31,7 +39,7 @@ import {
|
||||
type ImportJobFailedInput,
|
||||
type ImportJobRunningInput,
|
||||
type ImportJobSucceededInput,
|
||||
} from "@toolctrl/db/import-jobs.server";
|
||||
} from "@hivegrid/db/import-jobs.server";
|
||||
import {
|
||||
createNotificationDelivery as createNotificationDeliveryInDb,
|
||||
getUserNotificationPreferences as getUserNotificationPreferencesInDb,
|
||||
@@ -39,15 +47,15 @@ import {
|
||||
redactNotificationDiagnosticText,
|
||||
type CreateNotificationDeliveryInput,
|
||||
type MarkNotificationDeliveryStatusInput,
|
||||
} from "@toolctrl/db/notifications.server";
|
||||
} from "@hivegrid/db/notifications.server";
|
||||
import {
|
||||
deleteDoneOutboxEvents as deleteDoneOutboxEventsInDb,
|
||||
type DeleteDoneOutboxEventsOptions,
|
||||
type DeleteDoneOutboxEventsResult,
|
||||
} from "@toolctrl/db/outbox-retention";
|
||||
import { type EnvRecord } from "@toolctrl/config";
|
||||
import type { ValidationReport } from "@toolctrl/imports";
|
||||
import { createMailjetProviderFromEnv, type MailMetricsHook, type MailProvider } from "@toolctrl/mail";
|
||||
} from "@hivegrid/db/outbox-retention";
|
||||
import { type EnvRecord } from "@hivegrid/config";
|
||||
import type { ValidationReport } from "@hivegrid/imports";
|
||||
import { createMailjetProviderFromEnv, type MailMetricsHook, type MailProvider } from "@hivegrid/mail";
|
||||
import {
|
||||
queueNames,
|
||||
type ExportJob,
|
||||
@@ -57,20 +65,38 @@ import {
|
||||
type QueueName,
|
||||
type QueuePayload,
|
||||
type TypedProcessor,
|
||||
} from "@toolctrl/queue";
|
||||
} from "@hivegrid/queue";
|
||||
import {
|
||||
authorizeStorageObjectAccess,
|
||||
type DeleteObjectInput,
|
||||
type DeleteObjectResult,
|
||||
type StorageScanDiagnostic,
|
||||
type StorageScanResult,
|
||||
} from "@toolctrl/storage";
|
||||
import { mapWithConcurrency, notify, type NotificationClient } from "@toolctrl/foundation";
|
||||
import { OutboundHttpUrlValidationError, fetchOutboundHttpWithRedirects, type OutboundHttpAddressResolver } from "@toolctrl/foundation/outbound-http";
|
||||
} from "@hivegrid/storage";
|
||||
import { mapWithConcurrency, notify, type NotificationClient } from "@hivegrid/foundation";
|
||||
import { OutboundHttpUrlValidationError, fetchOutboundHttpWithRedirects, type OutboundHttpAddressResolver } from "@hivegrid/foundation/outbound-http";
|
||||
|
||||
import {
|
||||
drainCalendarWebhookEventsForTenant,
|
||||
refreshCalendarTokensForTenant,
|
||||
renewCalendarWebhooksForTenant,
|
||||
syncInternalCalendarEventsForTenant,
|
||||
} from "@hivegrid/integrations/calendar-worker-tasks.server";
|
||||
import {
|
||||
cleanupEmailWebhookEventsForTenant,
|
||||
drainEmailWebhookEventsForTenant,
|
||||
pollStaleEmailMailboxesForTenant,
|
||||
refreshEmailTokensForTenant,
|
||||
renewEmailWebhooksForTenant,
|
||||
runEmailInitialSyncForTenant,
|
||||
} from "@hivegrid/integrations/email-worker-tasks.server";
|
||||
import { createEmailAutoMatchHook } from "@hivegrid/integrations/email-match-hook.server";
|
||||
import { refreshExpiringTokensForTenant, sweepConnectionHealthForTenant } from "@hivegrid/integrations/worker-tasks.server";
|
||||
import { withTenantRLS } from "./db/tenantRls.js";
|
||||
import { runDsgvoGraceCron, type DsgvoGraceCandidate } from "./jobs/dsgvo-grace-cron.js";
|
||||
import { reminderChannel, reminderMessageKey, reminderScanBounds, reminderSourceKey, selectReminderWindows } from "./jobs/task-reminder-cron.js";
|
||||
import { audienceMatches, nextReminderDue, HR_UPLOAD_REMINDER_ESCALATION_GRACE_MS } from "./jobs/hr-upload-reminder-cron.js";
|
||||
import { createDnsDomainResolver, decideDomainProbeStatus, type DomainDnsResolver } from "./jobs/email-domain-probe-cron.js";
|
||||
import { handlePluginInvokeJob, type PluginInvokeDeps } from "./jobs/plugin-invoke.js";
|
||||
import {
|
||||
reconcileWorkflowExecutionStalls,
|
||||
@@ -78,6 +104,10 @@ import {
|
||||
type WorkflowExecutionStalledReconcileResult,
|
||||
type WorkflowExecutionStalledReconcilerPrisma,
|
||||
} from "./jobs/workflow-execution-stalled-reconciler.js";
|
||||
import {
|
||||
isPrivacyGdprExportType,
|
||||
type PrivacyGdprExportRunResult,
|
||||
} from "./jobs/privacy-gdpr-export.js";
|
||||
|
||||
const DEFAULT_WORKER_FETCH_TIMEOUT_MS = 10_000;
|
||||
const WEBHOOK_RESPONSE_BODY_LIMIT_BYTES = 4_096;
|
||||
@@ -141,6 +171,9 @@ export type HandlerDependencies = {
|
||||
exportService?: {
|
||||
runExport(payload: ExportJob): Promise<WorkerExportRunResult>;
|
||||
};
|
||||
privacyGdprExportService?: {
|
||||
runExport(payload: ExportJob): Promise<PrivacyGdprExportRunResult>;
|
||||
};
|
||||
importService?: {
|
||||
runImport(payload: ImportJob): Promise<WorkerImportRunResult>;
|
||||
};
|
||||
@@ -157,6 +190,9 @@ export type HandlerDependencies = {
|
||||
markExportJobRunning?: (data: ExportJobRunningInput) => Promise<unknown>;
|
||||
markExportJobSucceeded?: (data: ExportJobSucceededInput) => Promise<unknown>;
|
||||
markExportJobFailed?: (data: ExportJobFailedInput) => Promise<unknown>;
|
||||
markGdprExportProcessing?: (data: MarkGdprExportProcessingInput) => Promise<unknown>;
|
||||
markGdprExportReady?: (data: MarkGdprExportReadyInput) => Promise<unknown>;
|
||||
markGdprExportFailed?: (data: MarkGdprExportFailedInput) => Promise<unknown>;
|
||||
markImportJobRunning?: (data: ImportJobRunningInput) => Promise<unknown>;
|
||||
markImportJobSucceeded?: (data: ImportJobSucceededInput) => Promise<unknown>;
|
||||
markImportJobFailed?: (data: ImportJobFailedInput) => Promise<unknown>;
|
||||
@@ -490,7 +526,10 @@ export function buildHandlerRegistry(dependencies: HandlerDependencies = {}): Ha
|
||||
};
|
||||
}
|
||||
|
||||
const fetchImpl = deps.fetch ?? fetch;
|
||||
// Only an injected (test) fetch is passed through; in production we leave
|
||||
// it undefined so the outbound guard uses its own undici fetch with the
|
||||
// DNS-rebinding IP pin engaged (see fetchWebhookWithRedirects).
|
||||
const fetchImpl = deps.fetch;
|
||||
const updateLegacyEventWebhookAttempt = deps.updateLegacyEventWebhookAttempt ?? updateLegacyEventWebhookAttemptInDb;
|
||||
const legacyAttemptId = typeof payload.metadata?.legacyAttemptId === "string" ? payload.metadata.legacyAttemptId : undefined;
|
||||
const legacyAttemptTenantId = typeof payload.metadata?.legacyAttemptTenantId === "string" ? payload.metadata.legacyAttemptTenantId : null;
|
||||
@@ -787,10 +826,70 @@ export function buildHandlerRegistry(dependencies: HandlerDependencies = {}): Ha
|
||||
return runTaskArchiveRetentionHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "lead-retention-sweep") {
|
||||
return runLeadRetentionHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "task-reminder-sweep") {
|
||||
return runTaskReminderSweepHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "integrations-token-refresh") {
|
||||
return runIntegrationsTokenRefreshHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "integrations-health-sweep") {
|
||||
return runIntegrationsHealthSweepHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "calendar-token-refresh") {
|
||||
return runCalendarTokenRefreshHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "calendar-webhook-renewal") {
|
||||
return runCalendarWebhookRenewalHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "calendar-webhook-drain") {
|
||||
return runCalendarWebhookDrainHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "calendar-internal-write-sync") {
|
||||
return runCalendarInternalWriteSyncHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "email-inbox-token-refresh") {
|
||||
return runEmailInboxTokenRefreshHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "email-inbox-webhook-renewal") {
|
||||
return runEmailInboxWebhookRenewalHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "email-inbox-webhook-drain") {
|
||||
return runEmailInboxWebhookDrainHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "email-inbox-initial-sync") {
|
||||
return runEmailInboxInitialSyncHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "email-inbox-stale-poll") {
|
||||
return runEmailInboxStalePollHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "email-inbox-event-cleanup") {
|
||||
return runEmailInboxEventCleanupHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "hr-upload-reminder-dispatch") {
|
||||
return runHrUploadReminderDispatchHandler(payload, deps);
|
||||
}
|
||||
|
||||
if (payload.task === "email-domain-probe") {
|
||||
return runEmailDomainProbeHandler(payload, deps);
|
||||
}
|
||||
|
||||
throw new UnsupportedWorkerJobError("Unsupported maintenance task.", {
|
||||
queueName: queueNames.maintenance,
|
||||
task: payload.task,
|
||||
@@ -798,14 +897,6 @@ export function buildHandlerRegistry(dependencies: HandlerDependencies = {}): Ha
|
||||
olderThanMinutes: payload.olderThanMinutes,
|
||||
});
|
||||
},
|
||||
[queueNames.aiChatbot]: async (payload) => {
|
||||
throw new UnsupportedWorkerJobError("AI worker job is not configured.", {
|
||||
queueName: queueNames.aiChatbot,
|
||||
kind: payload.kind,
|
||||
tenantId: payload.tenantId,
|
||||
userId: payload.userId,
|
||||
});
|
||||
},
|
||||
[queueNames.browserAutomation]: async (payload) => {
|
||||
throw new UnsupportedWorkerJobError("Browser automation worker job is not configured.", {
|
||||
queueName: queueNames.browserAutomation,
|
||||
@@ -1147,7 +1238,7 @@ async function runWorkflowExecutionStalledReconcileHandler(
|
||||
}
|
||||
|
||||
async function getDefaultDbClient() {
|
||||
const { getDbClient } = await import("@toolctrl/db/client.server");
|
||||
const { getDbClient } = await import("@hivegrid/db/client.server");
|
||||
return getDbClient();
|
||||
}
|
||||
|
||||
@@ -1190,6 +1281,284 @@ async function runTaskArchiveRetentionHandler(payload: QueuePayload<typeof queue
|
||||
};
|
||||
}
|
||||
|
||||
async function runLeadRetentionHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const take = typeof payload.take === "number" && payload.take > 0 ? Math.floor(payload.take) : 1000;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let archived = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const dueIds = await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.inboxLead.findMany({
|
||||
where: { tenantId, archivedAt: null, autoDeleteAt: { lte: now } },
|
||||
select: { id: true },
|
||||
orderBy: { autoDeleteAt: "asc" },
|
||||
take,
|
||||
}),
|
||||
);
|
||||
if (dueIds.length === 0) continue;
|
||||
const result = await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.inboxLead.updateMany({
|
||||
where: { tenantId, id: { in: dueIds.map((row) => row.id) }, archivedAt: null },
|
||||
data: { archivedAt: now },
|
||||
}),
|
||||
);
|
||||
archived += result.count;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, archived }, "leads.retention.completed");
|
||||
return {
|
||||
handled: true,
|
||||
action: "leads.retention.completed",
|
||||
details: { task: payload.task, tenants: tenants.length, archived, take },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* R-P5 §P2d — refresh OAuth tokens nearing expiry across all tenants. Enumerates
|
||||
* active tenants from the global registry (not RLS-scoped) and runs the scoped
|
||||
* refresh per tenant inside withTenantRLS() (so the policy binds at cutover). The
|
||||
* per-connection logic (decrypt → adapter.refreshToken → re-encrypt) lives in
|
||||
* @hivegrid/integrations/worker-tasks.server, shared with the web side.
|
||||
*/
|
||||
async function runIntegrationsTokenRefreshHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const windowMs = (typeof payload.olderThanMinutes === "number" && payload.olderThanMinutes > 0 ? Math.floor(payload.olderThanMinutes) : 10) * 60_000;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let refreshed = 0;
|
||||
let failed = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => refreshExpiringTokensForTenant(tx as unknown as Parameters<typeof refreshExpiringTokensForTenant>[0], tenantId, { now, windowMs }));
|
||||
refreshed += result.refreshed;
|
||||
failed += result.failed;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, refreshed, failed }, "integrations.token-refresh.completed");
|
||||
return { handled: true, action: "integrations.token-refresh.completed", details: { task: payload.task, tenants: tenants.length, refreshed, failed } };
|
||||
}
|
||||
|
||||
/**
|
||||
* R-P5 §P2d — probe every active connection's health across all tenants, persist
|
||||
* the status, and notify owners on degraded/broken. Same per-tenant RLS sweep
|
||||
* pattern; per-connection logic shared via worker-tasks.server.
|
||||
*/
|
||||
async function runIntegrationsHealthSweepHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let checked = 0;
|
||||
let unhealthy = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => sweepConnectionHealthForTenant(tx as unknown as Parameters<typeof sweepConnectionHealthForTenant>[0], tenantId, { now }));
|
||||
checked += result.checked;
|
||||
unhealthy += result.unhealthy;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, checked, unhealthy }, "integrations.health-sweep.completed");
|
||||
return { handled: true, action: "integrations.health-sweep.completed", details: { task: payload.task, tenants: tenants.length, checked, unhealthy } };
|
||||
}
|
||||
|
||||
async function runCalendarTokenRefreshHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const windowMs = (typeof payload.olderThanMinutes === "number" && payload.olderThanMinutes > 0 ? Math.floor(payload.olderThanMinutes) : 10) * 60_000;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let refreshed = 0;
|
||||
let failed = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => refreshCalendarTokensForTenant(tx as never, tenantId, { now, windowMs }));
|
||||
refreshed += result.refreshed;
|
||||
failed += result.failed;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, refreshed, failed }, "calendar.token-refresh.completed");
|
||||
return { handled: true, action: "calendar.token-refresh.completed", details: { task: payload.task, tenants: tenants.length, refreshed, failed } };
|
||||
}
|
||||
|
||||
async function runCalendarWebhookRenewalHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const thresholdMs = (typeof payload.olderThanMinutes === "number" && payload.olderThanMinutes > 0 ? Math.floor(payload.olderThanMinutes) : 24 * 60) * 60_000;
|
||||
const webhookUrl = process.env.GOOGLE_CALENDAR_WEBHOOK_URL;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let checked = 0;
|
||||
let renewed = 0;
|
||||
let failed = 0;
|
||||
let skipped = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => renewCalendarWebhooksForTenant(tx as never, tenantId, { now, thresholdMs, webhookUrl }));
|
||||
checked += result.checked;
|
||||
renewed += result.renewed;
|
||||
failed += result.failed;
|
||||
skipped += result.skipped;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, checked, renewed, failed, skipped }, "calendar.webhook-renewal.completed");
|
||||
return { handled: true, action: "calendar.webhook-renewal.completed", details: { task: payload.task, tenants: tenants.length, checked, renewed, failed, skipped } };
|
||||
}
|
||||
|
||||
async function runCalendarWebhookDrainHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const take = typeof payload.take === "number" && payload.take > 0 ? Math.floor(payload.take) : 50;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let claimed = 0;
|
||||
let completed = 0;
|
||||
let failed = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => drainCalendarWebhookEventsForTenant(tx as never, tenantId, { now, take }));
|
||||
claimed += result.claimed;
|
||||
completed += result.completed;
|
||||
failed += result.failed;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, claimed, completed, failed }, "calendar.webhook-drain.completed");
|
||||
return { handled: true, action: "calendar.webhook-drain.completed", details: { task: payload.task, tenants: tenants.length, claimed, completed, failed } };
|
||||
}
|
||||
|
||||
async function runCalendarInternalWriteSyncHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const take = typeof payload.take === "number" && payload.take > 0 ? Math.floor(payload.take) : 50;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let scanned = 0;
|
||||
let created = 0;
|
||||
let updated = 0;
|
||||
let deleted = 0;
|
||||
let conflicts = 0;
|
||||
let failed = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => syncInternalCalendarEventsForTenant(tx as never, tenantId, { now, take }));
|
||||
scanned += result.scanned;
|
||||
created += result.created;
|
||||
updated += result.updated;
|
||||
deleted += result.deleted;
|
||||
conflicts += result.conflicts;
|
||||
failed += result.failed;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, scanned, created, updated, deleted, conflicts, failed }, "calendar.internal-write-sync.completed");
|
||||
return {
|
||||
handled: true,
|
||||
action: "calendar.internal-write-sync.completed",
|
||||
details: { task: payload.task, tenants: tenants.length, scanned, created, updated, deleted, conflicts, failed },
|
||||
};
|
||||
}
|
||||
|
||||
async function runEmailInboxTokenRefreshHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const windowMs = (typeof payload.olderThanMinutes === "number" && payload.olderThanMinutes > 0 ? Math.floor(payload.olderThanMinutes) : 10) * 60_000;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let refreshed = 0;
|
||||
let failed = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => refreshEmailTokensForTenant(tx as never, tenantId, { now, windowMs }));
|
||||
refreshed += result.refreshed;
|
||||
failed += result.failed;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, refreshed, failed }, "email-inbox.token-refresh.completed");
|
||||
return { handled: true, action: "email-inbox.token-refresh.completed", details: { task: payload.task, tenants: tenants.length, refreshed, failed } };
|
||||
}
|
||||
|
||||
async function runEmailInboxWebhookRenewalHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const thresholdMs = (typeof payload.olderThanMinutes === "number" && payload.olderThanMinutes > 0 ? Math.floor(payload.olderThanMinutes) : 24 * 60) * 60_000;
|
||||
const graphWebhookUrl = process.env.MICROSOFT_MAIL_WEBHOOK_URL;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let checked = 0;
|
||||
let renewed = 0;
|
||||
let failed = 0;
|
||||
let skipped = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => renewEmailWebhooksForTenant(tx as never, tenantId, { now, thresholdMs, graphWebhookUrl }));
|
||||
checked += result.checked;
|
||||
renewed += result.renewed;
|
||||
failed += result.failed;
|
||||
skipped += result.skipped;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, checked, renewed, failed, skipped }, "email-inbox.webhook-renewal.completed");
|
||||
return { handled: true, action: "email-inbox.webhook-renewal.completed", details: { task: payload.task, tenants: tenants.length, checked, renewed, failed, skipped } };
|
||||
}
|
||||
|
||||
async function runEmailInboxWebhookDrainHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const take = typeof payload.take === "number" && payload.take > 0 ? Math.floor(payload.take) : 25;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let claimed = 0;
|
||||
let completed = 0;
|
||||
let failed = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => drainEmailWebhookEventsForTenant(tx as never, tenantId, { now, take, hook: emailInboxMessageHook }));
|
||||
claimed += result.claimed;
|
||||
completed += result.completed;
|
||||
failed += result.failed;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, claimed, completed, failed }, "email-inbox.webhook-drain.completed");
|
||||
return { handled: true, action: "email-inbox.webhook-drain.completed", details: { task: payload.task, tenants: tenants.length, claimed, completed, failed } };
|
||||
}
|
||||
|
||||
async function runEmailInboxInitialSyncHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const take = typeof payload.take === "number" && payload.take > 0 ? Math.floor(payload.take) : 10;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let folders = 0;
|
||||
let fetched = 0;
|
||||
let completed = 0;
|
||||
let failed = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => runEmailInitialSyncForTenant(tx as never, tenantId, { now, take, hook: emailInboxMessageHook }));
|
||||
folders += result.folders;
|
||||
fetched += result.fetched;
|
||||
completed += result.completed;
|
||||
failed += result.failed;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, folders, fetched, completed, failed }, "email-inbox.initial-sync.completed");
|
||||
return { handled: true, action: "email-inbox.initial-sync.completed", details: { task: payload.task, tenants: tenants.length, folders, fetched, completed, failed } };
|
||||
}
|
||||
|
||||
async function runEmailInboxStalePollHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const take = typeof payload.take === "number" && payload.take > 0 ? Math.floor(payload.take) : 25;
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let polled = 0;
|
||||
let failed = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => pollStaleEmailMailboxesForTenant(tx as never, tenantId, { now, take, hook: emailInboxMessageHook }));
|
||||
polled += result.polled;
|
||||
failed += result.failed;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, polled, failed }, "email-inbox.stale-poll.completed");
|
||||
return { handled: true, action: "email-inbox.stale-poll.completed", details: { task: payload.task, tenants: tenants.length, polled, failed } };
|
||||
}
|
||||
|
||||
async function runEmailInboxEventCleanupHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
let deleted = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const result = await withTenantRLS(db, tenantId, (tx) => cleanupEmailWebhookEventsForTenant(tx as never, tenantId, { now }));
|
||||
deleted += result.deleted;
|
||||
}
|
||||
logger.info({ task: payload.task, tenants: tenants.length, deleted }, "email-inbox.event-cleanup.completed");
|
||||
return { handled: true, action: "email-inbox.event-cleanup.completed", details: { task: payload.task, tenants: tenants.length, deleted } };
|
||||
}
|
||||
|
||||
// VA-P23 · auto-match + activity logging seam of the sync engine (M09 Block E/H).
|
||||
const emailInboxMessageHook = createEmailAutoMatchHook();
|
||||
|
||||
/**
|
||||
* P4 Tasks (AK-P4b) §7 — reminder sweep (DoD 4). For each open task with a
|
||||
* deadline in scan range, sends reminders to the assignee (or assigned group's
|
||||
@@ -1281,6 +1650,170 @@ async function runTaskReminderSweepHandler(payload: QueuePayload<typeof queueNam
|
||||
return { handled: true, action: "tasks.reminder-sweep.completed", details: { task: payload.task, tenants: tenants.length, scanned, sent } };
|
||||
}
|
||||
|
||||
/**
|
||||
* P17 HR-Hub (AK-P17b) — upload-reminder dispatch. For every active reminder
|
||||
* past its `dueAt`, fan out per-employee dispatch rows (idempotent via the unique
|
||||
* reminder+employee+dueAt), notify each employee, advance the reminder by its
|
||||
* cadence (or complete a one-off), then escalate overdue mandatory dispatches to
|
||||
* the employee's manager. Per-tenant from the global Tenant registry; all HR
|
||||
* reads/writes run inside withTenantRLS() so the policy binds once activated.
|
||||
* notify() runs outside the tenant transaction (slow I/O), deduped by sourceKey.
|
||||
*/
|
||||
async function runHrUploadReminderDispatchHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const db = await getDefaultDbClient();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
|
||||
let dispatched = 0;
|
||||
let escalated = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const due = await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.employeeUploadReminder.findMany({ where: { tenantId, status: "active", archivedAt: null, dueAt: { not: null, lte: now } } }),
|
||||
);
|
||||
const profiles =
|
||||
due.length > 0
|
||||
? await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.hrEmployeeProfile.findMany({ where: { tenantId }, select: { userId: true, team: true, locationId: true } }),
|
||||
)
|
||||
: [];
|
||||
|
||||
for (const reminder of due) {
|
||||
const dueAt = reminder.dueAt ?? now;
|
||||
const members = profiles.filter((profile) => audienceMatches(reminder.audienceKind, reminder.audienceRef, profile));
|
||||
|
||||
for (const member of members) {
|
||||
const created = await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.employeeUploadReminderDispatch.createMany({
|
||||
data: [{ tenantId, reminderId: reminder.id, employeeUserId: member.userId, dueAt, status: "sent", sentAt: now }],
|
||||
skipDuplicates: true,
|
||||
}),
|
||||
);
|
||||
if (created.count > 0) {
|
||||
dispatched += created.count;
|
||||
await notify(
|
||||
{
|
||||
tenantId,
|
||||
userId: member.userId,
|
||||
channel: "IN_APP",
|
||||
category: "hr",
|
||||
messageKey: "hr.notify.reminder.due",
|
||||
sourceKey: `hr-reminder:${reminder.id}:${dueAt.getTime()}:${member.userId}`,
|
||||
payloadMetadata: { reminderId: reminder.id },
|
||||
},
|
||||
db as unknown as NotificationClient,
|
||||
).catch((err) => logger.error({ task: payload.task, reminderId: reminder.id, userId: member.userId, err: err instanceof Error ? err.message : String(err) }, "hr.upload-reminder.notify-failed"));
|
||||
}
|
||||
}
|
||||
|
||||
const next = nextReminderDue({ cadenceKind: reminder.cadenceKind, intervalMonths: reminder.intervalMonths, from: dueAt });
|
||||
await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.employeeUploadReminder.update({ where: { id: reminder.id }, data: next ? { dueAt: next } : { dueAt: null, status: "completed" } }),
|
||||
);
|
||||
}
|
||||
|
||||
// Escalate still-unacknowledged mandatory dispatches past the grace window.
|
||||
const cutoff = new Date(now.getTime() - HR_UPLOAD_REMINDER_ESCALATION_GRACE_MS);
|
||||
const stale = await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.employeeUploadReminderDispatch.findMany({ where: { tenantId, status: "sent", dueAt: { lt: cutoff }, reminder: { escalateToManager: true, mandatory: true } } }),
|
||||
);
|
||||
const staleEmployeeUserIds = [...new Set(stale.map((dispatch) => dispatch.employeeUserId))];
|
||||
const escalationProfiles =
|
||||
staleEmployeeUserIds.length > 0
|
||||
? await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.hrEmployeeProfile.findMany({
|
||||
where: { tenantId, userId: { in: staleEmployeeUserIds } },
|
||||
select: { userId: true, managerUserId: true },
|
||||
}),
|
||||
)
|
||||
: [];
|
||||
const managerUserIdsByEmployee = new Map(escalationProfiles.map((profile) => [profile.userId, profile.managerUserId]));
|
||||
for (const dispatch of stale) {
|
||||
await withTenantRLS(db, tenantId, (tx) => tx.employeeUploadReminderDispatch.update({ where: { id: dispatch.id }, data: { status: "escalated", escalatedAt: now } }));
|
||||
escalated += 1;
|
||||
const managerUserId = managerUserIdsByEmployee.get(dispatch.employeeUserId);
|
||||
if (managerUserId) {
|
||||
await notify(
|
||||
{
|
||||
tenantId,
|
||||
userId: managerUserId,
|
||||
channel: "IN_APP",
|
||||
category: "hr",
|
||||
messageKey: "hr.notify.reminder.escalated",
|
||||
sourceKey: `hr-reminder-escalate:${dispatch.id}`,
|
||||
payloadMetadata: { dispatchId: dispatch.id },
|
||||
},
|
||||
db as unknown as NotificationClient,
|
||||
).catch((err) => logger.error({ task: payload.task, dispatchId: dispatch.id, err: err instanceof Error ? err.message : String(err) }, "hr.upload-reminder.escalate-notify-failed"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.info({ task: payload.task, tenants: tenants.length, dispatched, escalated }, "hr.upload-reminder-dispatch.completed");
|
||||
return { handled: true, action: "hr.upload-reminder-dispatch.completed", details: { task: payload.task, tenants: tenants.length, dispatched, escalated } };
|
||||
}
|
||||
|
||||
/**
|
||||
* AK-P14a · P14 Email — sending-domain DNS auto-probe (5-minute cadence). For
|
||||
* each active tenant, resolves DKIM/SPF/DMARC for every not-yet-settled sending
|
||||
* domain and advances `verificationStatus` via the pure decideDomainProbeStatus()
|
||||
* helper. Reads/writes run inside withTenantRLS() so the policy binds once the
|
||||
* P5 cutover activates RLS; the domain UPDATE is auto-audited by the M001 Prisma
|
||||
* middleware. A tenant-level notification fires only on the verified/failed
|
||||
* transition (not on every probe). The DNS resolver is the node:dns binding; the
|
||||
* decision logic is unit-tested without DNS/DB.
|
||||
*/
|
||||
async function runEmailDomainProbeHandler(payload: QueuePayload<typeof queueNames.maintenance>, deps: HandlerDependencies): Promise<HandlerResult> {
|
||||
const logger = deps.logger ?? noopHandlerLogger;
|
||||
const now = new Date();
|
||||
const db = await getDefaultDbClient();
|
||||
const resolver: DomainDnsResolver = createDnsDomainResolver();
|
||||
const tenants = await db.tenant.findMany({ where: { active: true }, select: { id: true } });
|
||||
|
||||
let probed = 0;
|
||||
let verified = 0;
|
||||
let failed = 0;
|
||||
for (const { id: tenantId } of tenants) {
|
||||
const domains = await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.tenantSendingDomain.findMany({ where: { tenantId, archivedAt: null, verificationStatus: { in: ["pending", "verifying"] } } }),
|
||||
);
|
||||
|
||||
for (const domain of domains) {
|
||||
probed += 1;
|
||||
const dns = await resolver({ domain: domain.domain, dkimSelector: domain.dkimSelector });
|
||||
const firstProbeAt = domain.firstProbeAt ?? now;
|
||||
const decision = decideDomainProbeStatus({ dns, firstProbeAt, now });
|
||||
const changed = decision.status !== domain.verificationStatus;
|
||||
|
||||
await withTenantRLS(db, tenantId, (tx) =>
|
||||
tx.tenantSendingDomain.update({
|
||||
where: { id: domain.id },
|
||||
data: { verificationStatus: decision.status, lastError: decision.lastError, lastProbedAt: now, firstProbeAt },
|
||||
}),
|
||||
);
|
||||
|
||||
if (changed && (decision.status === "verified" || decision.status === "failed")) {
|
||||
if (decision.status === "verified") verified += 1;
|
||||
else failed += 1;
|
||||
await notify(
|
||||
{
|
||||
tenantId,
|
||||
channel: "IN_APP",
|
||||
category: "email",
|
||||
messageKey: decision.status === "verified" ? "email.domain.notify.verified" : "email.domain.notify.failed",
|
||||
sourceKey: `email-domain-${decision.status}:${domain.id}`,
|
||||
payloadMetadata: { domainId: domain.id, domain: domain.domain },
|
||||
},
|
||||
db as unknown as NotificationClient,
|
||||
).catch((err) => logger.error({ task: payload.task, domainId: domain.id, err: err instanceof Error ? err.message : String(err) }, "email.domain-probe.notify-failed"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.info({ task: payload.task, tenants: tenants.length, probed, verified, failed }, "email.domain-probe.completed");
|
||||
return { handled: true, action: "email.domain-probe.completed", details: { task: payload.task, tenants: tenants.length, probed, verified, failed } };
|
||||
}
|
||||
|
||||
/**
|
||||
* Phase 10 #68 — maintenance-handler adapter for the DSGVO grace-period
|
||||
* scan. Wraps `runDsgvoGraceCron` (the pure unit-tested core) with the
|
||||
@@ -1352,6 +1885,10 @@ async function runExportHandler(
|
||||
});
|
||||
}
|
||||
|
||||
if (isPrivacyGdprExportType(payload.exportType)) {
|
||||
return runPrivacyGdprExportHandler(payload, deps, context);
|
||||
}
|
||||
|
||||
const markPaused = deps.markExportJobPaused ?? markExportJobPausedInDb;
|
||||
const markResumed = deps.markExportJobResumed ?? markExportJobResumedInDb;
|
||||
const markRunning = deps.markExportJobRunning ?? markExportJobRunningInDb;
|
||||
@@ -1434,6 +1971,66 @@ async function runExportHandler(
|
||||
}
|
||||
}
|
||||
|
||||
async function runPrivacyGdprExportHandler(
|
||||
payload: ExportJob,
|
||||
deps: HandlerDependencies,
|
||||
context?: HandlerExecutionContext<typeof queueNames.exports>
|
||||
): Promise<HandlerResult> {
|
||||
const attemptsMade = readAttemptsMade(payload.metadata, context);
|
||||
const markProcessing = deps.markGdprExportProcessing ?? markGdprExportProcessingInDb;
|
||||
const markReady = deps.markGdprExportReady ?? markGdprExportReadyInDb;
|
||||
const markFailed = deps.markGdprExportFailed ?? markGdprExportFailedInDb;
|
||||
|
||||
await markProcessing({ id: payload.exportJobId, tenantId: payload.tenantId });
|
||||
|
||||
if (!deps.privacyGdprExportService) {
|
||||
const message = "Privacy GDPR export service is not configured.";
|
||||
await markFailed({
|
||||
id: payload.exportJobId,
|
||||
tenantId: payload.tenantId,
|
||||
errorMessage: message,
|
||||
});
|
||||
throw new ExportHandlerError(message, buildExportDetails(payload, { status: "failed", attemptsMade, code: "privacy_gdpr_export_service_missing" }));
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await deps.privacyGdprExportService.runExport(payload);
|
||||
await markReady({
|
||||
id: payload.exportJobId,
|
||||
tenantId: payload.tenantId,
|
||||
resultBucket: result.artifactBucket,
|
||||
resultKey: result.artifactKey,
|
||||
resultSizeBytes: result.sizeBytes,
|
||||
resultExpiresAt: result.expiresAt,
|
||||
});
|
||||
|
||||
return {
|
||||
handled: true,
|
||||
action: "privacy-gdpr-export.completed",
|
||||
details: buildExportDetails(payload, {
|
||||
artifactBucket: result.artifactBucket,
|
||||
artifactKey: result.artifactKey,
|
||||
attemptsMade,
|
||||
expiresAt: result.expiresAt.toISOString(),
|
||||
rowCount: result.rowCount,
|
||||
sizeBytes: result.sizeBytes,
|
||||
status: "ready",
|
||||
}),
|
||||
};
|
||||
} catch (error) {
|
||||
const errorName = error instanceof Error ? error.name : "Error";
|
||||
const errorMessage = redactSecretLikeText(error instanceof Error ? error.message : String(error));
|
||||
if (isTerminalJobAttempt(attemptsMade, context)) {
|
||||
await markFailed({
|
||||
id: payload.exportJobId,
|
||||
tenantId: payload.tenantId,
|
||||
errorMessage,
|
||||
});
|
||||
}
|
||||
throw new ExportHandlerError("Privacy GDPR export job failed.", buildExportDetails(payload, { status: "failed", attemptsMade, errorName, message: errorMessage }));
|
||||
}
|
||||
}
|
||||
|
||||
async function runImportHandler(
|
||||
payload: QueuePayload<typeof queueNames.imports>,
|
||||
deps: HandlerDependencies,
|
||||
@@ -1743,14 +2340,16 @@ function serializeWebhookResponseDiagnostics(response: Response, body: string) {
|
||||
}
|
||||
|
||||
async function fetchWebhookWithRedirects(
|
||||
fetchImpl: typeof fetch,
|
||||
fetchImpl: typeof fetch | undefined,
|
||||
input: string,
|
||||
init: RequestInit,
|
||||
addressResolver?: OutboundHttpAddressResolver
|
||||
): Promise<Response> {
|
||||
try {
|
||||
return await fetchOutboundHttpWithRedirects(input, init, {
|
||||
fetchImpl,
|
||||
// In production fetchImpl is undefined, so the guard uses its pinned
|
||||
// undici fetch; a test-injected fetchImpl disables pinning.
|
||||
...(fetchImpl ? { fetchImpl } : {}),
|
||||
...(addressResolver ? { addressResolver } : {}),
|
||||
timeoutMs: DEFAULT_WORKER_FETCH_TIMEOUT_MS,
|
||||
redirectLimit: WEBHOOK_REDIRECT_LIMIT,
|
||||
|
||||
Reference in new issue
Block a user