mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-07 05:01:49 +02:00
Update house keeping tasks
This commit is contained in:
parent
d58ed5fddd
commit
d73a2f53ef
6 changed files with 63 additions and 69 deletions
3
.github/workflows/house-keeping.yml
vendored
3
.github/workflows/house-keeping.yml
vendored
|
@ -18,5 +18,4 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
curl -X "POST" --fail "https://app.rallly.co/api/house-keeping/remove-deleted-polls" \
|
curl -X "POST" --fail "https://app.rallly.co/api/house-keeping/remove-deleted-polls" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-H "Authorization: Bearer ${API_SECRET}" \
|
-H "Authorization: Bearer ${API_SECRET}"
|
||||||
-d '{"take": 1000}'
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { prisma } from "@rallly/database";
|
||||||
import { headers } from "next/headers";
|
import { headers } from "next/headers";
|
||||||
import { NextResponse } from "next/server";
|
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 headersList = headers();
|
||||||
const authorization = headersList.get("authorization");
|
const authorization = headersList.get("authorization");
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ export async function POST(req: Request, ctx: { params: { task: string } }) {
|
||||||
return await deleteInactivePolls();
|
return await deleteInactivePolls();
|
||||||
}
|
}
|
||||||
case "remove-deleted-polls": {
|
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.
|
* Remove polls and corresponding data that have been marked deleted for more than 7 days.
|
||||||
*/
|
*/
|
||||||
async function removeDeletedPolls(req: Request) {
|
async function removeDeletedPolls() {
|
||||||
const options = (await req.json()) as { take?: number } | undefined;
|
const { count: deletedPollCount } = await prisma.poll.deleteMany({
|
||||||
// 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,
|
|
||||||
},
|
|
||||||
where: {
|
where: {
|
||||||
deleted: true,
|
deleted: true,
|
||||||
deletedAt: {
|
deletedAt: {
|
||||||
lt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
|
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({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
summary: {
|
summary: {
|
||||||
deleted: {
|
deleted: {
|
||||||
votes: deletedVoteCount,
|
|
||||||
options: deletedOptionCount,
|
|
||||||
participants: deletedParticipantCount,
|
|
||||||
comments: deletedCommentCount,
|
|
||||||
watchers: deletedWatcherCount,
|
|
||||||
polls: deletedPollCount,
|
polls: deletedPollCount,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -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");
|
|
@ -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");
|
|
@ -0,0 +1,2 @@
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE INDEX "polls_deleted_deleted_at_idx" ON "polls"("deleted", "deleted_at");
|
|
@ -146,6 +146,8 @@ model Poll {
|
||||||
comments Comment[] @relation("PollToComments")
|
comments Comment[] @relation("PollToComments")
|
||||||
votes Vote[] @relation("PollToVotes")
|
votes Vote[] @relation("PollToVotes")
|
||||||
|
|
||||||
|
@@index([deleted, touchedAt])
|
||||||
|
@@index([deleted, deletedAt])
|
||||||
@@map("polls")
|
@@map("polls")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,6 +175,7 @@ model Watcher {
|
||||||
|
|
||||||
poll Poll @relation("PollToWatchers", fields: [pollId], references: [id], onDelete: Cascade)
|
poll Poll @relation("PollToWatchers", fields: [pollId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
@@index([pollId], type: Hash)
|
||||||
@@map("watchers")
|
@@map("watchers")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,7 +184,6 @@ model Participant {
|
||||||
name String
|
name String
|
||||||
email String?
|
email String?
|
||||||
userId String? @map("user_id")
|
userId String? @map("user_id")
|
||||||
poll Poll @relation("PollToParticipants", fields: [pollId], references: [id], onDelete: Cascade)
|
|
||||||
pollId String @map("poll_id")
|
pollId String @map("poll_id")
|
||||||
locale String?
|
locale String?
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
createdAt DateTime @default(now()) @map("created_at")
|
||||||
|
@ -190,7 +192,9 @@ model Participant {
|
||||||
deletedAt DateTime? @map("deleted_at")
|
deletedAt DateTime? @map("deleted_at")
|
||||||
|
|
||||||
votes Vote[] @relation("ParticipantToVotes")
|
votes Vote[] @relation("ParticipantToVotes")
|
||||||
|
poll Poll @relation("PollToParticipants", fields: [pollId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
@@index([pollId], type: Hash)
|
||||||
@@map("participants")
|
@@map("participants")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,6 +206,7 @@ model Option {
|
||||||
poll Poll @relation("PollToOptions", fields: [pollId], references: [id], onDelete: Cascade)
|
poll Poll @relation("PollToOptions", fields: [pollId], references: [id], onDelete: Cascade)
|
||||||
createdAt DateTime @default(now()) @map("created_at")
|
createdAt DateTime @default(now()) @map("created_at")
|
||||||
|
|
||||||
|
@@index([pollId], type: Hash)
|
||||||
@@map("options")
|
@@map("options")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,6 +230,9 @@ model Vote {
|
||||||
participant Participant @relation("ParticipantToVotes", fields: [participantId], references: [id], onDelete: Cascade)
|
participant Participant @relation("ParticipantToVotes", fields: [participantId], references: [id], onDelete: Cascade)
|
||||||
poll Poll @relation("PollToVotes", fields: [pollId], 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")
|
@@map("votes")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,6 +248,7 @@ model Comment {
|
||||||
|
|
||||||
poll Poll @relation("PollToComments", fields: [pollId], references: [id], onDelete: Cascade)
|
poll Poll @relation("PollToComments", fields: [pollId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
|
@@index([pollId], type: Hash)
|
||||||
@@map("comments")
|
@@map("comments")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue