69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
|
|
import { Injectable } from '@nestjs/common';
|
||
|
|
import { InjectModel } from '@nestjs/sequelize';
|
||
|
|
import { BlacklistReport } from './models/blacklistReport.model';
|
||
|
|
import { BlacklistItem } from './types/blacklistItem';
|
||
|
|
import { BlacklistItemList } from './types/blacklistItemList';
|
||
|
|
import { BlacklistReportDto } from './dto/blacklistReport';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class BlacklistService {
|
||
|
|
constructor(
|
||
|
|
@InjectModel(BlacklistReport)
|
||
|
|
private blacklistReportModel: typeof BlacklistReport,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
getHealthCheck(): string {
|
||
|
|
return 'OK';
|
||
|
|
}
|
||
|
|
|
||
|
|
async getAllRecords(): Promise<BlacklistItemList> {
|
||
|
|
const allRecords = await this.blacklistReportModel.findAll({ raw: true });
|
||
|
|
return this.groupRecordsByLink(allRecords);
|
||
|
|
}
|
||
|
|
|
||
|
|
async getRecordsByLink(link: string): Promise<BlacklistItemList> {
|
||
|
|
const allRecords: BlacklistReport[] =
|
||
|
|
await this.blacklistReportModel.findAll({
|
||
|
|
raw: true,
|
||
|
|
where: { steam_link: link },
|
||
|
|
});
|
||
|
|
return this.groupRecordsByLink(allRecords);
|
||
|
|
}
|
||
|
|
|
||
|
|
groupRecordsByLink(records: BlacklistReport[]): BlacklistItemList {
|
||
|
|
const result: BlacklistItemList = {};
|
||
|
|
for (const record of records) {
|
||
|
|
if (result[record.steam_link]) {
|
||
|
|
const item: BlacklistItem = result[record.steam_link];
|
||
|
|
item.comments.push(record.comment);
|
||
|
|
item.toxic += Number(record.toxic);
|
||
|
|
item.cheater += Number(record.cheater);
|
||
|
|
item.afk += Number(record.afk);
|
||
|
|
item.useless += Number(record.useless);
|
||
|
|
item.griefer += Number(record.griefer);
|
||
|
|
result[record.steam_link] = item;
|
||
|
|
} else {
|
||
|
|
const item: BlacklistItem = {
|
||
|
|
link: record.steam_link,
|
||
|
|
comments: [],
|
||
|
|
toxic: Number(record.toxic),
|
||
|
|
cheater: Number(record.cheater),
|
||
|
|
afk: Number(record.afk),
|
||
|
|
useless: Number(record.useless),
|
||
|
|
griefer: Number(record.griefer),
|
||
|
|
};
|
||
|
|
item.comments.push(record.comment);
|
||
|
|
result[record.steam_link] = item;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
async createBlacklistRecord(
|
||
|
|
blackListReportDto: BlacklistReportDto,
|
||
|
|
): Promise<BlacklistReport> {
|
||
|
|
const report = BlacklistReport.build(blackListReportDto);
|
||
|
|
return await report.save();
|
||
|
|
}
|
||
|
|
}
|