fix: sincronização silenciosa (background) para manter aba financeira aberta
This commit is contained in:
parent
657f7f39ae
commit
aadcf02cf2
|
|
@ -188,44 +188,15 @@ const Finance: React.FC<FinanceProps> = ({ data, updateData }) => {
|
|||
const syncResult = await syncResp.json();
|
||||
|
||||
if (syncResult.success && syncResult.updatedCount > 0) {
|
||||
showAlert('Sincronização Ativa', `${syncResult.updatedCount} pagamentos foram atualizados diretamente do Asaas. A página será atualizada.`, 'success');
|
||||
setTimeout(() => window.location.reload(), 2000);
|
||||
showAlert('Sincronização Ativa', `${syncResult.updatedCount} pagamentos foram atualizados diretamente do Asaas.`, 'success');
|
||||
if (syncResult.data) {
|
||||
updateData(syncResult.data);
|
||||
}
|
||||
return;
|
||||
} else if (syncResult.success) {
|
||||
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) {
|
||||
console.error('Erro ao sincronizar pagamentos:', error);
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -1399,11 +1399,26 @@ async function syncPaymentsWithAsaasAPI() {
|
|||
const pIdx = appData.payments.findIndex(p => p.asaasPaymentId === payment.id);
|
||||
if (pIdx !== -1) {
|
||||
const newStatus = jsonStatusMap[internalStatus];
|
||||
let changed = false;
|
||||
|
||||
if (appData.payments[pIdx].status !== newStatus) {
|
||||
appData.payments[pIdx].status = newStatus;
|
||||
appData.payments[pIdx].paidDate = payment.confirmedDate || payment.paymentDate || appData.payments[pIdx].paidDate;
|
||||
totalUpdated++;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
// 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) => {
|
||||
try {
|
||||
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) {
|
||||
console.error('[Asaas:FullSync] Erro:', e.message);
|
||||
res.status(500).json({ error: e.message });
|
||||
|
|
|
|||
Loading…
Reference in New Issue