feat(db): add role log table to database

This commit is contained in:
smyalygames 2023-03-03 17:59:02 +00:00
parent 873afcd4d6
commit 232cbc4b67
3 changed files with 116 additions and 0 deletions

View File

@ -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;

View File

@ -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 {

66
src/utils/logging/role.ts Normal file
View File

@ -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);
}