Files
community-rule/lib/server/pruneExpiredRecords.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

75 lines
2.2 KiB
TypeScript

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<PruneStore, "workshopInquiry">,
now = new Date(),
): Promise<number> {
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<PruneExpiredResult> {
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,
};
}