From 60ab2465aa8d26201098a296d0f805b50560a9ff Mon Sep 17 00:00:00 2001 From: Nils-Johan Gynther Date: Sat, 2 May 2026 22:51:17 +0200 Subject: [PATCH] fix(receipt-import): add hard bacon override to pork category --- .../receipt-import/receipt-import.service.ts | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/backend/src/receipt-import/receipt-import.service.ts b/backend/src/receipt-import/receipt-import.service.ts index 207704ce..993a430e 100644 --- a/backend/src/receipt-import/receipt-import.service.ts +++ b/backend/src/receipt-import/receipt-import.service.ts @@ -295,9 +295,13 @@ export class ReceiptImportService { ? this.applyContradictionGuard(signalText || item.rawName, nextSuggestion, categories) : null; + const finalSuggestion = guardedSuggestion + ? this.applyHardCategoryOverrides(signalText || item.rawName, guardedSuggestion, categories) + : null; + enriched.push( - guardedSuggestion - ? { ...item, categorySuggestion: guardedSuggestion } + finalSuggestion + ? { ...item, categorySuggestion: finalSuggestion } : item, ); } catch { @@ -309,6 +313,43 @@ export class ReceiptImportService { return enriched; } + private applyHardCategoryOverrides( + signalText: string, + suggestion: CategorySuggestion, + categories: Awaited>, + ): CategorySuggestion { + const normalized = normalizeForRules(signalText); + const hasBaconLikeSignal = + /\bbacon\b/.test(normalized) || + /\bbacn\b/.test(normalized) || + /\bbaco\b/.test(normalized) || + /\bbac[a-z]{1,3}\b/.test(normalized) || + /\bsidflask\b/.test(normalized) || + /\bpancetta\b/.test(normalized); + + if (!hasBaconLikeSignal) return suggestion; + + const l3Pork = categories.find( + (c) => + c.name.toLowerCase() === 'fläsk' && + c.path.toLowerCase().startsWith('kött, chark & fågel > kött > '), + ); + if (!l3Pork) return suggestion; + + if (suggestion.categoryId === l3Pork.id) return suggestion; + + this.logger.log( + `Hard-override: "${signalText}" remappas från "${suggestion.path}" till "${l3Pork.path}"`, + ); + return { + categoryId: l3Pork.id, + categoryName: l3Pork.name, + path: l3Pork.path, + confidence: 'high', + usedFallback: true, + }; + } + private ruleBasedCategorySuggestion( rawName: string, categories: Awaited>,