Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

✨ Auto-close polls with all dates in the past (#2488)

Luke Vella7236e8bdfd64
diff --git a/apps/web/src/app/api/house-keeping/[...method]/route.ts b/apps/web/src/app/api/house-keeping/[...method]/route.ts
index 907a432e1..c5ceb6dc7 100644
--- a/apps/web/src/app/api/house-keeping/[...method]/route.ts
+++ b/apps/web/src/app/api/house-keeping/[...method]/route.ts
@@ -75,6 +75,41 @@ app.get("/delete-inactive-polls", async (c) => {
});
});
+/**
+ * Closes polls whose options have all ended — i.e. no option ends in the
+ * future, where an option ends at start_time + duration (all-day options, with
+ * duration 0, are treated as ending 24h after their start). Closing is
+ * non-destructive: the poll becomes read-only but is preserved.
+ *
+ * Raw SQL because the option-end comparison (start_time + duration) can't be
+ * expressed in a Prisma `where`. It also deliberately does not touch
+ * `updated_at`, so closing a poll doesn't reset the inactivity clock that
+ * delete-inactive-polls keys off.
+ */
+app.get("/auto-close-polls", async (c) => {
+ const closed = await prisma.$executeRaw`
+ UPDATE polls p
+ SET status = 'closed'
+ WHERE p.status = 'open'
+ AND p.deleted = false
+ AND EXISTS (SELECT 1 FROM options o WHERE o.poll_id = p.id)
+ AND NOT EXISTS (
+ SELECT 1 FROM options o
+ WHERE o.poll_id = p.id
+ AND o.start_time + (CASE WHEN o.duration_minutes = 0
+ THEN interval '24 hours'
+ ELSE make_interval(mins => o.duration_minutes) END) > (now() AT TIME ZONE 'UTC')
+ )
+ `;
+
+ return c.json({
+ success: true,
+ summary: {
+ closed,
+ },
+ });
+});
+
/**
* Remove polls and corresponding data that have been marked deleted for more than 7 days.
*/
diff --git a/apps/web/vercel.json b/apps/web/vercel.json
index a98a4edee..2a4d71713 100644
--- a/apps/web/vercel.json
+++ b/apps/web/vercel.json
@@ -2,6 +2,10 @@
"$schema": "https://openapi.vercel.sh/vercel.json",
"buildCommand": "cd ../.. && pnpm build:web && pnpm db:deploy",
"crons": [
+ {
+ "path": "/api/house-keeping/auto-close-polls",
+ "schedule": "30 5 * * *"
+ },
{
"path": "/api/house-keeping/delete-inactive-polls",
"schedule": "0 6 * * *"