mirror of
https://github.com/veganhacktivists/arabot.git
synced 2025-05-18 13:34:14 +02:00
feat(database): add fun logging to database
This commit is contained in:
parent
c8521d0c06
commit
60c4b8f0f3
29
prisma/migrations/20230307144946_fun_logs/migration.sql
Normal file
29
prisma/migrations/20230307144946_fun_logs/migration.sql
Normal 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;
|
@ -45,6 +45,8 @@ model User {
|
||||
LeaveLog LeaveLog[]
|
||||
UserRoleLog RoleLog[] @relation("userRoleLog")
|
||||
ModRoleLog RoleLog[] @relation("modRoleLog")
|
||||
FunLogSender FunLog[] @relation("sendFunLog")
|
||||
FunLogReciever FunLog[] @relation("receiveFunLog")
|
||||
EventLeader Event[] @relation("eventLeader")
|
||||
StatLeader Stat[] @relation("statLeader")
|
||||
OutreachParticipation ParticipantStat[] @relation("participantUser")
|
||||
@ -160,6 +162,22 @@ model Role {
|
||||
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
|
||||
|
||||
model Event {
|
||||
|
91
src/utils/database/fun.ts
Normal file
91
src/utils/database/fun.ts
Normal 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,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user