|
@ -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() |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|