Files
cad-data-router/services/router.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

2026-03-05 14:45:06 +01:00
const path = require('path');
function normalizeCadType(ext) {
const lowerExt = String(ext || '').toLowerCase();
return lowerExt === 'drw' ? 'dwr' : lowerExt;
}
function getCadInfo(filename) {
const baseName = path.basename(filename);
const match = baseName.match(/^(.*)\.(prt|asm|drw|dwr)(?:\.([^.]+))?$/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;
return {
code: rawCode,
digits,
type,
version,
key: `${rawCode}.${type}`,
routingGroup,
};
}
function getDestinationDecision(filename, config, destinationIndex) {
const cadInfo = getCadInfo(filename);
if (!cadInfo) {
return { destination: null, reason: 'Nessuna regola trovata' };
}
2026-03-05 14:45:06 +01:00
if (!cadInfo.routingGroup) {
return {
destination: null,
reason: 'Nome file non conforme: servono almeno 5 cifre nel nome per ricavare la 3a-4a-5a cifra',
};
}
2026-03-05 14:45:06 +01:00
const group = cadInfo.routingGroup;
const candidates = destinationIndex?.get(group) || [];
2026-03-05 14:45:06 +01:00
if (!candidates.length) {
return {
destination: null,
reason: `Sottocartella ${group} non trovata nella destinazione`,
};
2026-03-05 14:45:06 +01:00
}
if (candidates.length > 1) {
return {
destination: null,
reason: `Sottocartella ${group} trovata in ${candidates.length} percorsi diversi`,
};
}
return { destination: candidates[0], reason: null };
}
function findDestination(filename, config, destinationIndex) {
return getDestinationDecision(filename, config, destinationIndex).destination;
2026-03-05 14:45:06 +01:00
}
function isCadFile(filename) {
return Boolean(getCadInfo(filename));
2026-03-05 14:45:06 +01:00
}
module.exports = { findDestination, getDestinationDecision, isCadFile, getCadInfo };