Browse Source

Refactoring

pull/3118/head
Thomas Kaul 1 year ago
parent
commit
065f60253f
  1. 25
      apps/api/src/app/portfolio/portfolio-calculator.ts

25
apps/api/src/app/portfolio/portfolio-calculator.ts

@ -22,6 +22,7 @@ import {
format, format,
isBefore, isBefore,
isSameDay, isSameDay,
max,
subDays subDays
} from 'date-fns'; } from 'date-fns';
import { cloneDeep, first, isNumber, last, sortBy, uniq } from 'lodash'; import { cloneDeep, first, isNumber, last, sortBy, uniq } from 'lodash';
@ -451,12 +452,21 @@ export class PortfolioCalculator {
start: Date, start: Date,
end?: Date end?: Date
): Promise<CurrentPositions> { ): Promise<CurrentPositions> {
const transactionPoints = const lastTransactionPoint = last(this.transactionPoints);
(end
? this.transactionPoints?.filter(({ date }) => { let endDate = end;
return isBefore(parseDate(date), end);
}) if (!endDate) {
: this.transactionPoints) ?? []; endDate = new Date(Date.now());
if (lastTransactionPoint) {
endDate = max([endDate, parseDate(lastTransactionPoint.date)]);
}
}
const transactionPoints = this.transactionPoints?.filter(({ date }) => {
return isBefore(parseDate(date), endDate);
});
if (!transactionPoints.length) { if (!transactionPoints.length) {
return { return {
@ -475,12 +485,9 @@ export class PortfolioCalculator {
}; };
} }
const lastTransactionPoint = last(transactionPoints);
const currencies: { [symbol: string]: string } = {}; const currencies: { [symbol: string]: string } = {};
const dataGatheringItems: IDataGatheringItem[] = []; const dataGatheringItems: IDataGatheringItem[] = [];
let dates: Date[] = []; let dates: Date[] = [];
const endDate = parseDate(lastTransactionPoint.date);
let firstIndex = transactionPoints.length; let firstIndex = transactionPoints.length;
let firstTransactionPoint: TransactionPoint = null; let firstTransactionPoint: TransactionPoint = null;

Loading…
Cancel
Save