From 349b52fdb0a4312e2ae9448fa641e77fa0548093 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 9 Mar 2024 10:42:19 +0100 Subject: [PATCH] Refactor item type --- .../portfolio-calculator.interface.ts | 2 +- .../src/app/portfolio/portfolio-calculator.ts | 22 +++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/apps/api/src/app/portfolio/interfaces/portfolio-calculator.interface.ts b/apps/api/src/app/portfolio/interfaces/portfolio-calculator.interface.ts index 357b454fd..044b98d82 100644 --- a/apps/api/src/app/portfolio/interfaces/portfolio-calculator.interface.ts +++ b/apps/api/src/app/portfolio/interfaces/portfolio-calculator.interface.ts @@ -5,7 +5,7 @@ import { PortfolioOrder } from './portfolio-order.interface'; export interface PortfolioOrderItem extends PortfolioOrder { feeInBaseCurrency?: Big; feeInBaseCurrencyWithCurrencyEffect?: Big; - itemType?: '' | 'start' | 'end'; + itemType?: 'between' | 'end' | 'start'; unitPriceInBaseCurrency?: Big; unitPriceInBaseCurrencyWithCurrencyEffect?: Big; } diff --git a/apps/api/src/app/portfolio/portfolio-calculator.ts b/apps/api/src/app/portfolio/portfolio-calculator.ts index f0551f3b8..a06198bbc 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.ts @@ -1040,28 +1040,26 @@ export class PortfolioCalculator { } } - // Sort orders so that the start and end placeholder order are at the right + // Sort orders so that the start and end placeholder order are at the correct // position - orders = sortBy(orders, (order) => { - let sortIndex = new Date(order.date); + orders = sortBy(orders, ({ date, itemType }) => { + let sortIndex = new Date(date); - if (order.itemType === 'start') { - sortIndex = addMilliseconds(sortIndex, -1); - } - - if (order.itemType === 'end') { + if (itemType === 'end') { sortIndex = addMilliseconds(sortIndex, 1); + } else if (itemType === 'start') { + sortIndex = addMilliseconds(sortIndex, -1); } return sortIndex.getTime(); }); - const indexOfStartOrder = orders.findIndex((order) => { - return order.itemType === 'start'; + const indexOfStartOrder = orders.findIndex(({ itemType }) => { + return itemType === 'start'; }); - const indexOfEndOrder = orders.findIndex((order) => { - return order.itemType === 'end'; + const indexOfEndOrder = orders.findIndex(({ itemType }) => { + return itemType === 'end'; }); let totalInvestmentDays = 0;