import { getDbClient, type DbClient } from "./client.server.js"; export const EXPORT_JOB_ERROR_MESSAGE_MAX_LENGTH = 2_000; export const EXPORT_JOB_RETENTION_ERROR_MESSAGE_MAX_LENGTH = 2_000; export const EXPORT_JOB_RETENTION_DEFAULT_TAKE = 100; export const EXPORT_JOB_RETENTION_MAX_TAKE = 500; export type ExportJobStatus = "PENDING" | "RUNNING" | "COMPLETED" | "FAILED"; export type ExportJobCreateInput = { tenantId: string; requestedByUserId?: string | null; exportType: string; format: string; requestId: string; attemptsMade?: number | null; expiresAt?: Date | null; }; export type ExportJobRunningInput = { id: string; tenantId: string; attemptsMade: number; startedAt?: Date | null; }; export type ExportJobSucceededInput = { id: string; tenantId: string; artifactBucket: string; artifactKey: string; rowCount: number; attemptsMade?: number | null; finishedAt?: Date | null; expiresAt?: Date | null; }; export type ExportJobFailedInput = { id: string; tenantId: string; attemptsMade: number; errorName?: string | null; errorMessage?: string | null; finishedAt?: Date | null; }; export type ExportJobPausedInput = { id: string; tenantId: string; attemptsMade?: number | null; reason?: string | null; }; export type ExportJobResumedInput = { id: string; tenantId: string; attemptsMade?: number | null; }; export type ExportJobDownloadRecord = { id: string; tenantId: string; requestedByUserId: string | null; format: string; status: ExportJobStatus; artifactBucket: string | null; artifactKey: string | null; rowCount: number | null; expiresAt: Date | null; deletedAt?: Date | null; hardDeletedAt?: Date | null; errorName: string | null; requestId: string; }; export type ExportJobDownloadLookupInput = { id: string; tenantId: string; }; export type ExportJobRetentionRecord = { id: string; tenantId: string; status: ExportJobStatus; artifactBucket: string; artifactKey: string; rowCount: number | null; expiresAt: Date | null; deletedAt?: Date | null; requestId: string; }; export type ExportJobRetentionListInput = { now: Date; take?: number | null; }; export type ExportJobExpiredInput = { id: string; tenantId: string; deletedAt?: Date | null; }; export type ExportJobHardDeleteSucceededInput = { id: string; tenantId: string; hardDeletedAt?: Date | null; }; export type ExportJobHardDeleteFailedInput = { id: string; tenantId: string; errorMessage?: string | null; }; type ExportJobCreateData = { tenantId: string; requestedByUserId: string | null; exportType: string; format: string; status: Extract; requestId: string; attemptsMade: number; artifactBucket: null; artifactKey: null; rowCount: null; errorName: null; errorMessage: null; retentionErrorMessage: null; expiresAt: Date | null; deletedAt: null; hardDeletedAt: null; startedAt: null; finishedAt: null; }; type ExportJobUpdateData = Partial<{ status: ExportJobStatus; attemptsMade: number; artifactBucket: string | null; artifactKey: string | null; rowCount: number | null; errorName: string | null; errorMessage: string | null; retentionErrorMessage: string | null; expiresAt: Date | null; deletedAt: Date | null; hardDeletedAt: Date | null; startedAt: Date | null; finishedAt: Date | null; }>; export type ExportJobWriterClient = { exportJob: { create(args: { data: ExportJobCreateData }): Promise; update(args: { where: { id: string }; data: ExportJobUpdateData }): Promise; updateMany(args: { where: { id: string; tenantId: string }; data: ExportJobUpdateData }): Promise<{ count?: number }>; }; }; export type ExportJobReaderClient = { exportJob: { findFirst(args: { where: { id: string; tenantId?: string }; select: { id: true; tenantId: true; requestedByUserId: true; format: true; status: true; artifactBucket: true; artifactKey: true; rowCount: true; expiresAt: true; deletedAt: true; hardDeletedAt: true; errorName: true; requestId: true; }; }): Promise; }; }; export type ExportJobRetentionReaderClient = { exportJob: { findMany(args: { where: | { status: Extract; expiresAt: { not: null; lte: Date }; artifactBucket: { not: null }; artifactKey: { not: null }; rowCount: { not: null }; } | { status: Extract; deletedAt: { not: null; lte: Date }; hardDeletedAt: null; artifactBucket: { not: null }; artifactKey: { not: null }; }; orderBy: [{ expiresAt: "asc" }, { id: "asc" }] | [{ deletedAt: "asc" }, { id: "asc" }]; take: number; select: { id: true; tenantId: true; status: true; artifactBucket: true; artifactKey: true; rowCount: true; expiresAt: true; deletedAt?: true; requestId: true; }; }): Promise; }; }; function normalizeAttemptsMade(attemptsMade?: number | null): number { return attemptsMade ?? 0; } function truncateErrorMessage(errorMessage: string): string { if (errorMessage.length <= EXPORT_JOB_ERROR_MESSAGE_MAX_LENGTH) { return errorMessage; } return errorMessage.slice(0, EXPORT_JOB_ERROR_MESSAGE_MAX_LENGTH); } function redactExportErrorMessage(errorMessage: string): string { return truncateErrorMessage(redactDiagnosticText(errorMessage)); } function redactDiagnosticText(value: string): string { return value .replace(/(authorization)\s*=\s*Bearer\s+\S+/gi, "$1=Bearer [REDACTED]") .replace(/Bearer\s+\S+/gi, "Bearer [REDACTED]") .replace(/(password|secret|token|api[-_]?key)=\S+/gi, "$1=[REDACTED]"); } function redactExportPauseReason(reason: string): string { return truncateErrorMessage(redactDiagnosticText(reason)); } function redactRetentionErrorMessage(errorMessage: string): string { const redacted = redactDiagnosticText(errorMessage); if (redacted.length <= EXPORT_JOB_RETENTION_ERROR_MESSAGE_MAX_LENGTH) { return redacted; } return redacted.slice(0, EXPORT_JOB_RETENTION_ERROR_MESSAGE_MAX_LENGTH); } function normalizeRetentionTake(take?: number | null): number { if (typeof take !== "number" || !Number.isFinite(take)) { return EXPORT_JOB_RETENTION_DEFAULT_TAKE; } return Math.max(1, Math.min(EXPORT_JOB_RETENTION_MAX_TAKE, Math.floor(take))); } async function updateExportJobForTenant( client: ExportJobWriterClient, data: { id: string; tenantId: string; update: ExportJobUpdateData }, ): Promise { const id = data.id.trim(); const tenantId = data.tenantId.trim(); if (!id || !tenantId) { throw new Error("Export job tenant scope is required."); } const result = await client.exportJob.updateMany({ where: { id, tenantId }, data: data.update, }); if (typeof result.count === "number" && result.count !== 1) { throw new Error("Export job tenant scope did not match exactly one job."); } return result; } export async function findExportJobForDownload( data: ExportJobDownloadLookupInput, client: ExportJobReaderClient = getDbClient() as DbClient & ExportJobReaderClient, ): Promise { const id = data.id.trim(); const tenantId = data.tenantId.trim(); if (!id || !tenantId) { throw new Error("Export job download tenant scope is required."); } return client.exportJob.findFirst({ where: { id, tenantId, }, select: { id: true, tenantId: true, requestedByUserId: true, format: true, status: true, artifactBucket: true, artifactKey: true, rowCount: true, expiresAt: true, deletedAt: true, hardDeletedAt: true, errorName: true, requestId: true, }, }); } export async function listSoftDeletedExportJobsForHardDelete( data: ExportJobRetentionListInput, client: ExportJobRetentionReaderClient = getDbClient() as DbClient & ExportJobRetentionReaderClient, ): Promise { return client.exportJob.findMany({ where: { status: "FAILED", deletedAt: { not: null, lte: data.now }, hardDeletedAt: null, artifactBucket: { not: null }, artifactKey: { not: null }, }, orderBy: [{ deletedAt: "asc" }, { id: "asc" }], take: normalizeRetentionTake(data.take), select: { id: true, tenantId: true, status: true, artifactBucket: true, artifactKey: true, rowCount: true, expiresAt: true, deletedAt: true, requestId: true, }, }); } export async function listExpiredCompletedExportJobs( data: ExportJobRetentionListInput, client: ExportJobRetentionReaderClient = getDbClient() as DbClient & ExportJobRetentionReaderClient, ): Promise { return client.exportJob.findMany({ where: { status: "COMPLETED", expiresAt: { not: null, lte: data.now }, artifactBucket: { not: null }, artifactKey: { not: null }, rowCount: { not: null }, }, orderBy: [{ expiresAt: "asc" }, { id: "asc" }], take: normalizeRetentionTake(data.take), select: { id: true, tenantId: true, status: true, artifactBucket: true, artifactKey: true, rowCount: true, expiresAt: true, requestId: true, }, }); } export async function createExportJob( data: ExportJobCreateInput, client: ExportJobWriterClient = getDbClient() as DbClient & ExportJobWriterClient, ): Promise { return client.exportJob.create({ data: { tenantId: data.tenantId, requestedByUserId: data.requestedByUserId ?? null, exportType: data.exportType, format: data.format, status: "PENDING", requestId: data.requestId, attemptsMade: normalizeAttemptsMade(data.attemptsMade), artifactBucket: null, artifactKey: null, rowCount: null, errorName: null, errorMessage: null, retentionErrorMessage: null, expiresAt: data.expiresAt ?? null, deletedAt: null, hardDeletedAt: null, startedAt: null, finishedAt: null, }, }); } export async function markExportJobRunning( data: ExportJobRunningInput, client: ExportJobWriterClient = getDbClient() as DbClient & ExportJobWriterClient, ): Promise { return updateExportJobForTenant(client, { id: data.id, tenantId: data.tenantId, update: { status: "RUNNING", attemptsMade: normalizeAttemptsMade(data.attemptsMade), artifactBucket: null, artifactKey: null, rowCount: null, errorName: null, errorMessage: null, retentionErrorMessage: null, deletedAt: null, hardDeletedAt: null, startedAt: data.startedAt ?? new Date(), finishedAt: null, }, }); } export async function markExportJobSucceeded( data: ExportJobSucceededInput, client: ExportJobWriterClient = getDbClient() as DbClient & ExportJobWriterClient, ): Promise { return updateExportJobForTenant(client, { id: data.id, tenantId: data.tenantId, update: { status: "COMPLETED", attemptsMade: normalizeAttemptsMade(data.attemptsMade), artifactBucket: data.artifactBucket, artifactKey: data.artifactKey, rowCount: data.rowCount, errorName: null, errorMessage: null, retentionErrorMessage: null, expiresAt: data.expiresAt ?? null, deletedAt: null, hardDeletedAt: null, finishedAt: data.finishedAt ?? new Date(), }, }); } export async function markExportJobFailed( data: ExportJobFailedInput, client: ExportJobWriterClient = getDbClient() as DbClient & ExportJobWriterClient, ): Promise { return updateExportJobForTenant(client, { id: data.id, tenantId: data.tenantId, update: { status: "FAILED", attemptsMade: normalizeAttemptsMade(data.attemptsMade), artifactBucket: null, artifactKey: null, rowCount: null, errorName: data.errorName ?? "Error", errorMessage: redactExportErrorMessage(data.errorMessage ?? ""), finishedAt: data.finishedAt ?? new Date(), }, }); } export async function markExportJobPaused( data: ExportJobPausedInput, client: ExportJobWriterClient = getDbClient() as DbClient & ExportJobWriterClient, ): Promise { return updateExportJobForTenant(client, { id: data.id, tenantId: data.tenantId, update: { status: "PENDING", attemptsMade: normalizeAttemptsMade(data.attemptsMade), artifactBucket: null, artifactKey: null, rowCount: null, errorName: "ExportPaused", errorMessage: redactExportPauseReason(data.reason ?? ""), retentionErrorMessage: null, expiresAt: null, deletedAt: null, hardDeletedAt: null, startedAt: null, finishedAt: null, }, }); } export async function markExportJobResumed( data: ExportJobResumedInput, client: ExportJobWriterClient = getDbClient() as DbClient & ExportJobWriterClient, ): Promise { return updateExportJobForTenant(client, { id: data.id, tenantId: data.tenantId, update: { status: "PENDING", attemptsMade: normalizeAttemptsMade(data.attemptsMade), artifactBucket: null, artifactKey: null, rowCount: null, errorName: null, errorMessage: null, retentionErrorMessage: null, expiresAt: null, deletedAt: null, hardDeletedAt: null, startedAt: null, finishedAt: null, }, }); } export async function markExportJobExpired( data: ExportJobExpiredInput, client: ExportJobWriterClient = getDbClient() as DbClient & ExportJobWriterClient, ): Promise { const deletedAt = data.deletedAt ?? new Date(); return updateExportJobForTenant(client, { id: data.id, tenantId: data.tenantId, update: { status: "FAILED", errorName: "ExportRetentionExpired", errorMessage: "Export artifact expired by retention policy.", retentionErrorMessage: null, deletedAt, hardDeletedAt: null, finishedAt: deletedAt, }, }); } export async function markExportJobHardDeleteSucceeded( data: ExportJobHardDeleteSucceededInput, client: ExportJobWriterClient = getDbClient() as DbClient & ExportJobWriterClient, ): Promise { return updateExportJobForTenant(client, { id: data.id, tenantId: data.tenantId, update: { artifactBucket: null, artifactKey: null, retentionErrorMessage: null, hardDeletedAt: data.hardDeletedAt ?? new Date(), }, }); } export async function markExportJobHardDeleteFailed( data: ExportJobHardDeleteFailedInput, client: ExportJobWriterClient = getDbClient() as DbClient & ExportJobWriterClient, ): Promise { return updateExportJobForTenant(client, { id: data.id, tenantId: data.tenantId, update: { retentionErrorMessage: redactRetentionErrorMessage(data.errorMessage ?? ""), }, }); }