Browse Source

Handle account balance update

pull/2166/head
Thomas 2 years ago
parent
commit
4d4f45e3be
  1. 45
      apps/api/src/app/account/account.service.ts
  2. 3
      apps/api/src/app/order/order.module.ts
  3. 2
      apps/api/src/app/portfolio/portfolio.module.ts
  4. 10
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

45
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 { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { Filter } from '@ghostfolio/common/interfaces'; import { Filter } from '@ghostfolio/common/interfaces';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { import { Account, Order, Platform, Prisma } from '@prisma/client';
Account,
AccountBalance,
Order,
Platform,
Prisma
} from '@prisma/client';
import Big from 'big.js'; import Big from 'big.js';
import { groupBy } from 'lodash'; import { groupBy } from 'lodash';
@ -17,16 +12,21 @@ import { CashDetails } from './interfaces/cash-details.interface';
@Injectable() @Injectable()
export class AccountService { export class AccountService {
public constructor( public constructor(
private readonly accountBalanceService: AccountBalanceService,
private readonly exchangeRateDataService: ExchangeRateDataService, private readonly exchangeRateDataService: ExchangeRateDataService,
private readonly prismaService: PrismaService private readonly prismaService: PrismaService
) {} ) {}
public async account( public async account({
accountWhereUniqueInput: Prisma.AccountWhereUniqueInput id_userId
): Promise<Account | null> { }: Prisma.AccountWhereUniqueInput): Promise<Account | null> {
return this.prismaService.account.findUnique({ const { id, userId } = id_userId;
where: accountWhereUniqueInput
const [account] = await this.accounts({
where: { id, userId }
}); });
return account;
} }
public async accountWithOrders( public async accountWithOrders(
@ -58,7 +58,7 @@ export class AccountService {
> { > {
const { include = {}, skip, take, cursor, where, orderBy } = params; 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({ const accounts = await this.prismaService.account.findMany({
cursor, cursor,
@ -239,16 +239,17 @@ export class AccountService {
); );
if (amountInCurrencyOfAccount) { if (amountInCurrencyOfAccount) {
await this.prismaService.account.update({ await this.accountBalanceService.createAccountBalance({
data: { date,
balance: new Big(balance).plus(amountInCurrencyOfAccount).toNumber() Account: {
}, connect: {
where: { id_userId: {
id_userId: { userId,
userId, id: accountId
id: accountId }
} }
} },
value: new Big(balance).plus(amountInCurrencyOfAccount).toNumber()
}); });
} }
} }

3
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 { CacheModule } from '@ghostfolio/api/app/cache/cache.module';
import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module'; import { RedisCacheModule } from '@ghostfolio/api/app/redis-cache/redis-cache.module';
import { UserModule } from '@ghostfolio/api/app/user/user.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 { ApiModule } from '@ghostfolio/api/services/api/api.module';
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
import { DataGatheringModule } from '@ghostfolio/api/services/data-gathering/data-gathering.module'; import { DataGatheringModule } from '@ghostfolio/api/services/data-gathering/data-gathering.module';
@ -31,6 +32,6 @@ import { OrderService } from './order.service';
SymbolProfileModule, SymbolProfileModule,
UserModule UserModule
], ],
providers: [AccountService, OrderService] providers: [AccountBalanceService, AccountService, OrderService]
}) })
export class OrderModule {} export class OrderModule {}

2
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 { AccountService } from '@ghostfolio/api/app/account/account.service';
import { OrderModule } from '@ghostfolio/api/app/order/order.module'; import { OrderModule } from '@ghostfolio/api/app/order/order.module';
import { UserModule } from '@ghostfolio/api/app/user/user.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 { ApiModule } from '@ghostfolio/api/services/api/api.module';
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
import { DataGatheringModule } from '@ghostfolio/api/services/data-gathering/data-gathering.module'; import { DataGatheringModule } from '@ghostfolio/api/services/data-gathering/data-gathering.module';
@ -36,6 +37,7 @@ import { RulesService } from './rules.service';
UserModule UserModule
], ],
providers: [ providers: [
AccountBalanceService,
AccountService, AccountService,
CurrentRateService, CurrentRateService,
PortfolioService, PortfolioService,

10
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -125,9 +125,11 @@ export class ExchangeRateDataService {
return 0; return 0;
} }
let factor = 1; let factor: number;
if (aFromCurrency !== aToCurrency) { if (aFromCurrency === aToCurrency) {
factor = 1;
} else {
if (this.exchangeRates[`${aFromCurrency}${aToCurrency}`]) { if (this.exchangeRates[`${aFromCurrency}${aToCurrency}`]) {
factor = this.exchangeRates[`${aFromCurrency}${aToCurrency}`]; factor = this.exchangeRates[`${aFromCurrency}${aToCurrency}`];
} else { } else {
@ -171,7 +173,9 @@ export class ExchangeRateDataService {
let factor: number; let factor: number;
if (aFromCurrency !== aToCurrency) { if (aFromCurrency === aToCurrency) {
factor = 1;
} else {
const dataSource = const dataSource =
this.dataProviderService.getDataSourceForExchangeRates(); this.dataProviderService.getDataSourceForExchangeRates();
const symbol = `${aFromCurrency}${aToCurrency}`; const symbol = `${aFromCurrency}${aToCurrency}`;

Loading…
Cancel
Save