Files
pagodera/lib/prisma/queries/items.ts
T
Evandro Fonseca Junior d8e7d5d7e6 feat: enhance purchase intentions with chat integration and offer tracking
- Added MongoDB chat/message integration for seller responses
- Improved seller response validation and default messaging
- Integrated automatic chat creation/update when responding to intentions
- Added highest custom offer fields to listing queries and models
- Enhanced listing data with top offer information for buyers
2025-11-08 11:51:21 -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;
}