2018-10-26 06:41:20 +03:00
|
|
|
import { Case } from "./entities/Case";
|
|
|
|
import { CaseNote } from "./entities/CaseNote";
|
|
|
|
import { BaseRepository } from "./BaseRepository";
|
|
|
|
import { getRepository, In, Repository } from "typeorm";
|
2018-07-12 02:53:26 +03:00
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
export class GuildCases extends BaseRepository {
|
|
|
|
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,
|
|
|
|
id: In(ids)
|
|
|
|
}
|
|
|
|
});
|
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,
|
|
|
|
id
|
|
|
|
}
|
|
|
|
});
|
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,
|
|
|
|
case_number: caseNumber
|
|
|
|
}
|
|
|
|
});
|
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,
|
|
|
|
user_id: userId
|
|
|
|
}
|
|
|
|
});
|
2018-08-02 00:51:25 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 02:53:26 +03:00
|
|
|
async create(data): Promise<number> {
|
2018-10-26 06:41:20 +03:00
|
|
|
const result = await this.cases.insert({
|
|
|
|
...data,
|
|
|
|
guild_id: this.guildId,
|
|
|
|
case_number: () => `(SELECT IFNULL(MAX(case_number)+1, 1) FROM cases AS ma2 WHERE guild_id = ${this.guildId})`
|
|
|
|
});
|
|
|
|
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
async createNote(caseId: number, data: any): Promise<number> {
|
|
|
|
const result = await this.caseNotes.insert({
|
|
|
|
...data,
|
|
|
|
case_id: caseId
|
|
|
|
});
|
|
|
|
|
|
|
|
return result.identifiers[0].id;
|
2018-07-12 02:53:26 +03:00
|
|
|
}
|
|
|
|
}
|