Browse Source

Refactor filters

Co-Authored-By: Zakaria YAHI <9142557+ZakYahi@users.noreply.github.com>
pull/1299/head
Thomas 3 years ago
parent
commit
6696023505
  1. 19
      apps/api/src/app/account/account.controller.ts
  2. 22
      apps/api/src/app/portfolio/portfolio.controller.ts
  3. 104
      apps/api/src/app/portfolio/portfolio.service.ts

19
apps/api/src/app/account/account.controller.ts

@ -95,11 +95,10 @@ export class AccountController {
); );
let accountsWithAggregations = let accountsWithAggregations =
await this.portfolioService.getAccountsWithAggregations( await this.portfolioService.getAccountsWithAggregations({
impersonationUserId || this.request.user.id, userId: impersonationUserId || this.request.user.id,
undefined, withExcludedAccounts: true
true });
);
if ( if (
impersonationUserId || impersonationUserId ||
@ -139,11 +138,11 @@ export class AccountController {
); );
let accountsWithAggregations = let accountsWithAggregations =
await this.portfolioService.getAccountsWithAggregations( await this.portfolioService.getAccountsWithAggregations({
impersonationUserId || this.request.user.id, filters: [{ id, type: 'ACCOUNT' }],
[{ id, type: 'ACCOUNT' }], userId: impersonationUserId || this.request.user.id,
true withExcludedAccounts: true
); });
if ( if (
impersonationUserId || impersonationUserId ||

22
apps/api/src/app/portfolio/portfolio.controller.ts

@ -158,12 +158,12 @@ export class PortfolioController {
holdings, holdings,
summary, summary,
totalValueInBaseCurrency totalValueInBaseCurrency
} = await this.portfolioService.getDetails( } = await this.portfolioService.getDetails({
filters,
impersonationId, impersonationId,
this.request.user.id, dateRange: range,
range, userId: this.request.user.id
filters });
);
if (hasErrors || hasNotDefinedValuesInObject(holdings)) { if (hasErrors || hasNotDefinedValuesInObject(holdings)) {
hasError = true; hasError = true;
@ -400,12 +400,12 @@ export class PortfolioController {
hasDetails = user.subscription.type === 'Premium'; hasDetails = user.subscription.type === 'Premium';
} }
const { holdings } = await this.portfolioService.getDetails( const { holdings } = await this.portfolioService.getDetails({
access.userId, dateRange: 'max',
access.userId, filters: [{ id: 'EQUITY', type: 'ASSET_CLASS' }],
'max', impersonationId: access.userId,
[{ id: 'EQUITY', type: 'ASSET_CLASS' }] userId: access.userId
); });
const portfolioPublicDetails: PortfolioPublicDetails = { const portfolioPublicDetails: PortfolioPublicDetails = {
hasDetails, hasDetails,

104
apps/api/src/app/portfolio/portfolio.service.ts

@ -107,15 +107,19 @@ export class PortfolioService {
this.baseCurrency = this.configurationService.get('BASE_CURRENCY'); this.baseCurrency = this.configurationService.get('BASE_CURRENCY');
} }
public async getAccounts( public async getAccounts({
aUserId: string, filters,
aFilters?: Filter[], userId,
withExcludedAccounts = false withExcludedAccounts = false
): Promise<AccountWithValue[]> { }: {
const where: Prisma.AccountWhereInput = { userId: aUserId }; filters?: Filter[];
userId: string;
withExcludedAccounts?: boolean;
}): Promise<AccountWithValue[]> {
const where: Prisma.AccountWhereInput = { userId: userId };
if (aFilters?.[0].id && aFilters?.[0].type === 'ACCOUNT') { if (filters?.[0].id && filters?.[0].type === 'ACCOUNT') {
where.id = aFilters[0].id; where.id = filters[0].id;
} }
const [accounts, details] = await Promise.all([ const [accounts, details] = await Promise.all([
@ -124,13 +128,12 @@ export class PortfolioService {
include: { Order: true, Platform: true }, include: { Order: true, Platform: true },
orderBy: { name: 'asc' } orderBy: { name: 'asc' }
}), }),
this.getDetails( this.getDetails({
aUserId, filters,
aUserId, userId,
undefined, withExcludedAccounts,
aFilters, impersonationId: userId
withExcludedAccounts })
)
]); ]);
const userCurrency = this.request.user.Settings.settings.baseCurrency; const userCurrency = this.request.user.Settings.settings.baseCurrency;
@ -168,16 +171,20 @@ export class PortfolioService {
}); });
} }
public async getAccountsWithAggregations( public async getAccountsWithAggregations({
aUserId: string, filters,
aFilters?: Filter[], userId,
withExcludedAccounts = false withExcludedAccounts = false
): Promise<Accounts> { }: {
const accounts = await this.getAccounts( filters?: Filter[];
aUserId, userId: string;
aFilters, withExcludedAccounts?: boolean;
}): Promise<Accounts> {
const accounts = await this.getAccounts({
filters,
userId,
withExcludedAccounts withExcludedAccounts
); });
let totalBalanceInBaseCurrency = new Big(0); let totalBalanceInBaseCurrency = new Big(0);
let totalValueInBaseCurrency = new Big(0); let totalValueInBaseCurrency = new Big(0);
let transactionCount = 0; let transactionCount = 0;
@ -421,14 +428,21 @@ export class PortfolioService {
}; };
} }
public async getDetails( public async getDetails({
aImpersonationId: string, impersonationId,
aUserId: string, userId,
aDateRange: DateRange = 'max', dateRange = 'max',
aFilters?: Filter[], filters,
withExcludedAccounts = false withExcludedAccounts = false
): Promise<PortfolioDetails & { hasErrors: boolean }> { }: {
const userId = await this.getUserId(aImpersonationId, aUserId); impersonationId: string;
userId: string;
dateRange?: DateRange;
filters?: Filter[];
withExcludedAccounts?: boolean;
}): Promise<PortfolioDetails & { hasErrors: boolean }> {
// TODO:
userId = await this.getUserId(impersonationId, userId);
const user = await this.userService.user({ id: userId }); const user = await this.userService.user({ id: userId });
const emergencyFund = new Big( const emergencyFund = new Big(
@ -441,9 +455,9 @@ export class PortfolioService {
const { orders, portfolioOrders, transactionPoints } = const { orders, portfolioOrders, transactionPoints } =
await this.getTransactionPoints({ await this.getTransactionPoints({
filters,
userId, userId,
withExcludedAccounts, withExcludedAccounts
filters: aFilters
}); });
const portfolioCalculator = new PortfolioCalculator({ const portfolioCalculator = new PortfolioCalculator({
@ -457,15 +471,15 @@ export class PortfolioService {
const portfolioStart = parseDate( const portfolioStart = parseDate(
transactionPoints[0]?.date ?? format(new Date(), DATE_FORMAT) transactionPoints[0]?.date ?? format(new Date(), DATE_FORMAT)
); );
const startDate = this.getStartDate(aDateRange, portfolioStart); const startDate = this.getStartDate(dateRange, portfolioStart);
const currentPositions = await portfolioCalculator.getCurrentPositions( const currentPositions = await portfolioCalculator.getCurrentPositions(
startDate startDate
); );
const cashDetails = await this.accountService.getCashDetails({ const cashDetails = await this.accountService.getCashDetails({
filters,
userId, userId,
currency: userCurrency, currency: userCurrency
filters: aFilters
}); });
const holdings: PortfolioDetails['holdings'] = {}; const holdings: PortfolioDetails['holdings'] = {};
@ -475,10 +489,10 @@ export class PortfolioService {
let filteredValueInBaseCurrency = currentPositions.currentValue; let filteredValueInBaseCurrency = currentPositions.currentValue;
if ( if (
aFilters?.length === 0 || filters?.length === 0 ||
(aFilters?.length === 1 && (filters?.length === 1 &&
aFilters[0].type === 'ASSET_CLASS' && filters[0].type === 'ASSET_CLASS' &&
aFilters[0].id === 'CASH') filters[0].id === 'CASH')
) { ) {
filteredValueInBaseCurrency = filteredValueInBaseCurrency.plus( filteredValueInBaseCurrency = filteredValueInBaseCurrency.plus(
cashDetails.balanceInBaseCurrency cashDetails.balanceInBaseCurrency
@ -574,10 +588,10 @@ export class PortfolioService {
} }
if ( if (
aFilters?.length === 0 || filters?.length === 0 ||
(aFilters?.length === 1 && (filters?.length === 1 &&
aFilters[0].type === 'ASSET_CLASS' && filters[0].type === 'ASSET_CLASS' &&
aFilters[0].id === 'CASH') filters[0].id === 'CASH')
) { ) {
const cashPositions = await this.getCashPositions({ const cashPositions = await this.getCashPositions({
cashDetails, cashDetails,
@ -593,15 +607,15 @@ export class PortfolioService {
} }
const accounts = await this.getValueOfAccounts({ const accounts = await this.getValueOfAccounts({
filters,
orders, orders,
portfolioItemsNow, portfolioItemsNow,
userCurrency, userCurrency,
userId, userId,
withExcludedAccounts, withExcludedAccounts
filters: aFilters
}); });
const summary = await this.getSummary(aImpersonationId); const summary = await this.getSummary(impersonationId);
return { return {
accounts, accounts,

Loading…
Cancel
Save