feat: enhance donation creation logging and adjust amount limits
- Add detailed secure logging throughout the donation creation API route for production debugging, including request receipt, validation results, amount calculations, PIX creation success, and error handling - Update custom donation amount schema to allow minimum 0.01 and maximum 100,000 for broader donation flexibility - Modify secure logger to always output data in production for improved monitoring and troubleshooting
This commit is contained in:
1 parent
d05fbe967e
commit
e30d9493ff
4 files changed
+58
-12
No files matched your search
@@ -21,6 +21,13 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
const body = await request.json();
|
||||
|
||||
// Log detalhado para debug em produção
|
||||
secureLogger.info("📥 POST /api/donations/create - Recebido", {
|
||||
ip,
|
||||
body: JSON.stringify(body),
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
// Validação com Zod
|
||||
const validationResult = createDonationSchema.safeParse(body);
|
||||
|
||||
@@ -30,6 +37,12 @@ export async function POST(request: NextRequest) {
|
||||
message: err.message,
|
||||
}));
|
||||
|
||||
secureLogger.error("❌ Validação falhou", {
|
||||
body: JSON.stringify(body),
|
||||
errors: JSON.stringify(errors),
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
@@ -42,6 +55,13 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
const validatedData = validationResult.data;
|
||||
|
||||
secureLogger.info("✅ Validação passou", {
|
||||
tier: validatedData.tier,
|
||||
anonymous: validatedData.anonymous,
|
||||
customAmount: validatedData.customAmount,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
const product =
|
||||
DONATION_PRODUCTS[validatedData.tier as keyof typeof DONATION_PRODUCTS];
|
||||
|
||||
@@ -55,9 +75,21 @@ export async function POST(request: NextRequest) {
|
||||
finalDescription = `Doação Personalizada - R$ ${validatedData.customAmount!.toFixed(
|
||||
2
|
||||
)}`;
|
||||
|
||||
secureLogger.info("💰 Valor customizado calculado", {
|
||||
valor_original: validatedData.customAmount,
|
||||
valor_centavos: finalAmount,
|
||||
description: finalDescription,
|
||||
});
|
||||
} else {
|
||||
finalAmount = product.price;
|
||||
finalDescription = product.name;
|
||||
|
||||
secureLogger.info("💰 Valor fixo", {
|
||||
tier: validatedData.tier,
|
||||
amount: product.amount,
|
||||
price_centavos: finalAmount,
|
||||
});
|
||||
}
|
||||
|
||||
let pixData;
|
||||
@@ -103,15 +135,27 @@ export async function POST(request: NextRequest) {
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
const response = {
|
||||
success: true,
|
||||
pixId: pixData.id,
|
||||
brCode: pixData.brCode,
|
||||
brCodeBase64: pixData.brCodeBase64,
|
||||
expiresAt: pixData.expiresAt,
|
||||
};
|
||||
|
||||
secureLogger.info("✅ PIX criado com sucesso", {
|
||||
pixId: pixData.id,
|
||||
expiresAt: pixData.expiresAt,
|
||||
});
|
||||
|
||||
return NextResponse.json(response);
|
||||
} catch (error) {
|
||||
secureLogger.error("Donation creation error", error);
|
||||
secureLogger.error("❌ Erro ao criar doação", {
|
||||
error: error instanceof Error ? error.message : "Erro desconhecido",
|
||||
stack: error instanceof Error ? error.stack : undefined,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
|
||||
Reference in new issue
Block a user