Browse Source

Refactoring

pull/2166/head
Thomas 2 years ago
parent
commit
a724e32b5f
  1. 33
      apps/api/src/app/account/account.service.ts
  2. 11
      apps/api/src/app/portfolio/portfolio.service.ts
  3. 3
      prisma/schema.prisma

33
apps/api/src/app/account/account.service.ts

@ -57,9 +57,11 @@ export class AccountService {
Platform?: Platform; Platform?: Platform;
})[] })[]
> { > {
const { include, skip, take, cursor, where, orderBy } = params; const { include = {}, skip, take, cursor, where, orderBy } = params;
return this.prismaService.account.findMany({ include.balances = { orderBy: { createdAt: 'desc' }, take: 1 };
const accounts = await this.prismaService.account.findMany({
cursor, cursor,
include, include,
orderBy, orderBy,
@ -67,15 +69,32 @@ export class AccountService {
take, take,
where where
}); });
return accounts.map((account) => {
return { ...account, balance: account.balances[0]?.value ?? 0 };
});
} }
public async createAccount( public async createAccount(
data: Prisma.AccountCreateInput, data: Prisma.AccountCreateInput,
aUserId: string aUserId: string
): Promise<Account> { ): Promise<Account> {
return this.prismaService.account.create({ const account = await this.prismaService.account.create({
data data
}); });
await this.prismaService.accountBalance.create({
data: {
Account: {
connect: {
id_userId: { id: account.id, userId: aUserId }
}
},
value: <any>data.balance
}
});
return account;
} }
public async deleteAccount( public async deleteAccount(
@ -90,7 +109,6 @@ export class AccountService {
public async getAccounts(aUserId: string) { public async getAccounts(aUserId: string) {
const accounts = await this.accounts({ const accounts = await this.accounts({
include: { include: {
balances: { orderBy: { createdAt: 'desc' }, take: 1 },
Order: true, Order: true,
Platform: true Platform: true
}, },
@ -153,16 +171,13 @@ export class AccountService {
} }
const accounts = await this.accounts({ const accounts = await this.accounts({
where, where
include: {
balances: { orderBy: { createdAt: 'desc' }, take: 1 }
}
}); });
for (const account of accounts) { for (const account of accounts) {
totalCashBalanceInBaseCurrency = totalCashBalanceInBaseCurrency.plus( totalCashBalanceInBaseCurrency = totalCashBalanceInBaseCurrency.plus(
this.exchangeRateDataService.toCurrency( this.exchangeRateDataService.toCurrency(
account.balances[0].value, account.balance,
account.currency, account.currency,
currency currency
) )

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

@ -1810,7 +1810,6 @@ export class PortfolioService {
} else if (filters.length === 1 && filters[0].type === 'ACCOUNT') { } else if (filters.length === 1 && filters[0].type === 'ACCOUNT') {
currentAccounts = await this.accountService.accounts({ currentAccounts = await this.accountService.accounts({
include: { include: {
balances: { orderBy: { createdAt: 'desc' }, take: 1 },
Platform: true Platform: true
}, },
where: { id: filters[0].id } where: { id: filters[0].id }
@ -1846,11 +1845,11 @@ export class PortfolioService {
ordersByAccount = ordersByAccount.concat(ordersOfTypeItemByAccount); ordersByAccount = ordersByAccount.concat(ordersOfTypeItemByAccount);
accounts[account.id] = { accounts[account.id] = {
balance: account.balances[0].value, balance: account.balance,
currency: account.currency, currency: account.currency,
name: account.name, name: account.name,
valueInBaseCurrency: this.exchangeRateDataService.toCurrency( valueInBaseCurrency: this.exchangeRateDataService.toCurrency(
account.balances[0].value, account.balance,
account.currency, account.currency,
userCurrency userCurrency
) )
@ -1859,17 +1858,17 @@ export class PortfolioService {
if (platforms[account.Platform?.id || UNKNOWN_KEY]?.valueInBaseCurrency) { if (platforms[account.Platform?.id || UNKNOWN_KEY]?.valueInBaseCurrency) {
platforms[account.Platform?.id || UNKNOWN_KEY].valueInBaseCurrency += platforms[account.Platform?.id || UNKNOWN_KEY].valueInBaseCurrency +=
this.exchangeRateDataService.toCurrency( this.exchangeRateDataService.toCurrency(
account.balances[0].value, account.balance,
account.currency, account.currency,
userCurrency userCurrency
); );
} else { } else {
platforms[account.Platform?.id || UNKNOWN_KEY] = { platforms[account.Platform?.id || UNKNOWN_KEY] = {
balance: account.balances[0].value, balance: account.balance,
currency: account.currency, currency: account.currency,
name: account.Platform?.name, name: account.Platform?.name,
valueInBaseCurrency: this.exchangeRateDataService.toCurrency( valueInBaseCurrency: this.exchangeRateDataService.toCurrency(
account.balances[0].value, account.balance,
account.currency, account.currency,
userCurrency userCurrency
) )

3
prisma/schema.prisma

@ -63,11 +63,12 @@ model AuthDevice {
model AccountBalance { model AccountBalance {
accountId String accountId String
createdAt DateTime @default(now()) createdAt DateTime @default(now())
date DateTime @default(now())
id String @id @default(uuid()) id String @id @default(uuid())
value Float value Float
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
userId String userId String
Account Account @relation(fields: [accountId, userId], references: [id, userId]) Account Account @relation(fields: [accountId, userId], onDelete: Cascade, references: [id, userId])
} }
model MarketData { model MarketData {

Loading…
Cancel
Save