Files
cad-data-router/services/router.js
Davide Grilli 65b1777a60 feat(router): instrada in sottocartella A/M_B e ottimizza cache destinazione
- Router: aggiunge campo `subfolder` a getCadInfo (91xxxxx → A, 92xxxxx → M_B);
  getDestinationDecision appende la sottocartella al percorso finale
- Scanner (fast path): legge A e M_B dentro ogni cartella a 3 cifre per
  il rilevamento duplicati, anziché la cartella a 3 cifre stessa
- Cache: persistenza su disco del folderIndex; cadKeyIndex sempre fresco
- isCacheStillValid: validazione a due livelli (top-level + cartelle a 3
  cifre) — rileva automaticamente nuove cartelle aggiunte da colleghi
- UI: rimosso pulsante "Riscansiona destinazione"; gestione cache completamente
  automatica e trasparente per l'utente
2026-03-16 16:17:32 +01:00

83 lines
2.1 KiB
JavaScript

const path = require('path');
function normalizeCadType(ext) {
return String(ext || '').toLowerCase();
}
function getCadInfo(filename) {
const baseName = path.basename(filename);
const match = baseName.match(/^(.*)\.(prt|asm|drw)(?:\.([^.]+))?$/i);
if (!match) {
return null;
}
const rawCode = match[1];
const type = normalizeCadType(match[2]);
const version = match[3] || '';
const digits = rawCode.replace(/\D/g, '');
const routingGroup = digits.length >= 5 ? digits.slice(2, 5) : null;
const subfolder = digits.length >= 2
? (digits[1] === '1' ? 'A' : digits[1] === '2' ? 'M_B' : null)
: null;
return {
code: rawCode,
digits,
type,
version,
key: `${rawCode}.${type}`,
routingGroup,
subfolder,
};
}
function getDestinationDecision(filename, config, destinationIndex) {
const cadInfo = getCadInfo(filename);
if (!cadInfo) {
return { destination: null, reason: 'Nessuna regola trovata' };
}
if (!cadInfo.routingGroup) {
return {
destination: null,
reason: 'Nome file non conforme: servono almeno 5 cifre nel nome per ricavare la 3a-4a-5a cifra',
};
}
const group = cadInfo.routingGroup;
const candidates = destinationIndex?.get(group) || [];
if (!candidates.length) {
return {
destination: null,
reason: `Sottocartella ${group} non trovata nella destinazione`,
};
}
if (candidates.length > 1) {
return {
destination: null,
reason: `Sottocartella ${group} trovata in ${candidates.length} percorsi diversi`,
};
}
if (!cadInfo.subfolder) {
return {
destination: null,
reason: `Prefisso non riconosciuto (${cadInfo.digits[1]}): atteso 91 (→ A) o 92 (→ M_B)`,
};
}
return { destination: path.join(candidates[0], cadInfo.subfolder), reason: null };
}
function findDestination(filename, config, destinationIndex) {
return getDestinationDecision(filename, config, destinationIndex).destination;
}
function isCadFile(filename) {
return Boolean(getCadInfo(filename));
}
module.exports = { findDestination, getDestinationDecision, isCadFile, getCadInfo };