Skip to content

Commit

Permalink
Throw error if using holding is both weight and value
Browse files Browse the repository at this point in the history
  • Loading branch information
Eskils committed Sep 19, 2024
1 parent dbbe40e commit 4e5f1ce
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/RiskScore/RiskScoreConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 4e5f1ce

Please sign in to comment.