Amélioration de l'import
This commit is contained in:
@@ -344,6 +344,43 @@ router.post('/apply', (req, res, next) => {
|
||||
} catch (e) { next(e); }
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/imports/template
|
||||
* Génère, à partir du fichier déjà analysé (preview) et du mappage/valeurs par défaut choisis,
|
||||
* un jeu de données complet : toutes les colonnes du module cible (obligatoires + optionnelles),
|
||||
* y compris celles qui n'ont pas de colonne source dans le fichier (laissées vides). L'utilisateur
|
||||
* peut ainsi compléter/enrichir ce fichier avant de le réimporter.
|
||||
*/
|
||||
router.post('/template', (req, res, next) => {
|
||||
try {
|
||||
const { tempId, module, mapping = {}, defaults = {} } = req.body || {};
|
||||
if (!tempId || !module) throw new HttpError(400, 'tempId et module sont requis');
|
||||
const def = MODULES[module];
|
||||
if (!def) throw new HttpError(400, 'Unknown module');
|
||||
|
||||
const tempPath = path.join(UPLOAD_DIR, tempId);
|
||||
if (!fs.existsSync(tempPath)) throw new HttpError(404, 'Uploaded file expired');
|
||||
|
||||
const origName = req.body.originalFilename || '';
|
||||
const { rows } = parseFile(tempPath, origName || 'file.xlsx');
|
||||
|
||||
const allTargets = [...def.requiredTargets, ...def.optionalTargets];
|
||||
const v = (row, target) => {
|
||||
const col = mapping[target];
|
||||
if (col && row[col] !== undefined && row[col] !== null && row[col] !== '') return row[col];
|
||||
return defaults[target] !== undefined ? defaults[target] : '';
|
||||
};
|
||||
|
||||
const templateRows = rows.map(row => {
|
||||
const out = {};
|
||||
for (const t of allTargets) out[t] = v(row, t);
|
||||
return out;
|
||||
});
|
||||
|
||||
res.json({ headers: allTargets, rows: templateRows });
|
||||
} catch (e) { next(e); }
|
||||
});
|
||||
|
||||
/**
|
||||
* POST /api/imports/dossier
|
||||
* Importe un dossier investissement complet (format d'export natif).
|
||||
|
||||
Reference in New Issue
Block a user