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.
This commit is contained in:
adilallo committed 2026-11-10 15:43:30 -07:00
1 parent b4d0d085d3
commit a8b23bddc2
6 files changed
+15 -48

No files matched your search

+3 -2
View File
@@ -294,8 +294,9 @@ Periods the privacy policy states. Source of truth:
`node /app/prune-expired.bundle.cjs` (the production bundle of `node /app/prune-expired.bundle.cjs` (the production bundle of
[`scripts/prune-sessions.ts`](../../scripts/prune-sessions.ts)) and [`scripts/prune-sessions.ts`](../../scripts/prune-sessions.ts)) and
deletes expired sessions, unused login / save-progress / email-change deletes expired sessions, unused login / save-progress / email-change
tokens and the drafts they carry, unused invitation tokens, and workshop tokens and the drafts they carry, and workshop inquiries older than 12
inquiries older than 12 months. months. It leaves invitation token hashes in place: an expired link cannot
be used to join, and that hash is how the person requests a new link.
Manual runs: Manual runs:
+1 -1
View File
@@ -10,7 +10,7 @@ read this text. Do not switch the route on from this file.
| --- | --- | --- | | --- | --- | --- |
| Login and save-progress links | Work for **60 minutes**. Unused rows, and any draft they carry, are deleted once expired. A daily job runs the delete, so they are gone **within 24 hours** of expiry. | `MAGIC_LINK_TTL_MS`, Cloudron scheduler `prune-expired` | | Login and save-progress links | Work for **60 minutes**. Unused rows, and any draft they carry, are deleted once expired. A daily job runs the delete, so they are gone **within 24 hours** of expiry. | `MAGIC_LINK_TTL_MS`, Cloudron scheduler `prune-expired` |
| Email-change links | Same as login links: **60 minutes**, then gone within **24 hours**. | `EMAIL_CHANGE_TTL_MS` | | Email-change links | Same as login links: **60 minutes**, then gone within **24 hours**. | `EMAIL_CHANGE_TTL_MS` |
| Unused invitation tokens | The unused token is cleared once the invite expires. Gone within **24 hours** of expiry. The invitation row stays so it can be sent again. | Daily prune | | Unused invitation tokens | The link stops granting access when it expires (**60 minutes**). The hash stays on the invitation so the person can request a new link from the expired page, and so the organizer can send it again. The daily job does not delete it. | `STAKEHOLDER_INVITE_TTL_MS` |
| Workshop inquiries that do not lead to a workshop | **12 months**, then deleted. | `WORKSHOP_INQUIRY_RETENTION_MS` | | Workshop inquiries that do not lead to a workshop | **12 months**, then deleted. | `WORKSHOP_INQUIRY_RETENTION_MS` |
| Ask an organizer messages | **Not stored** on CommunityRule. They are emailed to MEDLab. MEDLab deletes mailbox copies after **12 months**. | Mail only | | Ask an organizer messages | **Not stored** on CommunityRule. They are emailed to MEDLab. MEDLab deletes mailbox copies after **12 months**. | Mail only |
| Unused accounts | **Kept until the person deletes the account.** There is no unused-account timer at launch. | — | | Unused accounts | **Kept until the person deletes the account.** There is no unused-account timer at launch. | — |
+6 -23
View File
@@ -16,15 +16,6 @@ export interface PruneStore {
where: { expiresAt: { lt: Date } }; where: { expiresAt: { lt: Date } };
}) => Promise<{ count: number }>; }) => Promise<{ count: number }>;
}; };
ruleStakeholder: {
updateMany: (args: {
where: {
inviteTokenHash: { not: null };
inviteExpiresAt: { lt: Date };
};
data: { inviteTokenHash: null; inviteExpiresAt: null };
}) => Promise<{ count: number }>;
};
workshopInquiry: { workshopInquiry: {
deleteMany: (args: { deleteMany: (args: {
where: { createdAt: { lt: Date } }; where: { createdAt: { lt: Date } };
@@ -36,7 +27,6 @@ export interface PruneExpiredResult {
sessions: number; sessions: number;
magicLinkTokens: number; magicLinkTokens: number;
emailChangeTokens: number; emailChangeTokens: number;
unusedInviteTokens: number;
workshopInquiries: number; workshopInquiries: number;
} }
@@ -53,8 +43,12 @@ export async function deleteExpiredWorkshopInquiries(
/** /**
* Delete expired sessions, login/save-progress tokens (and the draft they * Delete expired sessions, login/save-progress tokens (and the draft they
* carry), email-change tokens, unused invitation tokens, and old workshop * carry), email-change tokens, and old workshop inquiries.
* inquiries. Does not delete stakeholder rows or accounts. *
* 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( export async function pruneExpiredRecords(
db: PruneStore, db: PruneStore,
@@ -69,23 +63,12 @@ export async function pruneExpiredRecords(
const emailChangeTokens = await db.emailChangeToken.deleteMany({ const emailChangeTokens = await db.emailChangeToken.deleteMany({
where: { expiresAt: { lt: now } }, 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); const workshopInquiries = await deleteExpiredWorkshopInquiries(db, now);
return { return {
sessions: sessions.count, sessions: sessions.count,
magicLinkTokens: magicLinkTokens.count, magicLinkTokens: magicLinkTokens.count,
emailChangeTokens: emailChangeTokens.count, emailChangeTokens: emailChangeTokens.count,
unusedInviteTokens: unusedInviteTokens.count,
workshopInquiries, workshopInquiries,
}; };
} }
+2 -2
View File
@@ -1,7 +1,7 @@
/** /**
* Prune expired sessions, unused login/save-progress/email-change tokens * Prune expired sessions, unused login/save-progress/email-change tokens
* (and any draft they carry), unused invitation tokens, and old workshop * (and any draft they carry), and old workshop inquiries. Invitation token
* inquiries. * hashes are left in place so an expired link can still request a new one.
* *
* Usage (from repo root): * 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 # global sweep
-2
View File
@@ -44,7 +44,6 @@ beforeEach(() => {
sessions: 1, sessions: 1,
magicLinkTokens: 2, magicLinkTokens: 2,
emailChangeTokens: 0, emailChangeTokens: 0,
unusedInviteTokens: 3,
workshopInquiries: 4, workshopInquiries: 4,
}); });
retryPendingWorkshopInquiryMailMock.mockResolvedValue(1); retryPendingWorkshopInquiryMailMock.mockResolvedValue(1);
@@ -79,7 +78,6 @@ describe("POST /api/ops/prune", () => {
sessions: 1, sessions: 1,
magicLinkTokens: 2, magicLinkTokens: 2,
emailChangeTokens: 0, emailChangeTokens: 0,
unusedInviteTokens: 3,
workshopInquiries: 4, workshopInquiries: 4,
}); });
expect(pruneExpiredRecordsMock).toHaveBeenCalledWith( expect(pruneExpiredRecordsMock).toHaveBeenCalledWith(
+3 -18
View File
@@ -6,7 +6,6 @@ function store(counts: {
sessions?: number; sessions?: number;
magicLinkTokens?: number; magicLinkTokens?: number;
emailChangeTokens?: number; emailChangeTokens?: number;
unusedInviteTokens?: number;
workshopInquiries?: number; workshopInquiries?: number;
}) { }) {
const sessionDeleteMany = vi.fn().mockResolvedValue({ const sessionDeleteMany = vi.fn().mockResolvedValue({
@@ -18,9 +17,7 @@ function store(counts: {
const emailChangeDeleteMany = vi.fn().mockResolvedValue({ const emailChangeDeleteMany = vi.fn().mockResolvedValue({
count: counts.emailChangeTokens ?? 0, count: counts.emailChangeTokens ?? 0,
}); });
const inviteUpdateMany = vi.fn().mockResolvedValue({ const inviteUpdateMany = vi.fn().mockResolvedValue({ count: 0 });
count: counts.unusedInviteTokens ?? 0,
});
const workshopDeleteMany = vi.fn().mockResolvedValue({ const workshopDeleteMany = vi.fn().mockResolvedValue({
count: counts.workshopInquiries ?? 0, count: counts.workshopInquiries ?? 0,
}); });
@@ -41,13 +38,12 @@ function store(counts: {
} }
describe("pruneExpiredRecords", () => { describe("pruneExpiredRecords", () => {
it("deletes expired tokens and sessions, and clears unused invite hashes", async () => { it("deletes expired tokens and sessions without clearing invite hashes", async () => {
const now = new Date("2026-11-10T12:00:00.000Z"); const now = new Date("2026-11-10T12:00:00.000Z");
const mocks = store({ const mocks = store({
sessions: 2, sessions: 2,
magicLinkTokens: 3, magicLinkTokens: 3,
emailChangeTokens: 1, emailChangeTokens: 1,
unusedInviteTokens: 4,
workshopInquiries: 5, workshopInquiries: 5,
}); });
@@ -57,7 +53,6 @@ describe("pruneExpiredRecords", () => {
sessions: 2, sessions: 2,
magicLinkTokens: 3, magicLinkTokens: 3,
emailChangeTokens: 1, emailChangeTokens: 1,
unusedInviteTokens: 4,
workshopInquiries: 5, workshopInquiries: 5,
}); });
@@ -70,16 +65,7 @@ describe("pruneExpiredRecords", () => {
expect(mocks.emailChangeDeleteMany).toHaveBeenCalledWith({ expect(mocks.emailChangeDeleteMany).toHaveBeenCalledWith({
where: { expiresAt: { lt: now } }, where: { expiresAt: { lt: now } },
}); });
expect(mocks.inviteUpdateMany).toHaveBeenCalledWith({ expect(mocks.inviteUpdateMany).not.toHaveBeenCalled();
where: {
inviteTokenHash: { not: null },
inviteExpiresAt: { lt: now },
},
data: {
inviteTokenHash: null,
inviteExpiresAt: null,
},
});
const workshopArg = mocks.workshopDeleteMany.mock.calls[0]?.[0] as { const workshopArg = mocks.workshopDeleteMany.mock.calls[0]?.[0] as {
where: { createdAt: { lt: Date } }; where: { createdAt: { lt: Date } };
@@ -96,7 +82,6 @@ describe("pruneExpiredRecords", () => {
mocks.sessionDeleteMany.mock.calls, mocks.sessionDeleteMany.mock.calls,
mocks.magicLinkDeleteMany.mock.calls, mocks.magicLinkDeleteMany.mock.calls,
mocks.emailChangeDeleteMany.mock.calls, mocks.emailChangeDeleteMany.mock.calls,
mocks.inviteUpdateMany.mock.calls,
]); ]);
expect(payload).not.toMatch(/tokenHash|email/); expect(payload).not.toMatch(/tokenHash|email/);
}); });