From eef01b1c3df0fd4bc778bf855acc2b8db63e633f Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 17 Aug 2022 17:58:56 +0100 Subject: [PATCH] Add database table --- src/constants/AuditType.ts | 7 +++ src/entity/Audit.ts | 60 ++++++++++++++++++++++ src/entity/Server.ts | 4 ++ src/migration/1660754832945-CreateAudit.ts | 13 +++++ 4 files changed, 84 insertions(+) create mode 100644 src/constants/AuditType.ts create mode 100644 src/entity/Audit.ts create mode 100644 src/migration/1660754832945-CreateAudit.ts diff --git a/src/constants/AuditType.ts b/src/constants/AuditType.ts new file mode 100644 index 0000000..4ad8df6 --- /dev/null +++ b/src/constants/AuditType.ts @@ -0,0 +1,7 @@ +export enum AuditType { + General, + Warn, + Mute, + Kick, + Ban, +} \ No newline at end of file diff --git a/src/entity/Audit.ts b/src/entity/Audit.ts new file mode 100644 index 0000000..b2dfcd5 --- /dev/null +++ b/src/entity/Audit.ts @@ -0,0 +1,60 @@ +import { Column, Entity, getConnection, ManyToOne } from "typeorm"; +import { AuditType } from "../constants/AuditType"; +import BaseEntity from "../contracts/BaseEntity"; +import StringTools from "../helpers/StringTools"; +import Server from "./Server"; + +@Entity() +export default class Audit extends BaseEntity { + constructor(userId: string, auditType: AuditType, reason: string, moderatorId: string) { + super(); + + this.AuditId = StringTools.RandomString(5).toUpperCase(); + this.UserId = userId; + this.AuditType = auditType; + this.Reason = reason; + this.ModeratorId = moderatorId; + } + + @Column() + AuditId: string; + + @Column() + UserId: string; + + @Column() + AuditType: AuditType; + + @Column() + Reason: string; + + @Column() + ModeratorId: string; + + @ManyToOne(() => Server, x => x.Audits) + Server: Server; + + public AssignToServer(server: Server) { + this.Server = server; + } + + public static async FetchAuditsByUserId(userId: string): Promise { + const connection = getConnection(); + + const repository = connection.getRepository(Audit); + + const all = await repository.find({ UserId: userId }); + + return all; + } + + public static async FetchAuditByAuditId(auditId: string): Promise { + const connection = getConnection(); + + const repository = connection.getRepository(Audit); + + const single = await repository.findOne({ AuditId: auditId }); + + return single; + } +} \ No newline at end of file diff --git a/src/entity/Server.ts b/src/entity/Server.ts index f669e58..eadb249 100644 --- a/src/entity/Server.ts +++ b/src/entity/Server.ts @@ -1,5 +1,6 @@ import { Entity, OneToMany } from "typeorm"; import BaseEntity from "../contracts/BaseEntity"; +import Audit from "./Audit"; import Role from "./Role"; import Setting from "./Setting"; @@ -17,6 +18,9 @@ export default class Server extends BaseEntity { @OneToMany(() => Role, x => x.Server) Roles: Role[]; + @OneToMany(() => Audit, x => x.Server) + Audits: Audit[]; + public AddSettingToServer(setting: Setting) { this.Settings.push(setting); } diff --git a/src/migration/1660754832945-CreateAudit.ts b/src/migration/1660754832945-CreateAudit.ts new file mode 100644 index 0000000..9a44ed7 --- /dev/null +++ b/src/migration/1660754832945-CreateAudit.ts @@ -0,0 +1,13 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class CreateAudit1660754832945 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + queryRunner.query(`CREATE TABLE audit (Id varchar(255), WhenCreated datetime, WhenUpdated datetime, auditId varchar(255), userId varchar(255), auditType int, reason varchar(255), moderatorId varchar(255), serverId varchar(255), PRIMARY KEY (Id), FOREIGN KEY (serverId) REFERENCES server(id))`); + } + + public async down(queryRunner: QueryRunner): Promise { + queryRunner.query(`DROP TABLE audit`); + } + +}