♻️ Refactor poll view tracking (#1644)

This commit is contained in:
Luke Vella 2025-03-28 10:10:46 +00:00 committed by GitHub
parent 6b914610d9
commit f05f437b56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 288 additions and 41 deletions

View file

@ -0,0 +1,26 @@
-- CreateTable
CREATE TABLE "poll_views" (
"id" TEXT NOT NULL,
"poll_id" TEXT NOT NULL,
"ip_address" TEXT,
"user_id" TEXT,
"user_agent" TEXT,
"viewed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "poll_views_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "poll_views_poll_id_idx" ON "poll_views" USING HASH ("poll_id");
-- CreateIndex
CREATE INDEX "poll_views_user_id_idx" ON "poll_views" USING HASH ("user_id");
-- CreateIndex
CREATE INDEX "poll_views_viewed_at_idx" ON "poll_views"("viewed_at");
-- AddForeignKey
ALTER TABLE "poll_views" ADD CONSTRAINT "poll_views_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "poll_views" ADD CONSTRAINT "poll_views_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View file

@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"

View file

@ -62,6 +62,7 @@ model User {
participants Participant[]
paymentMethods PaymentMethod[]
subscription Subscription? @relation("UserToSubscription")
pollViews PollView[]
@@map("users")
}
@ -167,6 +168,7 @@ model Poll {
watchers Watcher[]
comments Comment[]
votes Vote[]
views PollView[]
@@index([guestId])
@@map("polls")
@ -284,6 +286,23 @@ model Comment {
@@map("comments")
}
model PollView {
id String @id @default(cuid())
pollId String @map("poll_id")
ipAddress String? @map("ip_address")
userId String? @map("user_id")
userAgent String? @map("user_agent")
viewedAt DateTime @default(now()) @map("viewed_at")
poll Poll @relation(fields: [pollId], references: [id], onDelete: Cascade)
user User? @relation(fields: [userId], references: [id], onDelete: SetNull)
@@index([pollId], type: Hash)
@@index([userId], type: Hash)
@@index([viewedAt])
@@map("poll_views")
}
model VerificationToken {
identifier String @db.Citext
token String @unique