Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/utils/db/blacklist.db.server.ts
T

68 lines
2.0 KiB
TypeScript

import { Blacklist } from "@toolctrl/db/prisma-browser";
import Constants from "~/application/Constants";
import { FiltersDto } from "~/application/dtos/data/FiltersDto";
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
import { db } from "../db.server";
import { createScopedLogger } from "../logger.server";
const blacklistLogger = createScopedLogger("db.blacklist");
export async function getBlacklist(
filters?: FiltersDto,
pagination?: { page: number; pageSize: number }
): Promise<{ items: Blacklist[]; pagination: PaginationDto }> {
const items = await db.blacklist.findMany({
skip: pagination ? pagination?.pageSize * (pagination?.page - 1) : undefined,
take: pagination ? pagination?.pageSize : undefined,
orderBy: [{ registerAttempts: "desc" }, { createdAt: "desc" }],
});
const totalItems = await db.blacklist.count({});
return {
items,
pagination: {
page: pagination?.page ?? 1,
pageSize: pagination?.pageSize ?? Constants.DEFAULT_PAGE_SIZE,
totalItems,
totalPages: Math.ceil(totalItems / (pagination?.pageSize ?? Constants.DEFAULT_PAGE_SIZE)),
},
};
}
export async function findInBlacklist(type: string, value: string): Promise<Blacklist | null> {
return await db.blacklist.findFirst({
where: {
type,
value,
},
});
}
export async function addBlacklistAttempt(item: Blacklist) {
blacklistLogger.info(
{ type: item.type, value: item.value, registerAttempts: item.registerAttempts + 1 },
`[BLACKLISTED ${item.type.toUpperCase()}]`
);
return await db.blacklist.updateMany({
where: {
type: item.type,
value: item.value,
},
data: {
registerAttempts: item.registerAttempts + 1,
},
});
}
export async function addToBlacklist(data: { type: string; value: string }) {
return await db.blacklist.create({
data,
});
}
export async function deleteFromBlacklist(where: { type: string; value: string }) {
return await db.blacklist.deleteMany({
where,
});
}