feat: implement market page with QueryProvider and dynamic listings
- wrap root layout with QueryProvider for React Query - replace market UnderConstruction placeholder with MarketClient and real data fetching - implement search, pagination, and sort via search params using getActiveListings - add necessary client components and queries - update dependencies (e.g., prisma, pkg-types)
This commit is contained in:
1 parent
55eebde0ba
commit
ab37b477df
106 files changed
+15261
-301
No files matched your search
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* Queries de transactions usando Prisma
|
||||
* Migrado de lib/db/queries/transactions.ts
|
||||
*/
|
||||
|
||||
import { prisma } from "../app";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
export type TransactionStatus =
|
||||
| "pending"
|
||||
| "deposited"
|
||||
| "item_delivered"
|
||||
| "completed"
|
||||
| "disputed"
|
||||
| "cancelled"
|
||||
| "refunded";
|
||||
|
||||
export interface Transaction {
|
||||
transaction_id: number;
|
||||
listing_id: number | null;
|
||||
buyer_id: number;
|
||||
seller_id: number;
|
||||
item_name: string;
|
||||
quantity: number;
|
||||
server_name: string;
|
||||
amount: number;
|
||||
payment_method: "pix" | "tibia_coins" | "gold_coins" | null;
|
||||
escrow_fee: number | null;
|
||||
transaction_status: TransactionStatus;
|
||||
created_at: Date;
|
||||
deposited_at: Date | null;
|
||||
delivered_at: Date | null;
|
||||
completed_at: Date | null;
|
||||
buyer_confirmed: boolean;
|
||||
seller_confirmed: boolean;
|
||||
buyer_notes: string | null;
|
||||
seller_notes: string | null;
|
||||
}
|
||||
|
||||
export interface CreateTransactionData {
|
||||
listing_id?: number;
|
||||
buyer_id: number;
|
||||
seller_id: number;
|
||||
item_name: string;
|
||||
quantity: number;
|
||||
server_name: string;
|
||||
amount: number;
|
||||
payment_method: "pix" | "tibia_coins" | "gold_coins";
|
||||
escrow_fee?: number;
|
||||
}
|
||||
|
||||
export interface UpdateTransactionStatusData {
|
||||
transaction_status: TransactionStatus;
|
||||
deposited_at?: Date;
|
||||
delivered_at?: Date;
|
||||
completed_at?: Date;
|
||||
buyer_confirmed?: boolean;
|
||||
seller_confirmed?: boolean;
|
||||
buyer_notes?: string;
|
||||
seller_notes?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cria uma nova transação
|
||||
*/
|
||||
export async function createTransaction(
|
||||
data: CreateTransactionData
|
||||
): Promise<Transaction> {
|
||||
// Calcular escrow_fee se não fornecido (2% do valor)
|
||||
const escrowFee = data.escrow_fee ?? Number((data.amount * 0.02).toFixed(2));
|
||||
|
||||
return await prisma.transaction.create({
|
||||
data: {
|
||||
listing_id: data.listing_id || null,
|
||||
buyer_id: data.buyer_id,
|
||||
seller_id: data.seller_id,
|
||||
item_name: data.item_name,
|
||||
quantity: data.quantity,
|
||||
server_name: data.server_name,
|
||||
amount: data.amount,
|
||||
payment_method: data.payment_method,
|
||||
escrow_fee: escrowFee,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Busca transação por ID
|
||||
*/
|
||||
export async function getTransactionById(
|
||||
transactionId: number
|
||||
): Promise<Transaction | null> {
|
||||
return await prisma.transaction.findUnique({
|
||||
where: { transaction_id: transactionId },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Atualiza status da transação
|
||||
*/
|
||||
export async function updateTransactionStatus(
|
||||
transactionId: number,
|
||||
data: UpdateTransactionStatusData
|
||||
): Promise<Transaction | null> {
|
||||
const updateData: Prisma.TransactionUpdateInput = {
|
||||
transaction_status: data.transaction_status,
|
||||
};
|
||||
|
||||
if (data.deposited_at !== undefined) updateData.deposited_at = data.deposited_at;
|
||||
if (data.delivered_at !== undefined) updateData.delivered_at = data.delivered_at;
|
||||
if (data.completed_at !== undefined) updateData.completed_at = data.completed_at;
|
||||
if (data.buyer_confirmed !== undefined) updateData.buyer_confirmed = data.buyer_confirmed;
|
||||
if (data.seller_confirmed !== undefined) updateData.seller_confirmed = data.seller_confirmed;
|
||||
if (data.buyer_notes !== undefined) updateData.buyer_notes = data.buyer_notes;
|
||||
if (data.seller_notes !== undefined) updateData.seller_notes = data.seller_notes;
|
||||
|
||||
return await prisma.transaction.update({
|
||||
where: { transaction_id: transactionId },
|
||||
data: updateData,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Busca transações de um usuário (comprador ou vendedor)
|
||||
*/
|
||||
export async function getTransactionsByUser(
|
||||
userId: number,
|
||||
filters: {
|
||||
role?: "buyer" | "seller" | "both";
|
||||
status?: TransactionStatus;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
} = {}
|
||||
): Promise<{ transactions: Transaction[]; total: number }> {
|
||||
const { role = "both", status, page = 1, pageSize = 20 } = filters;
|
||||
|
||||
const where: Prisma.TransactionWhereInput = {};
|
||||
|
||||
if (role === "buyer") {
|
||||
where.buyer_id = userId;
|
||||
} else if (role === "seller") {
|
||||
where.seller_id = userId;
|
||||
} else {
|
||||
where.OR = [
|
||||
{ buyer_id: userId },
|
||||
{ seller_id: userId },
|
||||
];
|
||||
}
|
||||
|
||||
if (status) {
|
||||
where.transaction_status = status;
|
||||
}
|
||||
|
||||
// Contar total
|
||||
const total = await prisma.transaction.count({ where });
|
||||
|
||||
// Buscar transações
|
||||
const transactions = await prisma.transaction.findMany({
|
||||
where,
|
||||
orderBy: { created_at: "desc" },
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
});
|
||||
|
||||
return {
|
||||
transactions,
|
||||
total,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user