feat(database): add fun logging to database

This commit is contained in:
smyalygames 2023-03-07 15:04:26 +00:00
parent c8521d0c06
commit 60c4b8f0f3
3 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,29 @@
-- CreateTable
CREATE TABLE "FunLog" (
"id" SERIAL NOT NULL,
"sendUserId" TEXT NOT NULL,
"receiveUserId" TEXT,
"typeId" INTEGER NOT NULL,
CONSTRAINT "FunLog_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "FunType" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "FunType_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "FunType_name_key" ON "FunType"("name");
-- AddForeignKey
ALTER TABLE "FunLog" ADD CONSTRAINT "FunLog_sendUserId_fkey" FOREIGN KEY ("sendUserId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "FunLog" ADD CONSTRAINT "FunLog_receiveUserId_fkey" FOREIGN KEY ("receiveUserId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "FunLog" ADD CONSTRAINT "FunLog_typeId_fkey" FOREIGN KEY ("typeId") REFERENCES "FunType"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -45,6 +45,8 @@ model User {
LeaveLog LeaveLog[] LeaveLog LeaveLog[]
UserRoleLog RoleLog[] @relation("userRoleLog") UserRoleLog RoleLog[] @relation("userRoleLog")
ModRoleLog RoleLog[] @relation("modRoleLog") ModRoleLog RoleLog[] @relation("modRoleLog")
FunLogSender FunLog[] @relation("sendFunLog")
FunLogReciever FunLog[] @relation("receiveFunLog")
EventLeader Event[] @relation("eventLeader") EventLeader Event[] @relation("eventLeader")
StatLeader Stat[] @relation("statLeader") StatLeader Stat[] @relation("statLeader")
OutreachParticipation ParticipantStat[] @relation("participantUser") OutreachParticipation ParticipantStat[] @relation("participantUser")
@ -160,6 +162,22 @@ model Role {
RoleLog RoleLog[] RoleLog RoleLog[]
} }
model FunLog {
id Int @id @default(autoincrement())
sendUser User @relation("sendFunLog", fields: [sendUserId], references: [id])
sendUserId String
receiveUser User? @relation("receiveFunLog", fields: [receiveUserId], references: [id])
receiveUserId String?
type FunType @relation(fields: [typeId], references: [id])
typeId Int
}
model FunType {
id Int @id @default(autoincrement())
name String @unique
FunLog FunLog[]
}
// Outreach // Outreach
model Event { model Event {

91
src/utils/database/fun.ts Normal file
View File

@ -0,0 +1,91 @@
import { container } from '@sapphire/framework';
import type { Snowflake } from 'discord.js';
// Balance
export async function countTotal(
senderId: Snowflake,
type: string,
receiverId: Snowflake | undefined = undefined,
) {
const result = await container.database.funLog.count({
where: {
sendUserId: senderId,
receiveUserId: receiverId,
type: {
name: type,
},
},
});
return result;
}
export async function addFunLog(
senderId: Snowflake,
type: string,
receiverId: Snowflake | undefined = undefined,
) {
if (receiverId === undefined) {
await container.database.funLog.create({
data: {
sendUser: {
connectOrCreate: {
where: {
id: senderId,
},
create: {
id: senderId,
},
},
},
type: {
connectOrCreate: {
where: {
name: type,
},
create: {
name: type,
},
},
},
},
});
return;
}
await container.database.funLog.create({
data: {
sendUser: {
connectOrCreate: {
where: {
id: senderId,
},
create: {
id: senderId,
},
},
},
receiveUser: {
connectOrCreate: {
where: {
id: receiverId,
},
create: {
id: receiverId,
},
},
},
type: {
connectOrCreate: {
where: {
name: type,
},
create: {
name: type,
},
},
},
},
});
}