feat(router): gestisce versioni duplicate mantenendo solo la più alta nello smistamento
This commit is contained in:
@@ -3,7 +3,44 @@ const path = require('path');
|
||||
const { getDestinationDecision, getCadInfo } = require('./router');
|
||||
const { buildDestinationIndex } = require('./destinationIndex');
|
||||
const { buildExistingCadKeyIndex, toCadKey } = require('./duplicateIndex');
|
||||
const { getUnroutedTarget, getDuplicateTarget } = require('./unrouted');
|
||||
const { prepareUnroutedTarget, prepareDuplicateTarget } = require('./unrouted');
|
||||
|
||||
function parseNumericVersion(version) {
|
||||
const rawVersion = String(version || '').trim();
|
||||
if (!rawVersion || !/^\d+$/.test(rawVersion)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return BigInt(rawVersion);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function buildHighestNumericVersionByCadKey(files) {
|
||||
const index = new Map();
|
||||
|
||||
for (const row of files) {
|
||||
const cadInfo = getCadInfo(row.fileName);
|
||||
if (!cadInfo) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const version = parseNumericVersion(cadInfo.version);
|
||||
if (version === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const key = toCadKey(cadInfo);
|
||||
const current = index.get(key);
|
||||
if (current === undefined || version > current) {
|
||||
index.set(key, version);
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
async function collectFilesRecursively(rootDir) {
|
||||
const files = [];
|
||||
@@ -33,6 +70,7 @@ async function processFolder(folder, config) {
|
||||
const files = await collectFilesRecursively(folder);
|
||||
const destinationIndex = await buildDestinationIndex(config?.destination);
|
||||
const existingCadKeys = await buildExistingCadKeyIndex(config?.destination);
|
||||
const sourceMaxVersions = buildHighestNumericVersionByCadKey(files);
|
||||
const result = {
|
||||
scanned: 0,
|
||||
copied: 0,
|
||||
@@ -53,7 +91,26 @@ async function processFolder(folder, config) {
|
||||
}
|
||||
|
||||
if (existingCadKeys.has(toCadKey(cadInfo))) {
|
||||
const duplicateTarget = await getDuplicateTarget(file);
|
||||
const incomingVersion = parseNumericVersion(cadInfo.version);
|
||||
const highestInSource = sourceMaxVersions.get(toCadKey(cadInfo));
|
||||
if (incomingVersion !== null && highestInSource !== undefined && incomingVersion < highestInSource) {
|
||||
result.details.push({
|
||||
file,
|
||||
reason: 'Versione piu alta presente nei file da smistare',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const duplicateTarget = await prepareDuplicateTarget(file);
|
||||
if (!duplicateTarget.shouldCopy) {
|
||||
result.details.push({
|
||||
file,
|
||||
destination: duplicateTarget.destinationDir,
|
||||
reason: duplicateTarget.reason || 'Versione piu alta gia presente',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
await fs.copy(fullPath, duplicateTarget.destinationPath, { overwrite: false });
|
||||
|
||||
result.copied += 1;
|
||||
@@ -70,7 +127,16 @@ async function processFolder(folder, config) {
|
||||
const destDir = decision.destination;
|
||||
|
||||
if (!destDir) {
|
||||
const unroutedTarget = await getUnroutedTarget(file);
|
||||
const unroutedTarget = await prepareUnroutedTarget(file);
|
||||
if (!unroutedTarget.shouldCopy) {
|
||||
result.details.push({
|
||||
file,
|
||||
destination: unroutedTarget.destinationDir,
|
||||
reason: unroutedTarget.reason || 'Versione piu alta gia presente',
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
await fs.copy(fullPath, unroutedTarget.destinationPath, { overwrite: false });
|
||||
|
||||
result.copied += 1;
|
||||
|
||||
Reference in New Issue
Block a user