style: clean up code formatting and remove trailing whitespace
refactor: reorganize imports and fix type imports
This commit is contained in:
1 parent
d3704602fd
commit
47c04de6fe
232 files changed
+7140
-4798
No files matched your search
+75
-35
@@ -4,9 +4,9 @@
|
||||
*/
|
||||
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "../app";
|
||||
import { ensureItemsSeeded } from "../../items/import-from-sqlite";
|
||||
import { secureLogger } from "../../secure-logger";
|
||||
import { prisma } from "../app";
|
||||
|
||||
export type ItemSort =
|
||||
| "name"
|
||||
@@ -96,13 +96,14 @@ function buildOrder(sort: ItemSort): Prisma.ItemOrderByWithRelationInput[] {
|
||||
return [{ category: "asc" }, { item_name: "asc" }];
|
||||
case "name":
|
||||
return [{ item_name: "asc" }];
|
||||
case "updated-desc":
|
||||
default:
|
||||
return [{ updated_at: "desc" }];
|
||||
}
|
||||
}
|
||||
|
||||
function parseMetadata(metadata: Prisma.JsonValue | null | undefined): Record<string, any> {
|
||||
function parseMetadata(
|
||||
metadata: Prisma.JsonValue | null | undefined,
|
||||
): Record<string, any> {
|
||||
if (!metadata || typeof metadata !== "object" || Array.isArray(metadata)) {
|
||||
return {};
|
||||
}
|
||||
@@ -142,8 +143,14 @@ function extractTags(meta: Record<string, any>): string[] {
|
||||
}
|
||||
|
||||
function buildItem(raw: any): Item {
|
||||
const createdAt = raw.created_at instanceof Date ? raw.created_at.toISOString() : raw.created_at;
|
||||
const updatedAt = raw.updated_at instanceof Date ? raw.updated_at.toISOString() : raw.updated_at;
|
||||
const createdAt =
|
||||
raw.created_at instanceof Date
|
||||
? raw.created_at.toISOString()
|
||||
: raw.created_at;
|
||||
const updatedAt =
|
||||
raw.updated_at instanceof Date
|
||||
? raw.updated_at.toISOString()
|
||||
: raw.updated_at;
|
||||
|
||||
return {
|
||||
...raw,
|
||||
@@ -158,7 +165,10 @@ function buildItemWithMeta(raw: any): ItemWithMeta {
|
||||
const base = buildItem(raw);
|
||||
const servers = extractServers(base.metadata);
|
||||
const tags = extractTags(base.metadata);
|
||||
const description = typeof base.metadata.description === "string" ? base.metadata.description : null;
|
||||
const description =
|
||||
typeof base.metadata.description === "string"
|
||||
? base.metadata.description
|
||||
: null;
|
||||
|
||||
return {
|
||||
...base,
|
||||
@@ -177,7 +187,12 @@ function toDecimal(value: number | null | undefined): Prisma.Decimal | null {
|
||||
|
||||
function buildMetadata(
|
||||
existing: Record<string, any>,
|
||||
overrides: { servers?: string[]; tags?: string[]; description?: string | null; metadata?: Record<string, any> }
|
||||
overrides: {
|
||||
servers?: string[];
|
||||
tags?: string[];
|
||||
description?: string | null;
|
||||
metadata?: Record<string, any>;
|
||||
},
|
||||
): Record<string, any> {
|
||||
const merged = { ...existing, ...(overrides.metadata || {}) };
|
||||
|
||||
@@ -220,10 +235,15 @@ const FORBIDDEN_VALUE_KEYS = new Set<string>([
|
||||
* Verifica recursivamente se há campos proibidos relacionados a valores/preços.
|
||||
* Lança erro se encontrar.
|
||||
*/
|
||||
export function assertNoValueFieldsInMetadata(meta: Record<string, any> | undefined | null, context?: string): void {
|
||||
export function assertNoValueFieldsInMetadata(
|
||||
meta: Record<string, any> | undefined | null,
|
||||
context?: string,
|
||||
): void {
|
||||
if (!meta || typeof meta !== "object") return;
|
||||
|
||||
const stack: Array<{ obj: Record<string, any>; path: string }> = [{ obj: meta as Record<string, any>, path: "metadata" }];
|
||||
const stack: Array<{ obj: Record<string, any>; path: string }> = [
|
||||
{ obj: meta as Record<string, any>, path: "metadata" },
|
||||
];
|
||||
while (stack.length) {
|
||||
const { obj, path } = stack.pop()!;
|
||||
for (const key of Object.keys(obj)) {
|
||||
@@ -264,7 +284,10 @@ export async function getItemByName(itemName: string): Promise<Item | null> {
|
||||
return buildItem(item);
|
||||
}
|
||||
|
||||
export async function getItemBySource(sourceTableName: string, sourceTableId: number): Promise<Item | null> {
|
||||
export async function getItemBySource(
|
||||
sourceTableName: string,
|
||||
sourceTableId: number,
|
||||
): Promise<Item | null> {
|
||||
const item = await prisma.item.findUnique({
|
||||
where: {
|
||||
source_table_name_source_table_id: {
|
||||
@@ -279,15 +302,10 @@ export async function getItemBySource(sourceTableName: string, sourceTableId: nu
|
||||
return buildItem(item);
|
||||
}
|
||||
|
||||
export async function searchAdminItems(params: AdminSearchParams = {}): Promise<AdminItemsResult> {
|
||||
const {
|
||||
q = "",
|
||||
category,
|
||||
rarity,
|
||||
server,
|
||||
page = 1,
|
||||
pageSize = 50,
|
||||
} = params;
|
||||
export async function searchAdminItems(
|
||||
params: AdminSearchParams = {},
|
||||
): Promise<AdminItemsResult> {
|
||||
const { q = "", category, rarity, server, page = 1, pageSize = 50 } = params;
|
||||
|
||||
await ensureItemsSeeded();
|
||||
|
||||
@@ -355,7 +373,9 @@ export async function searchAdminItems(params: AdminSearchParams = {}): Promise<
|
||||
total,
|
||||
page: safePage,
|
||||
pageSize: safePageSize,
|
||||
servers: Array.from(serversSet).sort((a, b) => a.localeCompare(b, "pt-BR", { sensitivity: "base" })),
|
||||
servers: Array.from(serversSet).sort((a, b) =>
|
||||
a.localeCompare(b, "pt-BR", { sensitivity: "base" }),
|
||||
),
|
||||
categories: categoryRows
|
||||
.map((row) => row.category)
|
||||
.filter((c): c is string => Boolean(c))
|
||||
@@ -363,12 +383,16 @@ export async function searchAdminItems(params: AdminSearchParams = {}): Promise<
|
||||
};
|
||||
}
|
||||
|
||||
export async function searchItems(params: SearchItemsParams = {}): Promise<{ items: ItemWithMeta[]; total: number }> {
|
||||
export async function searchItems(
|
||||
params: SearchItemsParams = {},
|
||||
): Promise<{ items: ItemWithMeta[]; total: number }> {
|
||||
const { items, total } = await searchAdminItems(params);
|
||||
return { items, total };
|
||||
}
|
||||
|
||||
export async function createAdminItem(input: ItemUpsertInput): Promise<ItemWithMeta> {
|
||||
export async function createAdminItem(
|
||||
input: ItemUpsertInput,
|
||||
): Promise<ItemWithMeta> {
|
||||
const now = Date.now();
|
||||
// Validação: impedir qualquer campo de valor em metadata (preço etc.)
|
||||
assertNoValueFieldsInMetadata(input.metadata, "createAdminItem");
|
||||
@@ -384,19 +408,25 @@ export async function createAdminItem(input: ItemUpsertInput): Promise<ItemWithM
|
||||
weight: toDecimal(input.weight),
|
||||
slots: input.slots ?? null,
|
||||
skill: input.skill ?? null,
|
||||
metadata: buildMetadata({}, {
|
||||
servers: input.servers ?? [],
|
||||
tags: input.tags ?? [],
|
||||
description: input.description ?? null,
|
||||
metadata: input.metadata ?? {},
|
||||
}),
|
||||
metadata: buildMetadata(
|
||||
{},
|
||||
{
|
||||
servers: input.servers ?? [],
|
||||
tags: input.tags ?? [],
|
||||
description: input.description ?? null,
|
||||
metadata: input.metadata ?? {},
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
const created = await prisma.item.create({ data });
|
||||
return buildItemWithMeta(created);
|
||||
}
|
||||
|
||||
export async function updateAdminItem(itemId: number, input: ItemUpsertInput): Promise<ItemWithMeta> {
|
||||
export async function updateAdminItem(
|
||||
itemId: number,
|
||||
input: ItemUpsertInput,
|
||||
): Promise<ItemWithMeta> {
|
||||
const existing = await prisma.item.findUnique({ where: { item_id: itemId } });
|
||||
if (!existing) {
|
||||
throw new Error("Item não encontrado");
|
||||
@@ -408,24 +438,34 @@ export async function updateAdminItem(itemId: number, input: ItemUpsertInput): P
|
||||
const metadata = buildMetadata(existingMeta, {
|
||||
servers: input.servers ?? extractServers(existingMeta),
|
||||
tags: input.tags ?? extractTags(existingMeta),
|
||||
description: input.description !== undefined ? input.description : existingMeta.description,
|
||||
description:
|
||||
input.description !== undefined
|
||||
? input.description
|
||||
: existingMeta.description,
|
||||
metadata: input.metadata,
|
||||
});
|
||||
|
||||
const data: Prisma.ItemUpdateInput = {
|
||||
item_name: input.item_name ?? existing.item_name,
|
||||
category: input.category ?? existing.category,
|
||||
image_local_path: input.image_local_path !== undefined ? input.image_local_path : existing.image_local_path,
|
||||
image_url: input.image_url !== undefined ? input.image_url : existing.image_url,
|
||||
image_local_path:
|
||||
input.image_local_path !== undefined
|
||||
? input.image_local_path
|
||||
: existing.image_local_path,
|
||||
image_url:
|
||||
input.image_url !== undefined ? input.image_url : existing.image_url,
|
||||
rarity: input.rarity !== undefined ? input.rarity : existing.rarity,
|
||||
level: input.level !== undefined ? input.level : existing.level,
|
||||
weight: input.weight !== undefined ? toDecimal(input.weight) : existing.weight,
|
||||
weight:
|
||||
input.weight !== undefined ? toDecimal(input.weight) : existing.weight,
|
||||
slots: input.slots !== undefined ? input.slots : existing.slots,
|
||||
skill: input.skill !== undefined ? input.skill : existing.skill,
|
||||
metadata,
|
||||
};
|
||||
|
||||
const updated = await prisma.item.update({ where: { item_id: itemId }, data });
|
||||
const updated = await prisma.item.update({
|
||||
where: { item_id: itemId },
|
||||
data,
|
||||
});
|
||||
return buildItemWithMeta(updated);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user