From ec16f75d348724a99542a85a2dbfffc11cdd1dc4 Mon Sep 17 00:00:00 2001 From: josefaidt Date: Fri, 25 Aug 2023 18:51:08 -0700 Subject: [PATCH] remove deleted threads/questions --- .../migration.sql | 15 ++++++++++++ prisma/schema.prisma | 4 ++-- src/lib/discord/client.ts | 24 ++++++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 prisma/migrations/20230826014143_add_referential_actions/migration.sql diff --git a/prisma/migrations/20230826014143_add_referential_actions/migration.sql b/prisma/migrations/20230826014143_add_referential_actions/migration.sql new file mode 100644 index 00000000..93e38e88 --- /dev/null +++ b/prisma/migrations/20230826014143_add_referential_actions/migration.sql @@ -0,0 +1,15 @@ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Participation" ( + "id" TEXT NOT NULL PRIMARY KEY, + "questionId" TEXT NOT NULL, + "participantId" TEXT NOT NULL, + CONSTRAINT "Participation_questionId_fkey" FOREIGN KEY ("questionId") REFERENCES "Question" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "Participation_participantId_fkey" FOREIGN KEY ("participantId") REFERENCES "DiscordUser" ("id") ON DELETE NO ACTION ON UPDATE CASCADE +); +INSERT INTO "new_Participation" ("id", "participantId", "questionId") SELECT "id", "participantId", "questionId" FROM "Participation"; +DROP TABLE "Participation"; +ALTER TABLE "new_Participation" RENAME TO "Participation"; +CREATE UNIQUE INDEX "Participation_questionId_participantId_key" ON "Participation"("questionId", "participantId"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index d05ab063..e8fd13c3 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -51,9 +51,9 @@ model QuestionTag { model Participation { id String @id @default(cuid()) - question Question @relation(fields: [questionId], references: [id]) + question Question @relation(fields: [questionId], references: [id], onDelete: Cascade) questionId String - participant DiscordUser @relation(fields: [participantId], references: [id]) + participant DiscordUser @relation(fields: [participantId], references: [id], onDelete: NoAction) // @TODO - track role at the time of participation, accounts for users that _leave_ "staff" participantId String participantRoles DiscordRole[] diff --git a/src/lib/discord/client.ts b/src/lib/discord/client.ts index 0ccd91c5..430b9fda 100644 --- a/src/lib/discord/client.ts +++ b/src/lib/discord/client.ts @@ -186,7 +186,6 @@ client.on(Events.MessageCreate, async (message: Message) => { // capture thread updates in public "help" channels if ( message.channel.type === ChannelType.PublicThread && - !message.author.bot && isThreadWithinHelpChannel(message.channel) ) { let record @@ -446,6 +445,29 @@ client.on(Events.ThreadUpdate, async (oldThread, newThread) => { } }) +client.on(Events.ThreadDelete, async (thread) => { + const log = (message: string) => + console.log(`[client:events.ThreadDelete] ${message}`) + log('started') + const timeMessage = '[client:events:ThreadDelete] finished' + console.time(timeMessage) + log('deleting question...') + try { + await prisma.question.delete({ + where: { + threadId: thread.id, + }, + include: { + participation: true, + answer: true, + }, + }) + } catch (error) { + console.error('Unable to delete question', error) + } + console.timeEnd(timeMessage) +}) + export function createBot(token = process.env.DISCORD_BOT_TOKEN) { return client.login(token) }