2018-10-26 06:41:20 +03:00
|
|
|
import { Case } from "./entities/Case";
|
|
|
|
import { CaseNote } from "./entities/CaseNote";
|
2019-05-25 21:25:34 +03:00
|
|
|
import { BaseGuildRepository } from "./BaseGuildRepository";
|
2018-10-26 06:41:20 +03:00
|
|
|
import { getRepository, In, Repository } from "typeorm";
|
2020-08-19 00:19:12 +03:00
|
|
|
import { DBDateFormat, disableLinkPreviews } from "../utils";
|
2019-01-15 04:15:22 +02:00
|
|
|
import { CaseTypes } from "./CaseTypes";
|
2019-04-13 02:27:52 +03:00
|
|
|
import moment = require("moment-timezone");
|
2020-08-09 22:44:07 +03:00
|
|
|
import { connection } from "./db";
|
2019-01-15 04:15:22 +02:00
|
|
|
|
|
|
|
const CASE_SUMMARY_REASON_MAX_LENGTH = 300;
|
2018-07-12 02:53:26 +03:00
|
|
|
|
2019-05-25 21:25:34 +03:00
|
|
|
export class GuildCases extends BaseGuildRepository {
|
2018-10-26 06:41:20 +03:00
|
|
|
private cases: Repository<Case>;
|
|
|
|
private caseNotes: Repository<CaseNote>;
|
2018-07-12 02:53:26 +03:00
|
|
|
|
|
|
|
constructor(guildId) {
|
2018-10-26 06:41:20 +03:00
|
|
|
super(guildId);
|
|
|
|
this.cases = getRepository(Case);
|
|
|
|
this.caseNotes = getRepository(CaseNote);
|
2018-07-12 02:53:26 +03:00
|
|
|
}
|
|
|
|
|
2018-08-05 00:18:50 +03:00
|
|
|
async get(ids: number[]): Promise<Case[]> {
|
2018-10-26 06:41:20 +03:00
|
|
|
return this.cases.find({
|
|
|
|
relations: this.getRelations(),
|
|
|
|
where: {
|
|
|
|
guild_id: this.guildId,
|
2019-02-17 15:23:40 +02:00
|
|
|
id: In(ids),
|
|
|
|
},
|
2018-10-26 06:41:20 +03:00
|
|
|
});
|
2018-08-05 00:18:50 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 02:53:26 +03:00
|
|
|
async find(id: number): Promise<Case> {
|
2018-10-26 06:41:20 +03:00
|
|
|
return this.cases.findOne({
|
|
|
|
relations: this.getRelations(),
|
|
|
|
where: {
|
|
|
|
guild_id: this.guildId,
|
2019-02-17 15:23:40 +02:00
|
|
|
id,
|
|
|
|
},
|
2018-10-26 06:41:20 +03:00
|
|
|
});
|
2018-07-12 02:53:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async findByCaseNumber(caseNumber: number): Promise<Case> {
|
2018-10-26 06:41:20 +03:00
|
|
|
return this.cases.findOne({
|
|
|
|
relations: this.getRelations(),
|
|
|
|
where: {
|
|
|
|
guild_id: this.guildId,
|
2019-02-17 15:23:40 +02:00
|
|
|
case_number: caseNumber,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async findLatestByModId(modId: string): Promise<Case> {
|
|
|
|
return this.cases.findOne({
|
|
|
|
relations: this.getRelations(),
|
|
|
|
where: {
|
|
|
|
guild_id: this.guildId,
|
|
|
|
mod_id: modId,
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
case_number: "DESC",
|
|
|
|
},
|
2018-10-26 06:41:20 +03:00
|
|
|
});
|
2018-07-12 02:53:26 +03:00
|
|
|
}
|
|
|
|
|
2019-04-14 17:05:07 +03:00
|
|
|
async findByAuditLogId(auditLogId: string): Promise<Case> {
|
|
|
|
return this.cases.findOne({
|
|
|
|
relations: this.getRelations(),
|
|
|
|
where: {
|
|
|
|
guild_id: this.guildId,
|
|
|
|
audit_log_id: auditLogId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-12 02:53:26 +03:00
|
|
|
async getByUserId(userId: string): Promise<Case[]> {
|
2018-10-26 06:41:20 +03:00
|
|
|
return this.cases.find({
|
|
|
|
relations: this.getRelations(),
|
|
|
|
where: {
|
|
|
|
guild_id: this.guildId,
|
2019-02-17 15:23:40 +02:00
|
|
|
user_id: userId,
|
|
|
|
},
|
2018-10-26 06:41:20 +03:00
|
|
|
});
|
2018-08-02 00:51:25 +03:00
|
|
|
}
|
|
|
|
|
2019-02-23 22:40:43 +02:00
|
|
|
async getRecentByModId(modId: string, count: number): Promise<Case[]> {
|
2019-02-23 21:33:57 +02:00
|
|
|
return this.cases.find({
|
|
|
|
relations: this.getRelations(),
|
|
|
|
where: {
|
|
|
|
guild_id: this.guildId,
|
2019-02-23 22:40:43 +02:00
|
|
|
mod_id: modId,
|
2019-02-23 21:33:57 +02:00
|
|
|
is_hidden: 0,
|
|
|
|
},
|
|
|
|
take: count,
|
|
|
|
order: {
|
|
|
|
case_number: "DESC",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-13 17:56:14 +02:00
|
|
|
async setHidden(id: number, hidden: boolean): Promise<void> {
|
|
|
|
await this.cases.update(
|
|
|
|
{ id },
|
|
|
|
{
|
2019-02-17 15:23:40 +02:00
|
|
|
is_hidden: hidden,
|
|
|
|
},
|
2019-01-13 17:56:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
async create(data): Promise<Case> {
|
2018-10-26 06:41:20 +03:00
|
|
|
const result = await this.cases.insert({
|
|
|
|
...data,
|
|
|
|
guild_id: this.guildId,
|
2019-02-17 15:23:40 +02:00
|
|
|
case_number: () => `(SELECT IFNULL(MAX(case_number)+1, 1) FROM cases AS ma2 WHERE guild_id = ${this.guildId})`,
|
2018-10-26 06:41:20 +03:00
|
|
|
});
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
return this.find(result.identifiers[0].id);
|
2018-07-12 02:53:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
update(id, data) {
|
2018-10-26 06:41:20 +03:00
|
|
|
return this.cases.update(id, data);
|
2018-07-12 02:53:26 +03:00
|
|
|
}
|
|
|
|
|
2020-08-09 22:44:07 +03:00
|
|
|
async softDelete(id: number, deletedById: string, deletedByName: string, deletedByText: string) {
|
|
|
|
return connection.transaction(async entityManager => {
|
|
|
|
const cases = entityManager.getRepository(Case);
|
|
|
|
const caseNotes = entityManager.getRepository(CaseNote);
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
caseNotes.delete({
|
|
|
|
case_id: id,
|
|
|
|
}),
|
|
|
|
cases.update(id, {
|
|
|
|
user_id: "0",
|
|
|
|
user_name: "Unknown#0000",
|
|
|
|
mod_id: null,
|
|
|
|
mod_name: "Unknown#0000",
|
|
|
|
type: CaseTypes.Deleted,
|
|
|
|
audit_log_id: null,
|
|
|
|
is_hidden: false,
|
|
|
|
pp_id: null,
|
|
|
|
pp_name: null,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
await caseNotes.insert({
|
|
|
|
case_id: id,
|
|
|
|
mod_id: deletedById,
|
|
|
|
mod_name: deletedByName,
|
|
|
|
body: deletedByText,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
async createNote(caseId: number, data: any): Promise<void> {
|
|
|
|
await this.caseNotes.insert({
|
2018-10-26 06:41:20 +03:00
|
|
|
...data,
|
2019-02-17 15:23:40 +02:00
|
|
|
case_id: caseId,
|
2018-10-26 06:41:20 +03:00
|
|
|
});
|
2018-07-12 02:53:26 +03:00
|
|
|
}
|
|
|
|
}
|