From a40925e9e91362fcb158caf6529b157dabeed20a Mon Sep 17 00:00:00 2001 From: smyalygames Date: Fri, 28 Oct 2022 02:24:44 +0100 Subject: [PATCH] feat(arabot): add database for bans --- .../20221028003502_unban/migration.sql | 12 +++ .../20221028011419_unban_fix/migration.sql | 8 ++ prisma/schema.prisma | 20 ++-- src/utils/database/ban.ts | 99 +++++++++++++++++++ 4 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 prisma/migrations/20221028003502_unban/migration.sql create mode 100644 prisma/migrations/20221028011419_unban_fix/migration.sql create mode 100644 src/utils/database/ban.ts diff --git a/prisma/migrations/20221028003502_unban/migration.sql b/prisma/migrations/20221028003502_unban/migration.sql new file mode 100644 index 0000000..45b2b28 --- /dev/null +++ b/prisma/migrations/20221028003502_unban/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - Added the required column `endModId` to the `Ban` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "Ban" ADD COLUMN "endModId" TEXT NOT NULL, +ADD COLUMN "endTime" TIMESTAMP(3); + +-- AddForeignKey +ALTER TABLE "Ban" ADD CONSTRAINT "Ban_endModId_fkey" FOREIGN KEY ("endModId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20221028011419_unban_fix/migration.sql b/prisma/migrations/20221028011419_unban_fix/migration.sql new file mode 100644 index 0000000..3778f83 --- /dev/null +++ b/prisma/migrations/20221028011419_unban_fix/migration.sql @@ -0,0 +1,8 @@ +-- DropForeignKey +ALTER TABLE "Ban" DROP CONSTRAINT "Ban_endModId_fkey"; + +-- AlterTable +ALTER TABLE "Ban" ALTER COLUMN "endModId" DROP NOT NULL; + +-- AddForeignKey +ALTER TABLE "Ban" ADD CONSTRAINT "Ban_endModId_fkey" FOREIGN KEY ("endModId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3e4f17f..129ad31 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -43,6 +43,7 @@ model User { RestrictMod Restrict[] @relation("restMod") BanUser Ban[] @relation("banUser") BanMod Ban[] @relation("banMod") + BanEndMod Ban[] @relation("endBanMod") TempBanUser TempBan[] @relation("tbanUser") TempBanMod TempBan[] @relation("tbanMod") } @@ -99,14 +100,17 @@ model Restrict { } model Ban { - id Int @id @default(autoincrement()) - user User @relation("banUser", fields: [userId], references: [id]) - userId String - mod User @relation("banMod", fields: [modId], references: [id]) - modId String - time DateTime @default(now()) - active Boolean @default(true) - reason String + id Int @id @default(autoincrement()) + user User @relation("banUser", fields: [userId], references: [id]) + userId String + mod User @relation("banMod", fields: [modId], references: [id]) + modId String + time DateTime @default(now()) + endMod User? @relation("endBanMod", fields: [endModId], references: [id]) + endModId String? + endTime DateTime? + active Boolean @default(true) + reason String } model TempBan { diff --git a/src/utils/database/ban.ts b/src/utils/database/ban.ts new file mode 100644 index 0000000..4eab7d9 --- /dev/null +++ b/src/utils/database/ban.ts @@ -0,0 +1,99 @@ +import { PrismaClient } from '@prisma/client'; + +export async function addBan(userId: string, modId: string, reason: string) { + // Initialise the database connection + const prisma = new PrismaClient(); + + // Add the user to the database + await prisma.ban.create({ + data: { + user: { + connect: { + id: userId, + }, + }, + mod: { + connect: { + id: modId, + }, + }, + reason, + }, + }); + + // Close the database connection + await prisma.$disconnect(); +} + +export async function removeBan(userId: string, modId: string) { + // Initialise the database connection + const prisma = new PrismaClient(); + + const ban = await prisma.ban.findFirst({ + where: { + userId, + }, + orderBy: { + id: 'desc', + }, + }); + + if (ban === null) { + return; + } + + // Query to deactivate the specific sus note + await prisma.ban.update({ + where: { + id: ban.id, + }, + data: { + endModId: modId, + endTime: new Date(), + active: false, + }, + }); + + // Close the database connection + await prisma.$disconnect(); +} + +export async function checkActive(userId: string) { + // Initialise the database connection + const prisma = new PrismaClient(); + + const ban = await prisma.ban.findFirst({ + where: { + userId, + }, + orderBy: { + id: 'desc', + }, + }); + + if (ban === null) { + return false; + } + + return ban.active; +} + +export async function getReason(userId: string) { + // Initialise the database connection + const prisma = new PrismaClient(); + + const ban = await prisma.ban.findFirst({ + where: { + userId, + }, + orderBy: { + id: 'desc', + }, + }); + + if (ban === null) { + return ''; + } + + return ban.reason; +}