Files
community-rule/scripts/prune-sessions.ts
T
adilallo a8b23bddc2 Keep expired invitation hashes so a request for a new link still sends mail.
Clearing the hash made the expired page report success without sending, and made the invite look accepted.
2026-11-10 15:43:30 -07:00

68 lines
1.7 KiB
TypeScript

/**
* 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 <userId> # 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());