From 8e16a4fda6ca55e45f9f2c63e1e2636ed2df38ab Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 30 Nov 2024 13:53:13 +0000 Subject: [PATCH] Add AutoKickConfig to database --- .../Down/01-AutoKickConfig.sql | 1 + .../Up/01-AutoKickConfig-Table.sql | 10 ++++ .../Up/02-AutoKickConfig-Key.sql | 2 + src/database/entities/AutoKickConfig.ts | 50 +++++++++++++++++++ .../1732973911304-createAutoKickConfig.ts | 19 +++++++ 5 files changed, 82 insertions(+) create mode 100644 database/3.2.4/1732973911304-createAutoKickConfig/Down/01-AutoKickConfig.sql create mode 100644 database/3.2.4/1732973911304-createAutoKickConfig/Up/01-AutoKickConfig-Table.sql create mode 100644 database/3.2.4/1732973911304-createAutoKickConfig/Up/02-AutoKickConfig-Key.sql create mode 100644 src/database/entities/AutoKickConfig.ts create mode 100644 src/database/migrations/3.2.4/1732973911304-createAutoKickConfig.ts diff --git a/database/3.2.4/1732973911304-createAutoKickConfig/Down/01-AutoKickConfig.sql b/database/3.2.4/1732973911304-createAutoKickConfig/Down/01-AutoKickConfig.sql new file mode 100644 index 0000000..8412a02 --- /dev/null +++ b/database/3.2.4/1732973911304-createAutoKickConfig/Down/01-AutoKickConfig.sql @@ -0,0 +1 @@ +DROP TABLE auto_kick_config; diff --git a/database/3.2.4/1732973911304-createAutoKickConfig/Up/01-AutoKickConfig-Table.sql b/database/3.2.4/1732973911304-createAutoKickConfig/Up/01-AutoKickConfig-Table.sql new file mode 100644 index 0000000..e7d377a --- /dev/null +++ b/database/3.2.4/1732973911304-createAutoKickConfig/Up/01-AutoKickConfig-Table.sql @@ -0,0 +1,10 @@ +CREATE TABLE auto_kick_config ( + Id varchar(255) NOT NULL, + WhenCreated datetime NOT NULL, + WhenUpdated datetime NOT NULL, + ServerId varchar(255) NOT NULL, + RoleId varchar(255) NOT NULL, + KickTime int NOT NULL, + NoticeTime int NULL, + NoticeChannelId varchar(255) NULL +); diff --git a/database/3.2.4/1732973911304-createAutoKickConfig/Up/02-AutoKickConfig-Key.sql b/database/3.2.4/1732973911304-createAutoKickConfig/Up/02-AutoKickConfig-Key.sql new file mode 100644 index 0000000..e3fec43 --- /dev/null +++ b/database/3.2.4/1732973911304-createAutoKickConfig/Up/02-AutoKickConfig-Key.sql @@ -0,0 +1,2 @@ +ALTER TABLE auto_kick_config + ADD PRIMARY KEY (Id); diff --git a/src/database/entities/AutoKickConfig.ts b/src/database/entities/AutoKickConfig.ts new file mode 100644 index 0000000..1ee3625 --- /dev/null +++ b/src/database/entities/AutoKickConfig.ts @@ -0,0 +1,50 @@ +import {Column, Entity} from "typeorm"; +import AppDataSource from "../dataSources/appDataSource"; +import BaseEntity from "../../contracts/BaseEntity"; + +@Entity() +export default class AutoKickConfig extends BaseEntity { + constructor(serverId: string, roleId: string, kickTime: number, noticeTime?: number, noticeChannelId?: string) { + super(); + + this.ServerId = serverId; + this.RoleId = roleId; + this.KickTime = kickTime; + this.NoticeTime = noticeTime; + this.NoticeChannelId = noticeChannelId; + } + + @Column() + ServerId: string; + + @Column() + RoleId: string; + + @Column({ type: "int" }) + KickTime: number; + + @Column({ type: "int", nullable: true }) + NoticeTime?: number; + + @Column({ nullable: true }) + NoticeChannelId?: string; + + public UpdateBasicDetails(roleId: string, kickTime: number, noticeTime?: number, noticeChannelId?: string) { + this.RoleId = roleId; + this.KickTime = kickTime; + this.NoticeTime = noticeTime; + this.NoticeChannelId = noticeChannelId; + } + + public static async FetchOneByServerIdAndRoleId(serverId: string, roleId: string): Promise { + const repository = AppDataSource.getRepository(AutoKickConfig); + + const query = repository + .createQueryBuilder("config") + .where("config.serverId = :serverId", { serverId }) + .andWhere("config.roleId = :roleId", { roleId }) + .getOne(); + + return query; + } +} diff --git a/src/database/migrations/3.2.4/1732973911304-createAutoKickConfig.ts b/src/database/migrations/3.2.4/1732973911304-createAutoKickConfig.ts new file mode 100644 index 0000000..c4e9a74 --- /dev/null +++ b/src/database/migrations/3.2.4/1732973911304-createAutoKickConfig.ts @@ -0,0 +1,19 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; +import MigrationHelper from "../../../helpers/MigrationHelper"; + +export class CreateAutoKickConfig1732973911304 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + MigrationHelper.Up("1732973911304-createAutoKickConfig", "3.2.4", [ + "01-AutoKickConfig-Table", + "02-AutoKickConfig-Key", + ], queryRunner) + } + + public async down(queryRunner: QueryRunner): Promise { + MigrationHelper.Down("1732973911304-createAutoKickConfig", "3.2.4", [ + "01-AutoKickConfig", + ], queryRunner) + } + +}