fix: sincronização em tempo real JSON <-> SQL e correção definitiva de valores no carnê

This commit is contained in:
Sidney 2026-05-08 10:41:54 -03:00
parent 214629adce
commit 839b3ca4eb
2 changed files with 34 additions and 2 deletions

View File

@ -433,7 +433,7 @@ const Finance: React.FC<FinanceProps> = ({ data, updateData }) => {
installmentId: id, installmentId: id,
payments: sorted, payments: sorted,
studentId: sorted[0].studentId, studentId: sorted[0].studentId,
totalAmount: sorted.reduce((sum, p) => sum + p.amount, 0), totalAmount: sorted.reduce((sum, p) => sum + Number(p.amount), 0),
totalInstallments: sorted[0].totalInstallments || sorted.length, totalInstallments: sorted[0].totalInstallments || sorted.length,
description: sorted[0].description?.split(' (')[0] || 'Parcelamento', description: sorted[0].description?.split(' (')[0] || 'Parcelamento',
dueDate: sorted[0].dueDate dueDate: sorted[0].dueDate

View File

@ -716,6 +716,33 @@ app.post('/api/webhook_asaas', async (req, res) => {
} }
await updateCobranca(asaasPaymentId, updateData); await updateCobranca(asaasPaymentId, updateData);
// Sincronização em tempo real com o JSON legado
try {
const appData = await getSchoolData();
const pIdx = appData.payments?.findIndex(p => p.asaasPaymentId === asaasPaymentId);
if (pIdx !== undefined && pIdx !== -1) {
const p = appData.payments[pIdx];
const statusStr = (updateData.status || '').toLowerCase();
const newStatus = statusStr === 'pago' ? 'paid' :
statusStr === 'atrasado' ? 'overdue' :
statusStr === 'cancelado' ? 'cancelled' : 'pending';
appData.payments[pIdx] = {
...p,
status: newStatus,
amount: updateData.valor || p.amount,
dueDate: updateData.vencimento || p.dueDate,
paidDate: updateData.data_pagamento || p.paidDate
};
appData.lastUpdated = new Date().toISOString();
await saveSchoolData(appData);
console.log(`[Webhook:Sync] JSON atualizado para boleto ${asaasPaymentId}`);
}
} catch (syncErr) {
console.error('[Webhook:Sync] Erro ao sincronizar JSON:', syncErr.message);
}
addLog('Webhook', `Sucesso ${payload.event}`, { asaasPaymentId }); addLog('Webhook', `Sucesso ${payload.event}`, { asaasPaymentId });
return res.status(200).json({ message: 'OK' }); return res.status(200).json({ message: 'OK' });
} catch (error) { } catch (error) {
@ -728,7 +755,12 @@ app.post('/api/webhook_asaas', async (req, res) => {
app.get('/api/admin/cobrancas', async (req, res) => { app.get('/api/admin/cobrancas', async (req, res) => {
try { try {
const result = await pool.query('SELECT * FROM alunos_cobrancas ORDER BY vencimento DESC'); const result = await pool.query('SELECT * FROM alunos_cobrancas ORDER BY vencimento DESC');
res.json(result.rows); // Garantir que valores numéricos sejam retornados como Number para evitar bugs de string no front
const rows = result.rows.map(r => ({
...r,
valor: Number(r.valor)
}));
res.json(rows);
} catch(e) { } catch(e) {
res.status(500).json({error: e.message}); res.status(500).json({error: e.message});
} }