119 lines
3.6 KiB
TypeScript
119 lines
3.6 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';
|
|
import { BadRequestException } from '@nestjs/common';
|
|
|
|
@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];
|
|
if (record.steam_nickname !== null) {
|
|
item.nicknames.push(record.steam_nickname);
|
|
}
|
|
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,
|
|
nicknames: [],
|
|
comments: [],
|
|
toxic: Number(record.toxic),
|
|
cheater: Number(record.cheater),
|
|
afk: Number(record.afk),
|
|
useless: Number(record.useless),
|
|
griefer: Number(record.griefer),
|
|
};
|
|
if (record.steam_nickname !== null) {
|
|
item.nicknames.push(record.steam_nickname);
|
|
}
|
|
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();
|
|
}
|
|
|
|
async updateBlacklistRecord(
|
|
blackListReportDto: BlacklistReportDto,
|
|
): Promise<BlacklistReport> {
|
|
const record = await this.blacklistReportModel.findOne({
|
|
where: { id: blackListReportDto.id },
|
|
raw: false,
|
|
});
|
|
|
|
if (!record) {
|
|
throw new BadRequestException('Record not found', {
|
|
cause: new Error(),
|
|
description: `Report with id ${blackListReportDto.id} does not exist in the blacklist`,
|
|
});
|
|
}
|
|
|
|
// Update fields with validation to ensure types are correct
|
|
record.steam_link = blackListReportDto.steam_link;
|
|
record.comment = blackListReportDto.comment;
|
|
record.toxic = blackListReportDto.toxic;
|
|
record.cheater = blackListReportDto.cheater;
|
|
record.afk = blackListReportDto.afk;
|
|
record.useless = blackListReportDto.useless;
|
|
record.griefer = blackListReportDto.griefer;
|
|
|
|
return await record.save();
|
|
}
|
|
|
|
async deleteBlacklistRecord(id: number): Promise<boolean> {
|
|
try {
|
|
const item = await this.blacklistReportModel.findByPk(id);
|
|
|
|
if (!item) {
|
|
return false;
|
|
}
|
|
|
|
await item.destroy();
|
|
return true;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|