From 4e5f1ce6eacf7e139c0885679b0bfc4a3f10be8f Mon Sep 17 00:00:00 2001 From: Eskil Gjerde Sviggum Date: Thu, 19 Sep 2024 10:51:26 +0200 Subject: [PATCH] Throw error if using holding is both weight and value --- src/RiskScore/RiskScoreConnector.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/RiskScore/RiskScoreConnector.ts b/src/RiskScore/RiskScoreConnector.ts index 828e840..0060914 100644 --- a/src/RiskScore/RiskScoreConnector.ts +++ b/src/RiskScore/RiskScoreConnector.ts @@ -124,21 +124,18 @@ function validatePortfolioHoldings ( let holdingType: 'weight' | 'value' | undefined; for (const holding of (holdings as MorningstarAnyHoldingOptions[])) { - let currentHoldingType: 'weight' | 'value' | 'both' | undefined; + let currentHoldingType: 'weight' | 'value' | undefined; + const holdingName = holding.name ?? holding.id; + if (holding.weight !== undefined && holding.value !== undefined) { - currentHoldingType = 'both'; + throw new Error(`The holding “${holdingName}” in the portfolio “${portfolioName}” cannot have both weight and value.`); } else if (holding.weight !== undefined) { currentHoldingType = 'weight'; } else if (holding.value !== undefined) { currentHoldingType = 'value'; } else { - const holdingName = holding.name ?? holding.id; throw new Error(`The holding “${holdingName}” in the portfolio “${portfolioName}” does not have a value nor a weight.`); } - - if (currentHoldingType === 'both') { - continue; - } if (holdingType === undefined) { holdingType = currentHoldingType; @@ -161,10 +158,7 @@ function convertMorningstarHoldingOptionsToMorningstarHoldingRequest ( const typeErasedHolding = holding as MorningstarAnyHoldingOptions; - if (typeErasedHolding.weight !== undefined && typeErasedHolding.value !== undefined) { - holdingRequest.value = typeErasedHolding.value; - holdingRequest.weight = typeErasedHolding.weight; - } else if (typeErasedHolding.weight !== undefined) { + if (typeErasedHolding.weight !== undefined) { holdingRequest.weight = typeErasedHolding.weight; } else if (typeErasedHolding.value !== undefined) { holdingRequest.value = typeErasedHolding.value;