Update hivegrid-cloud reference to latest main

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

No files matched your search

@@ -0,0 +1,133 @@
/**
* AK-P3 AP-2 — synchronous Audit-Export into the (private) object storage.
*
* Decision (ticket §1, Owner-confirmed): v1 is a synchronous export, NOT an
* async AuditExportJob (that is v2). The export builds a CSV/NDJSON artifact,
* uploads it to the files bucket under `audit-exports/...`, returns a short-lived
* signed download URL, and records an `audit.exported` event. A purpose
* (`Zweck`) is mandatory.
*
* Retention: the signed URL TTL is the fixed default retention window. Object
* cleanup of the `audit-exports/` prefix is a bucket lifecycle rule (infra) —
* there is no per-export tracking table in v1 (AuditExportJob = v2) and the
* storage adapter has no list operation, so an app-side enumerating cron is not
* possible here. Audit-LOG-row retention is handled by the existing worker job
* (`audit-retention`).
*/
import { getDbClient } from "@hivegrid/db/client.server";
import { createAuditEvent } from "@hivegrid/db/audit-events.server";
import { putBufferAsFile, resolveStorageBucket, hasStorageCredentials } from "~/utils/integrations/storageService";
import { createMediaDownloadUrl } from "~/utils/integrations/mediaSigning.server";
import {
AUDIT_EXPORT_MAX_ROWS,
buildAuditExportWhere,
computeAuditIntegritySummary,
renderAuditExportCsv,
renderAuditExportNdjson,
type AuditExportFilters,
type AuditExportRow,
} from "~/routes/admin/audit-export.helpers.server";
/** Fixed default retention window for an export artifact (== signed-URL TTL). */
export const AUDIT_EXPORT_RETENTION_SECONDS = 7 * 24 * 60 * 60;
export type AuditExportFormat = "csv" | "ndjson";
export type AuditExportActor = {
id?: string | null;
email?: string | null;
type?: string | null;
ip?: string | null;
};
export type AuditExportToStorageInput = {
filters: AuditExportFilters;
format: AuditExportFormat;
/** Mandatory purpose-of-processing note (DoD: Zweck-Pflichtfeld). */
purpose: string;
actor: AuditExportActor;
/** Audit-chain scope: null = platform/admin export across all tenants. */
tenantId: string | null;
};
export type AuditExportToStorageResult = {
url: string;
key: string;
rowCount: number;
integrityOk: boolean;
expiresInSeconds: number;
};
/** Thrown when no purpose was supplied; the route maps this to an i18n message. */
export class AuditExportPurposeRequiredError extends Error {
constructor() {
super("audit_export_purpose_required");
this.name = "AuditExportPurposeRequiredError";
}
}
/** Thrown when no object storage is configured; the route maps this to i18n. */
export class AuditExportStorageUnavailableError extends Error {
constructor() {
super("audit_export_storage_unavailable");
this.name = "AuditExportStorageUnavailableError";
}
}
export function parseAuditExportFormat(raw: string | null | undefined): AuditExportFormat {
return raw === "ndjson" ? "ndjson" : "csv";
}
export async function runAuditExportToStorage(input: AuditExportToStorageInput): Promise<AuditExportToStorageResult> {
if (!input.purpose || input.purpose.trim().length === 0) {
throw new AuditExportPurposeRequiredError();
}
if (!hasStorageCredentials()) {
throw new AuditExportStorageUnavailableError();
}
const db = getDbClient();
const rows = (await db.auditEvent.findMany({
where: buildAuditExportWhere(input.filters),
orderBy: { createdAt: "asc" },
take: AUDIT_EXPORT_MAX_ROWS,
})) as unknown as AuditExportRow[];
const integrity = computeAuditIntegritySummary(rows);
const body = input.format === "ndjson" ? renderAuditExportNdjson(rows, integrity) : renderAuditExportCsv(rows, integrity);
const ext = input.format === "ndjson" ? "ndjson" : "csv";
const contentType = input.format === "ndjson" ? "application/x-ndjson; charset=utf-8" : "text/csv; charset=utf-8";
const scope = input.tenantId ?? "platform";
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
const rand = Math.random().toString(36).slice(2, 10);
const filename = `audit-export-${scope}-${stamp}.${ext}`;
const key = `audit-exports/${scope}/${stamp}-${rand}.${ext}`;
const bucket = resolveStorageBucket();
await putBufferAsFile({ bucket, key, body: new TextEncoder().encode(body), contentType });
const url = await createMediaDownloadUrl({ key, filename, ttlSeconds: AUDIT_EXPORT_RETENTION_SECONDS });
// Every export is itself audited (DoD: audit.exported), with the purpose recorded.
await createAuditEvent({
tenantId: input.tenantId,
actorId: input.actor.id ?? null,
actorType: input.actor.type ?? "admin",
actorEmail: input.actor.email ?? null,
actorIp: input.actor.ip ?? null,
action: "audit.exported",
targetType: "AuditExport",
targetId: key,
sourceModule: "audit",
dataSensitivity: "confidential",
metadata: {
purpose: input.purpose.trim(),
format: input.format,
rowCount: rows.length,
integrityOk: integrity.ok,
key,
},
});
return { url, key, rowCount: rows.length, integrityOk: integrity.ok, expiresInSeconds: AUDIT_EXPORT_RETENTION_SECONDS };
}
@@ -2,7 +2,7 @@
* Phase 10 Task #67 — Inline PDF helper for the admin/tenant audit-export
* routes.
*
* Wraps `serializeAuditEventsToPdf` from `@toolctrl/exports/audit-pdf`
* Wraps `serializeAuditEventsToPdf` from `@hivegrid/exports/audit-pdf`
* with the same row + integrity shape the admin route already computes
* for the CSV path. Wired into the export route via `?format=pdf`
* (the default remains CSV).
@@ -18,7 +18,7 @@ import {
type AuditPdfIntegrity,
type AuditPdfLabelOverrides,
type AuditPdfReportInput,
} from "@toolctrl/exports/audit-pdf";
} from "@hivegrid/exports/audit-pdf";
import type { TFunction } from "i18next";
import type { AuditExportFilters, AuditExportRow, AuditIntegritySummary } from "~/routes/admin/audit-export.helpers.server";
@@ -27,7 +27,7 @@ export { AUDIT_PDF_CONTENT_TYPE, AUDIT_PDF_HARD_ROW_LIMIT };
/**
* Map an in-memory AuditExportRow (from the existing CSV pipeline) to
* the AuditPdfEvent shape consumed by @toolctrl/exports/audit-pdf.
* the AuditPdfEvent shape consumed by @hivegrid/exports/audit-pdf.
*
* `metadata` is opaque JSON in Prisma — we keep it as-is; the renderer
* never inlines metadata into the report body (privacy / page-density