♻️ Update how we store poll status (#957)

This commit is contained in:
Luke Vella 2023-12-05 14:43:48 +07:00 committed by GitHub
parent 7670db6778
commit 04211ac168
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 108 additions and 186 deletions

View file

@ -87,7 +87,6 @@ export const polls = router({
timeZone: input.timeZone,
location: input.location,
description: input.description,
demo: input.demo,
adminUrlId: adminToken,
participantUrlId,
userId: ctx.user.id,
@ -355,12 +354,11 @@ export const polls = router({
adminUrlId: true,
participantUrlId: true,
closed: true,
legacy: true,
status: true,
hideParticipants: true,
disableComments: true,
hideScores: true,
requireParticipantEmail: true,
demo: true,
options: {
select: {
id: true,
@ -436,6 +434,7 @@ export const polls = router({
timeZone: true,
adminUrlId: true,
participantUrlId: true,
status: true,
event: {
select: {
start: true,
@ -551,14 +550,20 @@ export const polls = router({
eventStart = eventStart.tz(poll.timeZone, true);
}
await prisma.event.create({
await prisma.poll.update({
where: {
id: input.pollId,
},
data: {
pollId: poll.id,
optionId: input.optionId,
start: eventStart.toDate(),
duration: option.duration,
title: poll.title,
userId: ctx.user.id,
event: {
create: {
optionId: input.optionId,
start: eventStart.toDate(),
duration: option.duration,
title: poll.title,
userId: ctx.user.id,
},
},
},
});
@ -721,18 +726,16 @@ export const polls = router({
)
.mutation(async ({ input }) => {
await prisma.$transaction([
prisma.event.delete({
where: {
pollId: input.pollId,
},
}),
prisma.poll.update({
where: {
id: input.pollId,
},
data: {
eventId: null,
closed: false,
event: {
delete: true,
},
status: "live",
closed: false, // @deprecated
},
}),
]);
@ -749,7 +752,8 @@ export const polls = router({
id: input.pollId,
},
data: {
closed: true,
closed: true, // TODO (Luke Vella) [2023-12-05]: Remove this
status: "paused",
},
});
}),
@ -827,6 +831,7 @@ export const polls = router({
},
data: {
closed: false,
status: "live",
},
});
}),

View file

@ -0,0 +1,50 @@
/*
Warnings:
- You are about to drop the column `demo` on the `polls` table. All the data in the column will be lost.
- You are about to drop the column `legacy` on the `polls` table. All the data in the column will be lost.
- A unique constraint covering the columns `[event_id]` on the table `polls` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateEnum
CREATE TYPE "poll_status" AS ENUM ('live', 'paused', 'finalized');
-- AlterTable
ALTER TABLE "polls" DROP COLUMN "demo",
DROP COLUMN "legacy",
ADD COLUMN "status" "poll_status";
-- CreateIndex
CREATE UNIQUE INDEX "polls_event_id_key" ON "polls"("event_id");
-- Fix an issue where the "event_id" column was not being set
UPDATE "polls"
SET "event_id" = "events"."id"
FROM "events"
WHERE "events"."poll_id" = "polls"."id";
-- Set the "status" column to corressponding enum value
-- If "closed" is true, set to "paused"
-- If a poll has an "event_id", set to "finalized"
-- If a poll has a "deletedAt" date, set to "deleted"
-- Otherwise set to "live"
UPDATE "polls"
SET "status" = CASE
WHEN "closed" = true THEN 'paused'::poll_status
WHEN "event_id" IS NOT NULL THEN 'finalized'::poll_status
ELSE 'live'::poll_status
END;
-- Make the "status" column non-nullable and default to "live"
ALTER TABLE "polls"
ALTER COLUMN "status" SET NOT NULL,
ALTER COLUMN "status" SET DEFAULT 'live';
DROP INDEX "events_poll_id_idx";
-- DropIndex
DROP INDEX "events_poll_id_key";
-- AlterTable
ALTER TABLE "events" DROP COLUMN "poll_id";

View file

@ -117,6 +117,14 @@ enum ParticipantVisibility {
@@map("participant_visibility")
}
enum PollStatus {
live
paused
finalized
@@map("poll_status")
}
model Poll {
id String @id @unique @map("id")
createdAt DateTime @default(now()) @map("created_at")
@ -125,46 +133,45 @@ model Poll {
title String
description String?
location String?
user User? @relation(fields: [userId], references: [id])
userId String @map("user_id")
votes Vote[]
timeZone String? @map("time_zone")
options Option[]
participants Participant[]
watchers Watcher[]
demo Boolean @default(false)
comments Comment[]
legacy Boolean @default(false) // @deprecated
closed Boolean @default(false) // we use this to indicate whether a poll is paused
closed Boolean @default(false) // @deprecated
status PollStatus @default(live)
deleted Boolean @default(false)
deletedAt DateTime? @map("deleted_at")
touchedAt DateTime @default(now()) @map("touched_at")
participantUrlId String @unique @map("participant_url_id")
adminUrlId String @unique @map("admin_url_id")
eventId String? @map("event_id")
event Event?
eventId String? @unique @map("event_id")
hideParticipants Boolean @default(false) @map("hide_participants")
hideScores Boolean @default(false) @map("hide_scores")
disableComments Boolean @default(false) @map("disable_comments")
requireParticipantEmail Boolean @default(false) @map("require_participant_email")
user User? @relation(fields: [userId], references: [id])
event Event? @relation(fields: [eventId], references: [id])
options Option[]
participants Participant[]
watchers Watcher[]
comments Comment[]
votes Vote[]
@@index([userId], type: Hash)
@@map("polls")
}
model Event {
id String @id @default(cuid())
pollId String @unique @map("poll_id")
userId String @map("user_id")
user User @relation(fields: [userId], references: [id])
poll Poll @relation(fields: [pollId], references: [id])
optionId String @map("option_id")
title String
start DateTime @db.Timestamp(0)
duration Int @default(0) @map("duration_minutes")
createdAt DateTime @default(now()) @map("created_at")
@@index([pollId], type: Hash)
Poll Poll?
@@index([userId], type: Hash)
@@map("events")
}