fix: sincronização silenciosa (background) para manter aba financeira aberta

This commit is contained in:
Sidney 2026-05-08 14:20:41 -03:00
parent 657f7f39ae
commit aadcf02cf2
2 changed files with 23 additions and 36 deletions

View File

@ -188,44 +188,15 @@ const Finance: React.FC<FinanceProps> = ({ data, updateData }) => {
const syncResult = await syncResp.json(); const syncResult = await syncResp.json();
if (syncResult.success && syncResult.updatedCount > 0) { if (syncResult.success && syncResult.updatedCount > 0) {
showAlert('Sincronização Ativa', `${syncResult.updatedCount} pagamentos foram atualizados diretamente do Asaas. A página será atualizada.`, 'success'); showAlert('Sincronização Ativa', `${syncResult.updatedCount} pagamentos foram atualizados diretamente do Asaas.`, 'success');
setTimeout(() => window.location.reload(), 2000); if (syncResult.data) {
updateData(syncResult.data);
}
return; return;
} else if (syncResult.success) { } else if (syncResult.success) {
console.log('[Sync] Tudo atualizado com o Asaas.'); console.log('[Sync] Tudo atualizado com o Asaas.');
} }
if (cloudPayments && cloudPayments.length > 0) {
let updatedCount = 0;
const currentPayments = dataPaymentsRef.current;
const updatedPayments = currentPayments.map(p => {
const match = cloudPayments.find((cp: any) => cp.asaas_payment_id === p.asaasPaymentId);
if (match) {
const statusStr = (match.status || '').toLowerCase();
const newStatus = statusStr === 'pago' ? 'paid' :
statusStr === 'atrasado' ? 'overdue' :
statusStr === 'cancelado' ? 'cancelled' : 'pending';
if (p.status !== newStatus) {
updatedCount++;
return {
...p,
status: newStatus as any,
amount: Number(match.valor),
paidDate: match.data_pagamento || p.paidDate,
asaasPaymentUrl: match.link_boleto || p.asaasPaymentUrl,
asaasPaymentId: match.asaas_payment_id || p.asaasPaymentId
};
}
}
return p;
});
if (updatedCount > 0) {
updateData({ payments: updatedPayments });
}
}
} catch (error) { } catch (error) {
console.error('Erro ao sincronizar pagamentos:', error); console.error('Erro ao sincronizar pagamentos:', error);
} finally { } finally {

View File

@ -1399,11 +1399,26 @@ async function syncPaymentsWithAsaasAPI() {
const pIdx = appData.payments.findIndex(p => p.asaasPaymentId === payment.id); const pIdx = appData.payments.findIndex(p => p.asaasPaymentId === payment.id);
if (pIdx !== -1) { if (pIdx !== -1) {
const newStatus = jsonStatusMap[internalStatus]; const newStatus = jsonStatusMap[internalStatus];
let changed = false;
if (appData.payments[pIdx].status !== newStatus) { if (appData.payments[pIdx].status !== newStatus) {
appData.payments[pIdx].status = newStatus; appData.payments[pIdx].status = newStatus;
appData.payments[pIdx].paidDate = payment.confirmedDate || payment.paymentDate || appData.payments[pIdx].paidDate; changed = true;
totalUpdated++;
} }
// SEMPRE atualiza o valor e a data para garantir fidelidade ao Asaas
if (appData.payments[pIdx].amount !== valorNum) {
appData.payments[pIdx].amount = valorNum;
changed = true;
}
const newPaidDate = payment.confirmedDate || payment.paymentDate;
if (newPaidDate && appData.payments[pIdx].paidDate !== newPaidDate) {
appData.payments[pIdx].paidDate = newPaidDate;
changed = true;
}
if (changed) totalUpdated++;
} }
} }
@ -1571,7 +1586,8 @@ async function startServer() {
app.post('/api/admin/sync-asaas-full', async (req, res) => { app.post('/api/admin/sync-asaas-full', async (req, res) => {
try { try {
const updatedCount = await syncPaymentsWithAsaasAPI(); const updatedCount = await syncPaymentsWithAsaasAPI();
res.json({ success: true, updatedCount }); const appData = await getSchoolData(); // Busca o JSON já atualizado
res.json({ success: true, updatedCount, data: appData });
} catch (e) { } catch (e) {
console.error('[Asaas:FullSync] Erro:', e.message); console.error('[Asaas:FullSync] Erro:', e.message);
res.status(500).json({ error: e.message }); res.status(500).json({ error: e.message });