diff --git a/prisma/migrations/20230303182446_role_logs/migration.sql b/prisma/migrations/20230303182446_role_logs/migration.sql new file mode 100644 index 0000000..6efb58a --- /dev/null +++ b/prisma/migrations/20230303182446_role_logs/migration.sql @@ -0,0 +1,29 @@ +-- CreateTable +CREATE TABLE "RoleLog" ( + "id" SERIAL NOT NULL, + "userId" TEXT NOT NULL, + "modId" TEXT NOT NULL, + "roleId" TEXT NOT NULL, + "add" BOOLEAN NOT NULL DEFAULT false, + "time" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "RoleLog_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Role" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "staff" BOOLEAN NOT NULL, + + CONSTRAINT "Role_pkey" PRIMARY KEY ("id") +); + +-- AddForeignKey +ALTER TABLE "RoleLog" ADD CONSTRAINT "RoleLog_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "RoleLog" ADD CONSTRAINT "RoleLog_modId_fkey" FOREIGN KEY ("modId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "RoleLog" ADD CONSTRAINT "RoleLog_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index dcc7705..998727a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -42,6 +42,8 @@ model User { SendPayment Payment[] @relation("sendPayment") RecievePayment Payment[] @relation("recievePayment") LeaveLog LeaveLog[] + UserRoleLog RoleLog[] @relation("userRoleLog") + ModRoleLog RoleLog[] @relation("modRoleLog") EventLeader Event[] @relation("eventLeader") StatLeader Stat[] @relation("statLeader") OutreachParticipation ParticipantStat[] @relation("participantUser") @@ -129,6 +131,25 @@ model LeaveLog { roles String[] } +model RoleLog { + id Int @id @default(autoincrement()) + user User @relation("userRoleLog", fields: [userId], references: [id]) + userId String + mod User @relation("modRoleLog", fields: [modId], references: [id]) + modId String + role Role @relation(fields: [roleId], references: [id]) + roleId String + add Boolean @default(false) + time DateTime @default(now()) +} + +model Role { + id String @id + name String + staff Boolean + RoleLog RoleLog[] +} + // Outreach model Event { diff --git a/src/utils/logging/role.ts b/src/utils/logging/role.ts new file mode 100644 index 0000000..b102e2b --- /dev/null +++ b/src/utils/logging/role.ts @@ -0,0 +1,66 @@ +import { container } from '@sapphire/framework'; +import type { Snowflake, Role } from 'discord.js'; + +export async function roleLog( + userId: Snowflake, + modId: Snowflake, + role: Role, + staffRole : boolean, + add: boolean, +) { + await container.database.roleLog.create({ + data: { + user: { + connectOrCreate: { + where: { + id: userId, + }, + create: { + id: userId, + }, + }, + }, + mod: { + connectOrCreate: { + where: { + id: modId, + }, + create: { + id: modId, + }, + }, + }, + role: { + connectOrCreate: { + where: { + id: role.id, + }, + create: { + id: role.id, + name: role.name, + staff: staffRole, + }, + }, + }, + add, + }, + }); +} + +export async function roleAddLog( + userId: Snowflake, + modId: Snowflake, + role: Role, + staffRole = false, +) { + await roleLog(userId, modId, role, staffRole, true); +} + +export async function roleRemoveLog( + userId: Snowflake, + modId: Snowflake, + role: Role, + staffRole = false, +) { + await roleLog(userId, modId, role, staffRole, false); +}