import { WORKSHOP_INQUIRY_RETENTION_MS } from "../privacyRetention"; export interface PruneStore { session: { deleteMany: (args: { where: { expiresAt: { lt: Date }; userId?: string }; }) => Promise<{ count: number }>; }; magicLinkToken: { deleteMany: (args: { where: { expiresAt: { lt: Date } }; }) => Promise<{ count: number }>; }; emailChangeToken: { deleteMany: (args: { where: { expiresAt: { lt: Date } }; }) => Promise<{ count: number }>; }; workshopInquiry: { deleteMany: (args: { where: { createdAt: { lt: Date } }; }) => Promise<{ count: number }>; }; } export interface PruneExpiredResult { sessions: number; magicLinkTokens: number; emailChangeTokens: number; workshopInquiries: number; } export async function deleteExpiredWorkshopInquiries( db: Pick, now = new Date(), ): Promise { const cutoff = new Date(now.getTime() - WORKSHOP_INQUIRY_RETENTION_MS); const result = await db.workshopInquiry.deleteMany({ where: { createdAt: { lt: cutoff } }, }); return result.count; } /** * Delete expired sessions, login/save-progress tokens (and the draft they * carry), email-change tokens, and old workshop inquiries. * * Does not delete stakeholder rows, accounts, or invitation token hashes. * Verify already rejects an invite once `inviteExpiresAt` has passed. The * hash is how `/invite/expired` finds that row to send a new link, and a * null hash is treated as an accepted invite. */ export async function pruneExpiredRecords( db: PruneStore, now = new Date(), ): Promise { const sessions = await db.session.deleteMany({ where: { expiresAt: { lt: now } }, }); const magicLinkTokens = await db.magicLinkToken.deleteMany({ where: { expiresAt: { lt: now } }, }); const emailChangeTokens = await db.emailChangeToken.deleteMany({ where: { expiresAt: { lt: now } }, }); const workshopInquiries = await deleteExpiredWorkshopInquiries(db, now); return { sessions: sessions.count, magicLinkTokens: magicLinkTokens.count, emailChangeTokens: emailChangeTokens.count, workshopInquiries, }; }