/** * Prune expired sessions, unused login/save-progress/email-change tokens * (and any draft they carry), and old workshop inquiries. Invitation token * hashes are left in place so an expired link can still request a new one. * * Usage (from repo root): * node --env-file=.env --import tsx scripts/prune-sessions.ts # global sweep * node --env-file=.env --import tsx scripts/prune-sessions.ts # sessions for one user * * Intentionally does NOT import `lib/server/session` — that module pulls in * `next/headers` which requires a Next request context. For ad-hoc DB * surgery we talk to Prisma directly. */ import { PrismaClient } from "@prisma/client"; import { pruneExpiredRecords } from "../lib/server/pruneExpiredRecords"; const prisma = new PrismaClient(); async function main() { const userId = process.argv[2]; const now = new Date(); if (userId) { const before = await prisma.session.count({ where: { userId } }); const { count } = await prisma.session.deleteMany({ where: { expiresAt: { lt: now }, userId, }, }); const after = await prisma.session.count({ where: { userId } }); console.log( JSON.stringify( { scope: userId, now: now.toISOString(), sessionsBefore: before, pruned: count, sessionsAfter: after, }, null, 2, ), ); return; } const result = await pruneExpiredRecords(prisma, now); console.log( JSON.stringify( { scope: "global", now: now.toISOString(), ...result, }, null, 2, ), ); } main() .catch((err) => { console.error(err); process.exitCode = 1; }) .finally(() => prisma.$disconnect());