papertrail/lib/compliance/retention.ts
caelum0x 695a365ae4 feat: PaperTrail — enterprise evidence-verification platform
Multi-tenant platform (182 pages, 228 API routes, ~30 modules) built on a
deterministic verification core: clinical-trial claims checked against
ClinicalTrials.gov registered results and raw event counts (ARR/NNT/RR+CI
recomputed in code, oracle-tested), code-enforced exact-span grounding, and
multi-source cross-verification. Auth/RBAC/multi-tenancy, document processing
at scale (unpdf + Docling), agentic workflows, Claude Science connector,
compliance/audit, billing, public API, and more.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 07:21:22 +03:00

66 lines
1.9 KiB
TypeScript

import type { Pool } from "pg";
import { getPool } from "@/lib/db";
import type { RetentionPolicy } from "@/lib/compliance/types";
// Data retention policies: how long (retain_days) records of a given entity_type
// must be kept before they may be purged. One policy per (org, entity_type) —
// upserting replaces the window rather than accumulating duplicates.
interface RetentionRow {
id: string;
org_id: string;
entity_type: string;
retain_days: number | string;
created_at: string | Date;
}
function mapRow(row: RetentionRow): RetentionPolicy {
return {
id: row.id,
org_id: row.org_id,
entity_type: row.entity_type,
retain_days: Number(row.retain_days),
created_at:
row.created_at instanceof Date
? row.created_at.toISOString()
: String(row.created_at),
};
}
export async function listRetentionPolicies(
orgId: string,
pool: Pool = getPool()
): Promise<RetentionPolicy[]> {
const { rows } = await pool.query<RetentionRow>(
`select id, org_id, entity_type, retain_days, created_at
from retention_policies
where org_id = $1
order by entity_type asc`,
[orgId]
);
return rows.map(mapRow);
}
export interface UpsertRetentionInput {
orgId: string;
entityType: string;
retainDays: number;
}
// Create or update the retention window for an entity type. Idempotent per
// (org, entity_type).
export async function upsertRetentionPolicy(
input: UpsertRetentionInput,
pool: Pool = getPool()
): Promise<RetentionPolicy> {
const { rows } = await pool.query<RetentionRow>(
`insert into retention_policies (org_id, entity_type, retain_days)
values ($1, $2, $3)
on conflict (org_id, entity_type)
do update set retain_days = excluded.retain_days
returning id, org_id, entity_type, retain_days, created_at`,
[input.orgId, input.entityType, input.retainDays]
);
return mapRow(rows[0]);
}