diff --git a/apps/api/src/app/account/account.service.ts b/apps/api/src/app/account/account.service.ts index 06a5dd250..e2d0bedf4 100644 --- a/apps/api/src/app/account/account.service.ts +++ b/apps/api/src/app/account/account.service.ts @@ -1,14 +1,9 @@ +import { AccountBalanceService } from '@ghostfolio/api/services/account-balance/account-balance.service'; import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { Filter } from '@ghostfolio/common/interfaces'; import { Injectable } from '@nestjs/common'; -import { - Account, - AccountBalance, - Order, - Platform, - Prisma -} from '@prisma/client'; +import { Account, Order, Platform, Prisma } from '@prisma/client'; import Big from 'big.js'; import { groupBy } from 'lodash'; @@ -17,16 +12,21 @@ import { CashDetails } from './interfaces/cash-details.interface'; @Injectable() export class AccountService { public constructor( + private readonly accountBalanceService: AccountBalanceService, private readonly exchangeRateDataService: ExchangeRateDataService, private readonly prismaService: PrismaService ) {} - public async account( - accountWhereUniqueInput: Prisma.AccountWhereUniqueInput - ): Promise { - return this.prismaService.account.findUnique({ - where: accountWhereUniqueInput + public async account({ + id_userId + }: Prisma.AccountWhereUniqueInput): Promise { + const { id, userId } = id_userId; + + const [account] = await this.accounts({ + where: { id, userId } }); + + return account; } public async accountWithOrders( @@ -58,7 +58,7 @@ export class AccountService { > { const { include = {}, skip, take, cursor, where, orderBy } = params; - include.balances = { orderBy: { createdAt: 'desc' }, take: 1 }; + include.balances = { orderBy: { date: 'desc' }, take: 1 }; const accounts = await this.prismaService.account.findMany({ cursor, @@ -239,16 +239,17 @@ export class AccountService { ); if (amountInCurrencyOfAccount) { - await this.prismaService.account.update({ - data: { - balance: new Big(balance).plus(amountInCurrencyOfAccount).toNumber() - }, - where: { - id_userId: { - userId, - id: accountId + await this.accountBalanceService.createAccountBalance({ + date, + Account: { + connect: { + id_userId: { + userId, + id: accountId + } } - } + }, + value: new Big(balance).plus(amountInCurrencyOfAccount).toNumber() }); } } diff --git a/apps/api/src/app/order/order.module.ts b/apps/api/src/app/order/order.module.ts index c8742f9d2..8f033058d 100644 --- a/apps/api/src/app/order/order.module.ts +++ b/apps/api/src/app/order/order.module.ts @@ -2,6 +2,7 @@ import { AccountService } from '@ghostfolio/api/app/account/account.service'; import { CacheModule } from '@ghostfolio/api/app/cache/cache.module'; import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module'; import { UserModule } from '@ghostfolio/api/app/user/user.module'; +import { AccountBalanceService } from '@ghostfolio/api/services/account-balance/account-balance.service'; import { ApiModule } from '@ghostfolio/api/services/api/api.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { DataGatheringModule } from '@ghostfolio/api/services/data-gathering/data-gathering.module'; @@ -31,6 +32,6 @@ import { OrderService } from './order.service'; SymbolProfileModule, UserModule ], - providers: [AccountService, OrderService] + providers: [AccountBalanceService, AccountService, OrderService] }) export class OrderModule {} diff --git a/apps/api/src/app/portfolio/portfolio.module.ts b/apps/api/src/app/portfolio/portfolio.module.ts index fa11476ac..3b4ee5d76 100644 --- a/apps/api/src/app/portfolio/portfolio.module.ts +++ b/apps/api/src/app/portfolio/portfolio.module.ts @@ -2,6 +2,7 @@ import { AccessModule } from '@ghostfolio/api/app/access/access.module'; import { AccountService } from '@ghostfolio/api/app/account/account.service'; import { OrderModule } from '@ghostfolio/api/app/order/order.module'; import { UserModule } from '@ghostfolio/api/app/user/user.module'; +import { AccountBalanceService } from '@ghostfolio/api/services/account-balance/account-balance.service'; import { ApiModule } from '@ghostfolio/api/services/api/api.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { DataGatheringModule } from '@ghostfolio/api/services/data-gathering/data-gathering.module'; @@ -36,6 +37,7 @@ import { RulesService } from './rules.service'; UserModule ], providers: [ + AccountBalanceService, AccountService, CurrentRateService, PortfolioService, diff --git a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts index 9e1daa224..d94037530 100644 --- a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts +++ b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts @@ -125,9 +125,11 @@ export class ExchangeRateDataService { return 0; } - let factor = 1; + let factor: number; - if (aFromCurrency !== aToCurrency) { + if (aFromCurrency === aToCurrency) { + factor = 1; + } else { if (this.exchangeRates[`${aFromCurrency}${aToCurrency}`]) { factor = this.exchangeRates[`${aFromCurrency}${aToCurrency}`]; } else { @@ -171,7 +173,9 @@ export class ExchangeRateDataService { let factor: number; - if (aFromCurrency !== aToCurrency) { + if (aFromCurrency === aToCurrency) { + factor = 1; + } else { const dataSource = this.dataProviderService.getDataSourceForExchangeRates(); const symbol = `${aFromCurrency}${aToCurrency}`;