219 lines
5.8 KiB
TypeScript
219 lines
5.8 KiB
TypeScript
/**
|
|
* Queries de transactions usando Prisma
|
|
* Migrado de lib/db/queries/transactions.ts
|
|
*/
|
|
|
|
import type { Prisma } from "@prisma/client";
|
|
import { prisma } from "../app";
|
|
|
|
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));
|
|
|
|
const transaction = 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,
|
|
},
|
|
});
|
|
return {
|
|
...transaction,
|
|
amount: Number(transaction.amount),
|
|
escrow_fee: transaction.escrow_fee ? Number(transaction.escrow_fee) : null,
|
|
transaction_status: transaction.transaction_status as TransactionStatus,
|
|
payment_method: transaction.payment_method as
|
|
| "pix"
|
|
| "tibia_coins"
|
|
| "gold_coins"
|
|
| null,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Busca transação por ID
|
|
*/
|
|
export async function getTransactionById(
|
|
transactionId: number,
|
|
): Promise<Transaction | null> {
|
|
const transaction = await prisma.transaction.findUnique({
|
|
where: { transaction_id: transactionId },
|
|
});
|
|
if (!transaction) return null;
|
|
return {
|
|
...transaction,
|
|
amount: Number(transaction.amount),
|
|
escrow_fee: transaction.escrow_fee ? Number(transaction.escrow_fee) : null,
|
|
transaction_status: transaction.transaction_status as TransactionStatus,
|
|
payment_method: transaction.payment_method as
|
|
| "pix"
|
|
| "tibia_coins"
|
|
| "gold_coins"
|
|
| null,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
|
|
const transaction = await prisma.transaction.update({
|
|
where: { transaction_id: transactionId },
|
|
data: updateData,
|
|
});
|
|
return {
|
|
...transaction,
|
|
amount: Number(transaction.amount),
|
|
escrow_fee: transaction.escrow_fee ? Number(transaction.escrow_fee) : null,
|
|
transaction_status: transaction.transaction_status as TransactionStatus,
|
|
payment_method: transaction.payment_method as
|
|
| "pix"
|
|
| "tibia_coins"
|
|
| "gold_coins"
|
|
| null,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 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: transactions.map((transaction) => ({
|
|
...transaction,
|
|
amount: Number(transaction.amount),
|
|
escrow_fee: transaction.escrow_fee
|
|
? Number(transaction.escrow_fee)
|
|
: null,
|
|
transaction_status: transaction.transaction_status as TransactionStatus,
|
|
payment_method: transaction.payment_method as
|
|
| "pix"
|
|
| "tibia_coins"
|
|
| "gold_coins"
|
|
| null,
|
|
})),
|
|
total,
|
|
};
|
|
}
|