From afe15dd4f5ce77480ff8064f22bfb3f3d0dcf2f8 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Sat, 20 Dec 2025 14:54:32 +0100 Subject: [PATCH] Refactor tax calculation in calcRegionalSellPrice function of FalukantService to convert exemptTypes Set to PostgreSQL array string for improved query performance and clarity. --- backend/services/falukantService.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/backend/services/falukantService.js b/backend/services/falukantService.js index 0b159b2..d564be2 100644 --- a/backend/services/falukantService.js +++ b/backend/services/falukantService.js @@ -194,6 +194,12 @@ async function calcRegionalSellPrice(product, knowledgeFactor, regionId, worthPe if (hasChancellor) return 0; // Now compute cumulative tax but exclude regions whose regionType.labelTr is in exemptTypes + // Konvertiere exemptTypes Set zu einem PostgreSQL-Array-String + const exemptTypesArray = Array.from(exemptTypes); + const exemptTypesString = exemptTypesArray.length > 0 + ? `ARRAY[${exemptTypesArray.map(t => `'${t.replace(/'/g, "''")}'`).join(',')}]` + : `ARRAY[]::text[]`; + const rows = await sequelize.query( `WITH RECURSIVE ancestors AS ( SELECT r.id, r.parent_id, r.tax_percent, rt.label_tr as region_type @@ -206,9 +212,9 @@ async function calcRegionalSellPrice(product, knowledgeFactor, regionId, worthPe JOIN falukant_type.region_type rt2 ON rt2.id = reg.region_type_id JOIN ancestors a ON reg.id = a.parent_id ) - SELECT COALESCE(SUM(CASE WHEN :exempt_types::text[] && ARRAY[region_type] THEN 0 ELSE tax_percent END),0) AS total FROM ancestors;`, + SELECT COALESCE(SUM(CASE WHEN ${exemptTypesString} && ARRAY[region_type] THEN 0 ELSE tax_percent END),0) AS total FROM ancestors;`, { - replacements: { id: regionId, exempt_types: Array.from(exemptTypes) }, + replacements: { id: regionId }, type: sequelize.QueryTypes.SELECT } );