From 3b24fd448ca859b8d8937c6ef1f8f6d1019a1fd0 Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Wed, 26 Nov 2025 07:23:50 +0100 Subject: [PATCH] feat: Add `INCLUDE_CATEGORY_IDS` environment variable and logic to promote specified categories to root level. --- .env.example | 3 ++- src/utils/category-tree-utils.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 289fbd0..21280f6 100644 --- a/.env.example +++ b/.env.example @@ -13,4 +13,5 @@ JTL_STEUERZONE_ID=1 SERVER_PORT=3991 SERVER_HOST=127.0.0.1 SYNC_INTERVAL_MS=600000 -EXCLUDE_CATEGORY_IDS= \ No newline at end of file +EXCLUDE_CATEGORY_IDS= +INCLUDE_CATEGORY_IDS= \ No newline at end of file diff --git a/src/utils/category-tree-utils.js b/src/utils/category-tree-utils.js index c2b16b6..3942d31 100644 --- a/src/utils/category-tree-utils.js +++ b/src/utils/category-tree-utils.js @@ -15,6 +15,19 @@ export function parseExcludedIds() { ); } +/** + * Parse included category IDs from environment variable + * @returns {Set} - Set of included category IDs + */ +export function parseIncludedIds() { + return new Set( + (process.env.INCLUDE_CATEGORY_IDS || '') + .split(',') + .map(id => parseInt(id.trim())) + .filter(id => !isNaN(id)) + ); +} + /** * Build a hierarchical category tree from flat data * @param {Array} categories - Category records with kKategorie, kOberKategorie, nSort @@ -51,6 +64,7 @@ export function buildTree(categories, names, articleCounts, images, applyRootFil const rootNodes = []; const excludedIds = parseExcludedIds(); + const includedIds = parseIncludedIds(); // Build hierarchy categories.forEach(cat => { @@ -81,6 +95,23 @@ export function buildTree(categories, names, articleCounts, images, applyRootFil } } + // Add included categories to the result nodes (promoting them to root level) + includedIds.forEach(id => { + // Skip if excluded (exclusion takes precedence) + if (excludedIds.has(id)) return; + + const node = categoryMap.get(id); + if (node) { + // Remove subcategories for included categories as requested + node.children = []; + + // Avoid duplicates if the node is already in the result list + if (!resultNodes.includes(node)) { + resultNodes.push(node); + } + } + }); + // Sort children and remove nSort for (const node of categoryMap.values()) { node.children.sort((a, b) => a.nSort - b.nSort || a.kKategorie - b.kKategorie);