Verbessere die Berechnung der Geschenkekosten in FalukantService und füge Tests für die Funktion hinzu

This commit is contained in:
Torsten Schulz (local)
2026-02-14 16:12:07 +01:00
parent a3b550859c
commit be7db6ad96
3 changed files with 82 additions and 13 deletions

View File

@@ -0,0 +1,20 @@
function getGiftCostLocal(value, titleOfNobility, lowestTitleOfNobility) {
const val = Number(value) || 0;
const title = Number(titleOfNobility) || 1;
const lowest = Number(lowestTitleOfNobility) || 1;
const titleLevel = title - lowest + 1;
const cost = Math.round(val * Math.pow(1 + titleLevel * 0.3, 1.3) * 100) / 100;
return Number.isFinite(cost) ? cost : 0;
}
const cases = [
{ giftValue: 100, title: 3, lowest: 1 },
{ giftValue: '200', title: '2', lowest: '1' },
{ giftValue: null, title: null, lowest: null },
{ giftValue: undefined, title: undefined, lowest: undefined },
{ giftValue: 'abc', title: 5, lowest: 1 }
];
for (const c of cases) {
console.log(`in=${JSON.stringify(c)} -> cost=${getGiftCostLocal(c.giftValue, c.title, c.lowest)}`);
}

View File

@@ -0,0 +1,38 @@
import { fileURLToPath } from 'url';
import path from 'path';
import { readFileSync } from 'fs';
// Kleine Testhilfe: extrahiere getGiftCost aus service-file via eval (schneller Smoke-test ohne DB)
const svcPath = path.resolve(process.cwd(), 'services', 'falukantService.js');
const src = readFileSync(svcPath, 'utf8');
// Extrahiere die getGiftCost-Funktion via Regex (vereinfachte Annahme)
const re = /async getGiftCost\([\s\S]*?\n\s*}\n/;
const match = src.match(re);
if (!match) {
console.error('getGiftCost function not found');
process.exit(2);
}
const funcSrc = match[0];
// Wrappe in Async-Function und erzeuge getGiftCost im lokalen Scope
const wrapper = `(async () => { ${funcSrc}; return getGiftCost; })()`;
// eslint-disable-next-line no-eval
const getGiftCostPromise = eval(wrapper);
let getGiftCost;
getGiftCostPromise.then(f => { getGiftCost = f; runTests(); }).catch(e => { console.error('eval failed', e); process.exit(2); });
function runTests() {
const cases = [
{ value: 100, title: 3, lowest: 1 },
{ value: '200', title: '2', lowest: '1' },
{ value: null, title: null, lowest: null },
{ value: 'abc', title: 5, lowest: 1 }
];
for (const c of cases) {
getGiftCost(c.value, c.title, c.lowest).then(out => {
console.log(`in=${JSON.stringify(c)} -> cost=${out}`);
}).catch(err => console.error('error calling getGiftCost', err));
}
}
// Ende Patch