Expired sign-in, save-progress, email-change, and invitation tokens (and any draft they carry) are cleared by a Cloudron scheduler task. Page timing logs record the path without the query string, and mail failures log a request id instead of an address.
92 lines
2.5 KiB
TypeScript
92 lines
2.5 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 }>;
|
|
};
|
|
ruleStakeholder: {
|
|
updateMany: (args: {
|
|
where: {
|
|
inviteTokenHash: { not: null };
|
|
inviteExpiresAt: { lt: Date };
|
|
};
|
|
data: { inviteTokenHash: null; inviteExpiresAt: null };
|
|
}) => Promise<{ count: number }>;
|
|
};
|
|
workshopInquiry: {
|
|
deleteMany: (args: {
|
|
where: { createdAt: { lt: Date } };
|
|
}) => Promise<{ count: number }>;
|
|
};
|
|
}
|
|
|
|
export interface PruneExpiredResult {
|
|
sessions: number;
|
|
magicLinkTokens: number;
|
|
emailChangeTokens: number;
|
|
unusedInviteTokens: 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, unused invitation tokens, and old workshop
|
|
* inquiries. Does not delete stakeholder rows or accounts.
|
|
*/
|
|
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 unusedInviteTokens = await db.ruleStakeholder.updateMany({
|
|
where: {
|
|
inviteTokenHash: { not: null },
|
|
inviteExpiresAt: { lt: now },
|
|
},
|
|
data: {
|
|
inviteTokenHash: null,
|
|
inviteExpiresAt: null,
|
|
},
|
|
});
|
|
const workshopInquiries = await deleteExpiredWorkshopInquiries(db, now);
|
|
|
|
return {
|
|
sessions: sessions.count,
|
|
magicLinkTokens: magicLinkTokens.count,
|
|
emailChangeTokens: emailChangeTokens.count,
|
|
unusedInviteTokens: unusedInviteTokens.count,
|
|
workshopInquiries,
|
|
};
|
|
}
|