Files
pagodera/lib/prisma/queries/items.ts
T
Evandro Fonseca Junior 7f42def478 feat(market): add Minha Intenções tab with panel, hook, and badge
- Introduced "Minhas Intenções" tab visible only to logged-in users.
- Integrated MyPurchaseIntentionsPanel and useMyPurchaseIntentions hook.
- Added tab badge showing count of pending intentions.
- Extended session API to include user’s main character for downstream features.
2025-11-07 17:25:51 -03:00

156 lines
2.9 KiB
TypeScript

/**
* Queries de itens usando Prisma
* Busca itens da tabela unificada items (PostgreSQL)
*/
import { prisma } from "../app";
import { Prisma } from "@prisma/client";
export interface Item {
item_id: number;
source_table_id: number;
source_table_name: string;
item_name: string;
category: string;
image_local_path: string | null;
image_url: string | null;
rarity: string | null;
level: number | null;
weight: number | null;
slots: number | null;
skill: string | null;
metadata: any;
created_at: Date;
updated_at: Date;
}
export interface SearchItemsParams {
q?: string;
category?: string;
rarity?: string;
page?: number;
pageSize?: number;
}
/**
* Busca um item por ID unificado
*/
export async function getItemById(itemId: number): Promise<Item | null> {
const item = await prisma.item.findUnique({
where: { item_id: itemId },
});
if (!item) {
return null;
}
return {
...item,
weight: item.weight ? Number(item.weight) : null,
metadata: item.metadata || {},
} as Item;
}
/**
* Busca um item por nome (exato ou similar)
*/
export async function getItemByName(itemName: string): Promise<Item | null> {
const item = await prisma.item.findFirst({
where: {
item_name: {
equals: itemName,
mode: "insensitive",
},
},
});
if (!item) {
return null;
}
return {
...item,
weight: item.weight ? Number(item.weight) : null,
metadata: item.metadata || {},
} as Item;
}
/**
* Busca itens com filtros
*/
export async function searchItems(
params: SearchItemsParams = {}
): Promise<{ items: Item[]; total: number }> {
const {
q = "",
category,
rarity,
page = 1,
pageSize = 24,
} = params;
const where: Prisma.ItemWhereInput = {};
if (q) {
where.item_name = {
contains: q,
mode: "insensitive",
};
}
if (category) {
where.category = category;
}
if (rarity) {
where.rarity = rarity;
}
const total = await prisma.item.count({ where });
const items = await prisma.item.findMany({
where,
orderBy: { item_name: "asc" },
skip: (page - 1) * pageSize,
take: pageSize,
});
return {
items: items.map((item: any) => ({
...item,
weight: item.weight ? Number(item.weight) : null,
metadata: item.metadata || {},
})) as Item[],
total,
};
}
/**
* Busca item por source_table_name e source_table_id
* (útil para migração ou compatibilidade com IDs antigos)
*/
export async function getItemBySource(
sourceTableName: string,
sourceTableId: number
): Promise<Item | null> {
const item = await prisma.item.findUnique({
where: {
source_table_name_source_table_id: {
source_table_name: sourceTableName,
source_table_id: sourceTableId,
},
},
});
if (!item) {
return null;
}
return {
...item,
weight: item.weight ? Number(item.weight) : null,
metadata: item.metadata || {},
} as Item;
}