From 8d29449666959b3ef39a9f987392890a42d3d489 Mon Sep 17 00:00:00 2001 From: Sidney Date: Fri, 15 May 2026 07:49:53 -0300 Subject: [PATCH] Fix: synchronize server.js portal logic with server.selfhosted.js to include db.amount_original and db.discount in double discount recovery --- manager/scratch/check_row.js | 27 +++++++++++++++++++++++++++ portal/server.js | 4 ++-- portal/server.selfhosted.js | 4 ++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 manager/scratch/check_row.js diff --git a/manager/scratch/check_row.js b/manager/scratch/check_row.js new file mode 100644 index 0000000..127eac3 --- /dev/null +++ b/manager/scratch/check_row.js @@ -0,0 +1,27 @@ +const db = { + amount_original: '150.00', + valor: '150.00', + discount: '20.00' +}; + +const jsonP = { + amount: 150, // Assuming webhook corrupted it + discount: 20 +}; + +let amountOriginal = Number(db.amount_original) || Number(jsonP.amount) || Number(db.valor) || 0; +const discount = Number(db.discount) || (jsonP.amount ? Number(jsonP.discount || 0) : 0); + +console.log('amountOriginal before:', amountOriginal); +console.log('discount:', discount); +console.log('db.valor:', Number(db.valor)); + +if (amountOriginal === Number(db.valor) && discount > 0) { + amountOriginal += discount; + console.log('BUGFIX APPLIED! New amountOriginal:', amountOriginal); +} else { + console.log('BUGFIX NOT APPLIED. Conditions:', { + isEqual: amountOriginal === Number(db.valor), + hasDiscount: discount > 0 + }); +} diff --git a/portal/server.js b/portal/server.js index ca6d7d3..aacd618 100644 --- a/portal/server.js +++ b/portal/server.js @@ -275,8 +275,8 @@ app.get('/api/portal/financeiro', authMiddleware, async (req, res) => { } } - let amountOriginal = Number(jsonP.amount) || Number(db.valor) || 0; - const discount = jsonP.amount ? Number(jsonP.discount || 0) : 0; + let amountOriginal = Number(db.amount_original) || Number(jsonP.amount) || Number(db.valor) || 0; + const discount = Number(db.discount) || (jsonP.amount ? Number(jsonP.discount || 0) : 0); // [Bugfix]: Recupera o valor bruto corrompido pelo webhook antigo if (amountOriginal === Number(db.valor) && discount > 0) { diff --git a/portal/server.selfhosted.js b/portal/server.selfhosted.js index 13700a4..560da3b 100644 --- a/portal/server.selfhosted.js +++ b/portal/server.selfhosted.js @@ -286,7 +286,11 @@ app.get('/api/portal/financeiro', authMiddleware, async (req, res) => { // significa que o webhook antigo sobrescreveu o valor bruto pelo líquido no JSON. // Neste caso, o valor bruto real é o líquido + desconto. if (amountOriginal === Number(db.valor) && discount > 0) { + console.log(`[BUGFIX] Recovering amount for ${asaasId}: amountOriginal=${amountOriginal}, db.valor=${db.valor}, discount=${discount}. New amount: ${amountOriginal + discount}`); amountOriginal += discount; + } else if (asaasId === 'pay_iipssljwa9df3fsq' || asaasId === 'pay_krkf6cinlekjvw3l') { + // Log para debug se a condição falhar para os pagamentos conhecidos + console.log(`[DEBUG_FAIL] Bugfix failed for ${asaasId}: amountOriginal=${amountOriginal}, db.valor=${db.valor}, discount=${discount}`); } finalPayments.push({