Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
✨ Add poll kind field to distinguish date vs time polls (#2372)
Luke Vella14af74e460ff
diff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.tsindex e71167aaa..4c2b301bf 100644--- a/apps/web/src/trpc/routers/polls.ts+++ b/apps/web/src/trpc/routers/polls.ts@@ -164,6 +164,19 @@ export const polls = router({const pollId = nanoid();const spaceId = activeSpace?.id;+ const optionsData = input.options.map((option) => ({+ startTime: input.timeZone+ ? dayjs(option.startDate).tz(input.timeZone, true).toDate()+ : dayjs(option.startDate).utc(true).toDate(),+ duration: option.endDate+ ? dayjs(option.endDate).diff(dayjs(option.startDate), "minute")+ : 0,+ }));++ const kind = optionsData.some((option) => option.duration > 0)+ ? "time"+ : "date";+const poll = await prisma.poll.create({include: {options: {@@ -181,19 +194,10 @@ export const polls = router({adminUrlId: adminToken,participantUrlId,userId: ctx.user.id,+ kind,options: {createMany: {- data: input.options.map((option) => ({- startTime: input.timeZone- ? dayjs(option.startDate).tz(input.timeZone, true).toDate()- : dayjs(option.startDate).utc(true).toDate(),- duration: option.endDate- ? dayjs(option.endDate).diff(- dayjs(option.startDate),- "minute",- )- : 0,- })),+ data: optionsData,},},hideParticipants: input.hideParticipants,@@ -369,23 +373,30 @@ export const polls = router({}),});}- });- await prisma.poll.update({- select: { id: true },- where: {- id: pollId,- },- data: {- title: input.title,- location: input.location,- description: input.description,- timeZone: input.timeZone,- hideScores: input.hideScores,- hideParticipants: input.hideParticipants,- disableComments: input.disableComments,- requireParticipantEmail: input.requireParticipantEmail,- },+ const maxDuration = await tx.option.aggregate({+ where: { pollId },+ _max: { duration: true },+ });+ const kind = (maxDuration._max.duration ?? 0) > 0 ? "time" : "date";++ await tx.poll.update({+ select: { id: true },+ where: {+ id: pollId,+ },+ data: {+ title: input.title,+ location: input.location,+ description: input.description,+ timeZone: input.timeZone,+ hideScores: input.hideScores,+ hideParticipants: input.hideParticipants,+ disableComments: input.disableComments,+ requireParticipantEmail: input.requireParticipantEmail,+ kind,+ },+ });});// Get updated poll data for group update@@ -1120,6 +1131,7 @@ export const polls = router({requireParticipantEmail: true,disableComments: true,spaceId: true,+ kind: true,options: {select: {startTime: true,@@ -1151,6 +1163,7 @@ export const polls = router({disableComments: poll.disableComments,adminUrlId: nanoid(),participantUrlId: nanoid(),+ kind: poll.kind,options: {create: poll.options,},diff --git a/apps/web/tests/test-utils.ts b/apps/web/tests/test-utils.tsindex 18fb2af72..258e34171 100644--- a/apps/web/tests/test-utils.ts+++ b/apps/web/tests/test-utils.ts@@ -96,6 +96,7 @@ export async function createTestPoll({},}),...(hasFutureOptions && {+ kind: "time" as const,options: {create: {startTime: dayjs().add(10, "day").toDate(),diff --git a/packages/api-core/src/polls/mutations.ts b/packages/api-core/src/polls/mutations.tsindex 57f1d22e3..56a0a7ec5 100644--- a/packages/api-core/src/polls/mutations.ts+++ b/packages/api-core/src/polls/mutations.ts@@ -33,6 +33,8 @@ export const createPoll = async ({options,spaceId,}: CreatePollParams) => {+ const kind = options.some((o) => o.duration > 0) ? "time" : "date";+const poll = await prisma.poll.create({data: {id: nanoid(),@@ -48,6 +50,7 @@ export const createPoll = async ({participantUrlId: nanoid(),userId,spaceId,+ kind,options: { createMany: { data: options } },},select: {diff --git a/packages/database/prisma/migrations/20260509200506_add_poll_kind/migration.sql b/packages/database/prisma/migrations/20260509200506_add_poll_kind/migration.sqlnew file mode 100644index 000000000..ba883dac6--- /dev/null+++ b/packages/database/prisma/migrations/20260509200506_add_poll_kind/migration.sql@@ -0,0 +1,12 @@+-- CreateEnum+CREATE TYPE "poll_kind" AS ENUM ('date', 'time');++-- AlterTable+ALTER TABLE "polls" ADD COLUMN "kind" "poll_kind" NOT NULL DEFAULT 'date';++-- Backfill: polls with any option duration > 0 are time-based+UPDATE "polls"+SET "kind" = 'time'+WHERE "id" IN (+ SELECT DISTINCT "poll_id" FROM "options" WHERE "duration_minutes" > 0+);diff --git a/packages/database/prisma/models/poll.prisma b/packages/database/prisma/models/poll.prismaindex d3f685bbe..e0b7fc855 100644--- a/packages/database/prisma/models/poll.prisma+++ b/packages/database/prisma/models/poll.prisma@@ -15,6 +15,13 @@ enum PollStatus {@@map("poll_status")}+enum PollKind {+ date+ time++ @@map("poll_kind")+}+model Poll {id String @id @unique @map("id")createdAt DateTime @default(now()) @map("created_at")@@ -27,6 +34,7 @@ model Poll {guestId String? @map("guest_id") // @deprecated - legacy NextAuth field, no longer usedtimeZone String? @map("time_zone")status PollStatus @default(open)+ kind PollKind @default(date)deleted Boolean @default(false)deletedAt DateTime? @map("deleted_at")participantUrlId String @unique @map("participant_url_id")diff --git a/packages/database/prisma/seed.ts b/packages/database/prisma/seed.tsindex 50e3a25d6..793f04204 100644--- a/packages/database/prisma/seed.ts+++ b/packages/database/prisma/seed.ts@@ -84,6 +84,8 @@ async function main() {const adminUrlId = nextId();const participantUrlId = nextId();+ const kind = poll.options.some((opt) => opt.duration > 0) ? "time" : "date";+await prisma.poll.create({data: {id: pollId,@@ -102,6 +104,7 @@ async function main() {hideScores: poll.hideScores,disableComments: poll.disableComments,requireParticipantEmail: poll.requireParticipantEmail,+ kind,},});