Corrections sur les jobs En retard

This commit is contained in:
2026-07-12 17:39:57 +02:00
parent 0d8f92bc9a
commit ec3c7f7cab
4 changed files with 225 additions and 42 deletions
+46 -5
View File
@@ -7,7 +7,7 @@ import { generateSimul } from '../utils/schedule.js';
const router = Router();
const TRACKED_FIELDS = [
export const TRACKED_FIELDS = [
{ key: 'type_remb', label: 'Type de prêt' },
{ key: 'taux_interet', label: 'Taux annuel (%)' },
{ key: 'duree_mois', label: 'Durée (mois)' },
@@ -20,7 +20,7 @@ const TRACKED_FIELDS = [
{ key: 'plateforme_id', label: 'Plateforme' },
];
function recordHistory(investissementId, { type_evenement, changements, notes }) {
export function recordHistory(investissementId, { type_evenement, changements, notes }) {
if (!changements || changements.length === 0) return;
db.prepare(`
INSERT INTO investissement_historique (investissement_id, type_evenement, changements, notes)
@@ -28,7 +28,7 @@ function recordHistory(investissementId, { type_evenement, changements, notes })
`).run(investissementId, type_evenement, JSON.stringify(changements), notes || null);
}
function detectChangements(ancien, nouveau) {
export function detectChangements(ancien, nouveau) {
const diffs = [];
for (const { key, label } of TRACKED_FIELDS) {
const av = ancien[key] ?? null;
@@ -42,7 +42,7 @@ function detectChangements(ancien, nouveau) {
return diffs;
}
function detectTypeEvenement(changements) {
export function detectTypeEvenement(changements) {
const champsRestructuration = ['type_remb', 'date_debut_simul'];
if (changements.some(c => champsRestructuration.includes(c.champ))) return 'restructuration';
return 'modification';
@@ -180,7 +180,9 @@ router.post('/fix-differe-dates', (req, res, next) => {
try {
const rows = db.prepare(`
SELECT i.id, i.nom_projet, i.date_souscription, i.duree_mois,
i.date_premiere_echeance, i.date_cible
i.date_premiere_echeance, i.date_cible,
i.montant_investi, i.taux_interet, i.type_remb, i.freq_interets,
i.date_debut_simul, i.echeance_fin_de_mois
FROM investissements i
JOIN investisseurs inv ON inv.id = i.investisseur_id AND inv.user_id = ?
WHERE i.type_remb = 'differe'
@@ -222,6 +224,45 @@ router.post('/fix-differe-dates', (req, res, next) => {
if (incoherent) {
stmt.run(dateCalculee, dateCalculee, inv.id);
const changements = [];
if (String(inv.date_premiere_echeance ?? null) !== String(dateCalculee)) {
changements.push({
champ: 'date_premiere_echeance',
label: 'Date 1ère échéance',
ancienne_valeur: inv.date_premiere_echeance,
nouvelle_valeur: dateCalculee,
});
}
if (String(inv.date_cible ?? null) !== String(dateCalculee)) {
changements.push({
champ: 'date_cible',
label: 'Date cible',
ancienne_valeur: inv.date_cible,
nouvelle_valeur: dateCalculee,
});
}
recordHistory(inv.id, {
type_evenement: 'correction_auto_dates',
changements,
notes: 'Correction automatique (Nettoyage > Corriger les dates des prêts différés) : écart > 2 ans avec date_souscription + duree_mois',
});
// Régénère l'échéancier de projection avec la date corrigée — sans ça,
// simul_remboursements reste calé sur l'ancienne date aberrante.
generateSimul(db, {
id: inv.id,
montant_investi: inv.montant_investi,
taux_interet: inv.taux_interet,
duree_mois: inv.duree_mois,
type_remb: inv.type_remb,
freq_interets: inv.freq_interets,
date_premiere_echeance: dateCalculee,
date_debut_simul: inv.date_debut_simul,
date_souscription: inv.date_souscription,
echeance_fin_de_mois: inv.echeance_fin_de_mois ?? 0,
});
corriges.push({
id: inv.id,
nom_projet: inv.nom_projet,