Files
community-rule/lib/server/pruneExpiredRecords.ts
adilallo a725cf1a32 Spend login, invite, and email-change links only after a confirm POST so mail scanners cannot use them up.
GET still shows a one-button page, a used invitation can resend or reopen for the signed-in invitee, and an email-change GET no longer signs the account out.
2026-11-22 16:58: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, including
* after the original link was used. `acceptedAt` marks 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,
};
}