import { usePrisma } from "../utils/prisma"; /** Schreibt einen neuen Consent-Eintrag (Einwilligung). */ export async function writeConsentGrant(params: { userId: string; consentType: string; consentVersion: string; consentAt: Date; ipAddress?: string | null; userAgent?: string | null; mailConnectionId?: string | null; }) { const db = usePrisma(); return db.consentLog.create({ data: { userId: params.userId, consentType: params.consentType, consentVersion: params.consentVersion, consentAt: params.consentAt, ipAddress: params.ipAddress ?? null, userAgent: params.userAgent ?? null, mailConnectionId: params.mailConnectionId ?? null, }, }); } /** * Schreibt einen Widerruf-Eintrag. * Öffnet KEINE neue Row für die ursprüngliche Einwilligung — der Widerruf ist * ein eigenständiger Eintrag (revokedAt gesetzt, consentAt = Zeitpunkt des Widerrufs). */ export async function writeConsentRevoke(params: { userId: string; consentType: string; consentVersion: string; revokedAt: Date; revokeReason: string; mailConnectionId?: string | null; ipAddress?: string | null; userAgent?: string | null; }) { const db = usePrisma(); return db.consentLog.create({ data: { userId: params.userId, consentType: params.consentType, consentVersion: params.consentVersion, // consentAt = Zeitpunkt des Widerruf-Events (Eintrag-Erstellung) consentAt: params.revokedAt, revokedAt: params.revokedAt, revokeReason: params.revokeReason, mailConnectionId: params.mailConnectionId ?? null, ipAddress: params.ipAddress ?? null, userAgent: params.userAgent ?? null, }, }); } /** Liest alle Consent-Log-Einträge eines Users — für Datenexport (Art. 15 DSGVO). */ export async function getConsentLogsByUser(userId: string) { const db = usePrisma(); return db.consentLog.findMany({ where: { userId }, orderBy: { consentAt: "desc" }, }); } /** * Setzt consent_at + consent_version + consent_ip_address auf der MailConnection. * Wird nach writeConsentGrant aufgerufen — beide Writes sind nötig: * - consent_logs: append-only Beweislog * - mail_connections.consent_at: Gateway-Check (Daemon + connect.post.ts) */ export async function setMailConnectionConsent(params: { connectionId: string; userId: string; consentAt: Date; consentVersion: string; consentIpAddress: string | null; }) { const db = usePrisma(); return db.mailConnection.updateMany({ where: { id: params.connectionId, userId: params.userId }, data: { consentAt: params.consentAt, consentVersion: params.consentVersion, consentIpAddress: params.consentIpAddress, }, }); } /** * Gibt die aktive (nicht-widerrufene) MailConnection zurück und prüft * ob Consent vorhanden ist. * Wird in connect.post.ts als Gateway-Check verwendet. */ export async function getMailConnectionWithConsent( connectionId: string, userId: string, ) { const db = usePrisma(); return db.mailConnection.findFirst({ where: { id: connectionId, userId }, select: { id: true, consentAt: true, consentVersion: true, }, }); }