From bbc8690a0b78fda0e47d61bd290bdd7c7fc81826 Mon Sep 17 00:00:00 2001 From: Kenearos Date: Tue, 7 Jul 2026 11:41:39 +0200 Subject: [PATCH] refactor(image-import): extract resolveEntryName, dedupe resolution groupEntriesByResolvedEmployee and resolveImports carried an identical resolvedNames -> ignore/new/assign branch. One helper returning {resolved, isNew}; callers keep their own null/newEmployee handling. Co-Authored-By: Claude Opus 4.8 (1M context) --- image-import.js | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/image-import.js b/image-import.js index b4eaf6b..a76025b 100644 --- a/image-import.js +++ b/image-import.js @@ -556,6 +556,22 @@ class ImageImporter { } } + /** + * Resolve one extracted entry name to a target employee, using confirmed + * matches first, then the unknown-choice map. + * @returns {{ resolved: string|null, isNew: boolean }} resolved=null means "ignore" + */ + resolveEntryName(session, name, choiceByCandidate) { + if (session.resolvedNames.has(name)) { + return { resolved: session.resolvedNames.get(name), isNew: false }; + } + const choice = choiceByCandidate.get(name) || 'new'; + if (choice === 'ignore') return { resolved: null, isNew: false }; + if (choice === 'new') return { resolved: name, isNew: true }; + if (choice.startsWith('assign:')) return { resolved: choice.slice('assign:'.length), isNew: false }; + return { resolved: name, isNew: false }; + } + /** * Build Map based on session.entries * and unknowns choices. @@ -569,16 +585,7 @@ class ImageImporter { const grouped = new Map(); for (let i = 0; i < this.session.entries.length; i++) { const e = this.session.entries[i]; - let resolved; - if (this.session.resolvedNames.has(e.name)) { - resolved = this.session.resolvedNames.get(e.name); - } else { - const choice = choiceByCandidate.get(e.name) || 'new'; - if (choice === 'ignore') resolved = null; - else if (choice === 'new') resolved = e.name; - else if (choice.startsWith('assign:')) resolved = choice.slice('assign:'.length); - else resolved = e.name; - } + const { resolved } = this.resolveEntryName(this.session, e.name, choiceByCandidate); if (!grouped.has(resolved)) grouped.set(resolved, []); grouped.get(resolved).push({ entry: e, index: i }); } @@ -723,21 +730,9 @@ class ImageImporter { let skippedOutsideMonth = 0; for (const e of session.entries) { - let resolved; - if (session.resolvedNames.has(e.name)) { - resolved = session.resolvedNames.get(e.name); - } else { - const choice = choiceByCandidate.get(e.name) || 'new'; - if (choice === 'ignore') continue; - if (choice === 'new') { - resolved = e.name; - newEmployees.add(e.name); - } else if (choice.startsWith('assign:')) { - resolved = choice.slice('assign:'.length); - } else { - resolved = e.name; - } - } + const { resolved, isNew } = this.resolveEntryName(session, e.name, choiceByCandidate); + if (resolved === null) continue; + if (isNew) newEmployees.add(e.name); const y = e.date.getFullYear(); const m = e.date.getMonth() + 1;