Export des détenteurs

This commit is contained in:
2026-06-16 09:35:22 +02:00
parent f097fc65f7
commit 720634971d
+31 -2
View File
@@ -340,7 +340,7 @@ router.get('/export', (req, res, next) => {
}
const entries = [];
entries.push({ name: 'manifest.json', data: JSON.stringify({ version: '1.0', app: 'crowdlending', exported_at: new Date().toISOString(), count: rows.length, type: 'plateformes' }, null, 2) });
entries.push({ name: 'manifest.json', data: JSON.stringify({ version: '1.1', app: 'crowdlending', exported_at: new Date().toISOString(), count: rows.length, type: 'plateformes', includes_investisseurs: true }, null, 2) });
const dataRows = rows.map(r => ({
nom: r.nom,
@@ -365,6 +365,15 @@ router.get('/export', (req, res, next) => {
entries.push({ name: 'data.json', data: JSON.stringify(dataRows, null, 2) });
// Export des investisseurs (détenteurs) liés aux plateformes
const invIds = [...new Set(rows.map(r => r.investisseur_id).filter(Boolean))];
const invRows = invIds.length > 0
? db.prepare(`SELECT nom, prenom, type, type_fiscal, notes FROM investisseurs WHERE id IN (${invIds.map(() => '?').join(',')}) AND user_id = ?`).all(...invIds, req.user.id)
: [];
if (invRows.length > 0) {
entries.push({ name: 'investisseurs.json', data: JSON.stringify(invRows, null, 2) });
}
for (const r of rows) {
for (const fname of [r.logo_filename, r.icone_filename]) {
if (!fname) continue;
@@ -419,6 +428,12 @@ router.get('/:id/export', (req, res, next) => {
};
entries.push({ name: 'data.json', data: JSON.stringify([dataRow], null, 2) });
// Export du détenteur lié à la plateforme
if (withAll.investisseur_id) {
const inv = db.prepare('SELECT nom, prenom, type, type_fiscal, notes FROM investisseurs WHERE id = ? AND user_id = ?').get(withAll.investisseur_id, req.user.id);
if (inv) entries.push({ name: 'investisseurs.json', data: JSON.stringify([inv], null, 2) });
}
for (const fname of [withAll.logo_filename, withAll.icone_filename]) {
if (!fname) continue;
const fpath = path.join(logosDir, fname);
@@ -444,7 +459,21 @@ router.post('/import-zip', zipUpload.single('file'), async (req, res, next) => {
const platforms = JSON.parse(dataEntry.data.toString('utf8'));
if (!Array.isArray(platforms)) throw new HttpError(400, 'data.json doit être un tableau');
// Résoudre investisseur par nom+prénom
// Importer les investisseurs du ZIP (créer les manquants)
const invEntry = zipEntries.find(e => e.name === 'investisseurs.json');
if (invEntry) {
const invList = JSON.parse(invEntry.data.toString('utf8'));
for (const inv of (Array.isArray(invList) ? invList : [])) {
if (!inv.nom) continue;
const exists = db.prepare('SELECT id FROM investisseurs WHERE nom = ? AND user_id = ?').get(inv.nom, req.user.id);
if (!exists) {
db.prepare('INSERT INTO investisseurs (user_id, nom, prenom, type, type_fiscal, notes) VALUES (?,?,?,?,?,?)')
.run(req.user.id, inv.nom, inv.prenom ?? null, inv.type || 'famille', inv.type_fiscal ?? null, inv.notes ?? null);
}
}
}
// Résoudre investisseur par nom+prénom (après création éventuelle)
const userInvestisseurs = db.prepare('SELECT * FROM investisseurs WHERE user_id = ?').all(req.user.id);
function resolveInvestisseur(nom, prenom) {
if (!nom) return userInvestisseurs[0]?.id ?? null;