Update house keeping tasks

This commit is contained in:
Luke Vella 2024-12-17 19:07:21 +00:00
parent d58ed5fddd
commit d73a2f53ef
No known key found for this signature in database
GPG key ID: 469CAD687F0D784C
6 changed files with 63 additions and 69 deletions

View file

@ -18,5 +18,4 @@ jobs:
run: |
curl -X "POST" --fail "https://app.rallly.co/api/house-keeping/remove-deleted-polls" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${API_SECRET}" \
-d '{"take": 1000}'
-H "Authorization: Bearer ${API_SECRET}"

View file

@ -2,7 +2,7 @@ import { prisma } from "@rallly/database";
import { headers } from "next/headers";
import { NextResponse } from "next/server";
export async function POST(req: Request, ctx: { params: { task: string } }) {
export async function POST(_req: Request, ctx: { params: { task: string } }) {
const headersList = headers();
const authorization = headersList.get("authorization");
@ -20,7 +20,7 @@ export async function POST(req: Request, ctx: { params: { task: string } }) {
return await deleteInactivePolls();
}
case "remove-deleted-polls": {
return await removeDeletedPolls(req);
return await removeDeletedPolls();
}
}
}
@ -70,82 +70,20 @@ async function deleteInactivePolls() {
/**
* Remove polls and corresponding data that have been marked deleted for more than 7 days.
*/
async function removeDeletedPolls(req: Request) {
const options = (await req.json()) as { take?: number } | undefined;
// First get the ids of all the polls that have been marked as deleted for at least 7 days
const deletedPolls = await prisma.poll.findMany({
select: {
id: true,
},
async function removeDeletedPolls() {
const { count: deletedPollCount } = await prisma.poll.deleteMany({
where: {
deleted: true,
deletedAt: {
lt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
},
},
take: options?.take ?? 1000,
});
const deletedPollIds = deletedPolls.map((poll) => poll.id);
const { count: deletedWatcherCount } = await prisma.watcher.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});
const { count: deletedVoteCount } = await prisma.vote.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});
const { count: deletedParticipantCount } =
await prisma.participant.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});
const { count: deletedOptionCount } = await prisma.option.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});
const { count: deletedCommentCount } = await prisma.comment.deleteMany({
where: {
pollId: {
in: deletedPollIds,
},
},
});
const { count: deletedPollCount } = await prisma.poll.deleteMany({
where: {
id: {
in: deletedPollIds,
},
},
});
return NextResponse.json({
success: true,
summary: {
deleted: {
votes: deletedVoteCount,
options: deletedOptionCount,
participants: deletedParticipantCount,
comments: deletedCommentCount,
watchers: deletedWatcherCount,
polls: deletedPollCount,
},
},

View file

@ -0,0 +1,14 @@
-- CreateIndex
CREATE INDEX "comments_poll_id_idx" ON "comments"("poll_id");
-- CreateIndex
CREATE INDEX "options_poll_id_idx" ON "options"("poll_id");
-- CreateIndex
CREATE INDEX "participants_poll_id_idx" ON "participants"("poll_id");
-- CreateIndex
CREATE INDEX "polls_deleted_touched_at_idx" ON "polls"("deleted", "touched_at");
-- CreateIndex
CREATE INDEX "votes_poll_id_idx" ON "votes"("poll_id");

View file

@ -0,0 +1,32 @@
-- DropIndex
DROP INDEX "comments_poll_id_idx";
-- DropIndex
DROP INDEX "options_poll_id_idx";
-- DropIndex
DROP INDEX "participants_poll_id_idx";
-- DropIndex
DROP INDEX "votes_poll_id_idx";
-- CreateIndex
CREATE INDEX "comments_poll_id_idx" ON "comments" USING HASH ("poll_id");
-- CreateIndex
CREATE INDEX "options_poll_id_idx" ON "options" USING HASH ("poll_id");
-- CreateIndex
CREATE INDEX "participants_poll_id_idx" ON "participants" USING HASH ("poll_id");
-- CreateIndex
CREATE INDEX "votes_poll_id_idx" ON "votes" USING HASH ("poll_id");
-- CreateIndex
CREATE INDEX "votes_participant_id_idx" ON "votes" USING HASH ("participant_id");
-- CreateIndex
CREATE INDEX "votes_option_id_idx" ON "votes" USING HASH ("option_id");
-- CreateIndex
CREATE INDEX "watchers_poll_id_idx" ON "watchers" USING HASH ("poll_id");

View file

@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX "polls_deleted_deleted_at_idx" ON "polls"("deleted", "deleted_at");

View file

@ -146,6 +146,8 @@ model Poll {
comments Comment[] @relation("PollToComments")
votes Vote[] @relation("PollToVotes")
@@index([deleted, touchedAt])
@@index([deleted, deletedAt])
@@map("polls")
}
@ -173,6 +175,7 @@ model Watcher {
poll Poll @relation("PollToWatchers", fields: [pollId], references: [id], onDelete: Cascade)
@@index([pollId], type: Hash)
@@map("watchers")
}
@ -181,7 +184,6 @@ model Participant {
name String
email String?
userId String? @map("user_id")
poll Poll @relation("PollToParticipants", fields: [pollId], references: [id], onDelete: Cascade)
pollId String @map("poll_id")
locale String?
createdAt DateTime @default(now()) @map("created_at")
@ -190,7 +192,9 @@ model Participant {
deletedAt DateTime? @map("deleted_at")
votes Vote[] @relation("ParticipantToVotes")
poll Poll @relation("PollToParticipants", fields: [pollId], references: [id], onDelete: Cascade)
@@index([pollId], type: Hash)
@@map("participants")
}
@ -202,6 +206,7 @@ model Option {
poll Poll @relation("PollToOptions", fields: [pollId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now()) @map("created_at")
@@index([pollId], type: Hash)
@@map("options")
}
@ -225,6 +230,9 @@ model Vote {
participant Participant @relation("ParticipantToVotes", fields: [participantId], references: [id], onDelete: Cascade)
poll Poll @relation("PollToVotes", fields: [pollId], references: [id], onDelete: Cascade)
@@index([pollId], type: Hash)
@@index([participantId], type: Hash)
@@index([optionId], type: Hash)
@@map("votes")
}
@ -240,6 +248,7 @@ model Comment {
poll Poll @relation("PollToComments", fields: [pollId], references: [id], onDelete: Cascade)
@@index([pollId], type: Hash)
@@map("comments")
}