Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Keep polls for 30 days after their final date (#2720)
Luke Vellaf2e9294ae78e
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex 62bf8bd27..87fc6c00a 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -372,8 +372,6 @@"licenseKeyGenericError": "An error occurred while validating the license key","licenseLimitWarning": "You have exceeded the limits of your license. Please <a>upgrade</a>.","licenseType": "License Type",- "limitedPollLifetime": "Limited Poll Lifetime",- "limitedPollLifetimeDescription": "Inactive polls are automatically deleted","loading": "Loading...","location": "Location","locationCustomLink": "Custom link",@@ -695,6 +693,8 @@"themeDescription": "Choose your preferred appearance","themeLight": "Light","themeSystem": "System",+ "thirtyDayPollRetention": "30 Day Poll Retention",+ "thirtyDayPollRetentionDescription": "Polls are kept for 30 days after their final date","timeFormat": "Time format","timePickerSelectTime": "Select time","timeZone": "Time zone",diff --git a/apps/web/src/features/billing/components/pay-wall-dialog.tsx b/apps/web/src/features/billing/components/pay-wall-dialog.tsxindex fe7b3a1bb..838cc6d5c 100644--- a/apps/web/src/features/billing/components/pay-wall-dialog.tsx+++ b/apps/web/src/features/billing/components/pay-wall-dialog.tsx@@ -279,14 +279,14 @@ export function PayWallDialog({icon={<TimerResetIcon />}title={<Trans- i18nKey="limitedPollLifetime"- defaults="Limited Poll Lifetime"+ i18nKey="thirtyDayPollRetention"+ defaults="30 Day Poll Retention"/>}description={<Trans- i18nKey="limitedPollLifetimeDescription"- defaults="Inactive polls are automatically deleted"+ i18nKey="thirtyDayPollRetentionDescription"+ defaults="Polls are kept for 30 days after their final date"/>}/>diff --git a/apps/web/src/features/poll/mutations.test.ts b/apps/web/src/features/poll/mutations.test.tsnew file mode 100644index 000000000..d521d35d7--- /dev/null+++ b/apps/web/src/features/poll/mutations.test.ts@@ -0,0 +1,144 @@+import { beforeEach, describe, expect, it, vi } from "vitest";+import { deleteInactivePolls } from "./mutations";++const { mockUpdateMany } = vi.hoisted(() => ({ mockUpdateMany: vi.fn() }));++vi.mock("@rallly/database", () => ({+ prisma: {+ poll: {+ updateMany: mockUpdateMany,+ },+ },+}));++const NOW = new Date("2026-07-21T12:00:00Z");++const daysAgo = (days: number) =>+ new Date(NOW.getTime() - days * 24 * 60 * 60 * 1000);++type DeleteWhere = {+ deleted: boolean;+ options: { none: { startTime: { gt: Date } } };+ OR: [{ spaceId: null }, { space: { tier: { not: string } } }];+ updatedAt: { lt: Date };+ participants: { none: { updatedAt: { gte: Date } } };+ comments: { none: { createdAt: { gte: Date } } };+};++async function capturedWhere() {+ await deleteInactivePolls();+ const args = mockUpdateMany.mock.calls[0][0] as { where: DeleteWhere };+ return args.where;+}++/**+ * Evaluates the query's where clause against a poll the way Prisma would,+ * so the tests exercise the retention rule rather than the query's shape.+ */+function wouldDelete(+ where: DeleteWhere,+ poll: {+ optionStartTimes: Date[];+ spaceTier: "hobby" | "pro" | null;+ updatedAt: Date;+ participantUpdatedAts?: Date[];+ commentCreatedAts?: Date[];+ },+) {+ if (poll.optionStartTimes.some((t) => t > where.options.none.startTime.gt)) {+ return false;+ }+ if (poll.spaceTier === where.OR[1].space.tier.not) {+ return false;+ }+ if (!(poll.updatedAt < where.updatedAt.lt)) {+ return false;+ }+ if (+ (poll.participantUpdatedAts ?? []).some(+ (t) => t >= where.participants.none.updatedAt.gte,+ )+ ) {+ return false;+ }+ if (+ (poll.commentCreatedAts ?? []).some(+ (t) => t >= where.comments.none.createdAt.gte,+ )+ ) {+ return false;+ }+ return true;+}++describe("deleteInactivePolls", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ vi.useFakeTimers();+ vi.setSystemTime(NOW);+ mockUpdateMany.mockResolvedValue({ count: 0 });+ return () => vi.useRealTimers();+ });++ it("keeps a poll whose dates passed less than 30 days ago even with no activity for 30+ days", async () => {+ const where = await capturedWhere();+ expect(+ wouldDelete(where, {+ optionStartTimes: [daysAgo(45), daysAgo(15)],+ spaceTier: "hobby",+ updatedAt: daysAgo(60),+ }),+ ).toBe(false);+ });++ it("deletes a poll whose dates passed more than 30 days ago with no activity for 30+ days on a non pro space", async () => {+ const where = await capturedWhere();+ expect(+ wouldDelete(where, {+ optionStartTimes: [daysAgo(45), daysAgo(31)],+ spaceTier: "hobby",+ updatedAt: daysAgo(31),+ }),+ ).toBe(true);+ });++ it("keeps a poll with recent participant or comment activity even if all dates passed more than 30 days ago", async () => {+ const where = await capturedWhere();+ expect(+ wouldDelete(where, {+ optionStartTimes: [daysAgo(45)],+ spaceTier: "hobby",+ updatedAt: daysAgo(60),+ participantUpdatedAts: [daysAgo(5)],+ }),+ ).toBe(false);+ expect(+ wouldDelete(where, {+ optionStartTimes: [daysAgo(45)],+ spaceTier: "hobby",+ updatedAt: daysAgo(60),+ commentCreatedAts: [daysAgo(5)],+ }),+ ).toBe(false);+ });++ it("keeps polls on pro spaces", async () => {+ const where = await capturedWhere();+ expect(+ wouldDelete(where, {+ optionStartTimes: [daysAgo(45)],+ spaceTier: "pro",+ updatedAt: daysAgo(60),+ }),+ ).toBe(false);+ });++ it("marks matching polls as deleted with a deletion timestamp", async () => {+ await deleteInactivePolls();+ expect(mockUpdateMany).toHaveBeenCalledWith(+ expect.objectContaining({+ data: { deleted: true, deletedAt: NOW },+ }),+ );+ });+});diff --git a/apps/web/src/features/poll/mutations.ts b/apps/web/src/features/poll/mutations.tsindex f99981141..899757b31 100644--- a/apps/web/src/features/poll/mutations.ts+++ b/apps/web/src/features/poll/mutations.ts@@ -136,10 +136,13 @@ export async function adoptOrphanedPolls({}/**- * Marks inactive polls as deleted. Polls are inactive if they have not been- * updated in the last 30 days and all dates are in the past.- * Only marks polls as deleted if they belong to users without an active subscription- * or if they don't have a user associated with them.+ * Marks inactive polls as deleted. A poll is inactive when every date has+ * passed at least 30 days ago and there has been no activity (poll edits,+ * participant responses, new comments) in the last 30 days. This guarantees+ * polls are kept for at least 30 days after their final date, and activity+ * extends that.+ * Only marks polls as deleted if they belong to spaces without an active+ * subscription or if they don't have a space associated with them.*/export async function deleteInactivePolls() {// Define the 30-day threshold once@@ -149,10 +152,10 @@ export async function deleteInactivePolls() {const { count: markedDeleted } = await prisma.poll.updateMany({where: {deleted: false,- // All poll dates are in the past+ // All poll dates passed at least 30 days agooptions: {none: {- startTime: { gt: new Date() },+ startTime: { gt: thirtyDaysAgo },},},// We don't delete polls that belong to a space with an active subscription