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. 21
      prisma/schema.prisma

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

@ -57,9 +57,11 @@ export class AccountService {
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,
include,
orderBy,
@ -67,15 +69,32 @@ export class AccountService {
take,
where
});
return accounts.map((account) => {
return { ...account, balance: account.balances[0]?.value ?? 0 };
});
}
public async createAccount(
data: Prisma.AccountCreateInput,
aUserId: string
): Promise<Account> {
return this.prismaService.account.create({
const account = await this.prismaService.account.create({
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(
@ -90,7 +109,6 @@ export class AccountService {
public async getAccounts(aUserId: string) {
const accounts = await this.accounts({
include: {
balances: { orderBy: { createdAt: 'desc' }, take: 1 },
Order: true,
Platform: true
},
@ -153,16 +171,13 @@ export class AccountService {
}
const accounts = await this.accounts({
where,
include: {
balances: { orderBy: { createdAt: 'desc' }, take: 1 }
}
where
});
for (const account of accounts) {
totalCashBalanceInBaseCurrency = totalCashBalanceInBaseCurrency.plus(
this.exchangeRateDataService.toCurrency(
account.balances[0].value,
account.balance,
account.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') {
currentAccounts = await this.accountService.accounts({
include: {
balances: { orderBy: { createdAt: 'desc' }, take: 1 },
Platform: true
},
where: { id: filters[0].id }
@ -1846,11 +1845,11 @@ export class PortfolioService {
ordersByAccount = ordersByAccount.concat(ordersOfTypeItemByAccount);
accounts[account.id] = {
balance: account.balances[0].value,
balance: account.balance,
currency: account.currency,
name: account.name,
valueInBaseCurrency: this.exchangeRateDataService.toCurrency(
account.balances[0].value,
account.balance,
account.currency,
userCurrency
)
@ -1859,17 +1858,17 @@ export class PortfolioService {
if (platforms[account.Platform?.id || UNKNOWN_KEY]?.valueInBaseCurrency) {
platforms[account.Platform?.id || UNKNOWN_KEY].valueInBaseCurrency +=
this.exchangeRateDataService.toCurrency(
account.balances[0].value,
account.balance,
account.currency,
userCurrency
);
} else {
platforms[account.Platform?.id || UNKNOWN_KEY] = {
balance: account.balances[0].value,
balance: account.balance,
currency: account.currency,
name: account.Platform?.name,
valueInBaseCurrency: this.exchangeRateDataService.toCurrency(
account.balances[0].value,
account.balance,
account.currency,
userCurrency
)

21
prisma/schema.prisma

@ -21,21 +21,21 @@ model Access {
}
model Account {
accountType AccountType @default(SECURITIES)
balance Float @default(0)
accountType AccountType @default(SECURITIES)
balance Float @default(0)
balances AccountBalance[]
comment String?
createdAt DateTime @default(now())
createdAt DateTime @default(now())
currency String?
id String @default(uuid())
isDefault Boolean @default(false)
isExcluded Boolean @default(false)
id String @default(uuid())
isDefault Boolean @default(false)
isExcluded Boolean @default(false)
name String?
platformId String?
updatedAt DateTime @updatedAt
updatedAt DateTime @updatedAt
userId String
Platform Platform? @relation(fields: [platformId], references: [id])
User User @relation(fields: [userId], references: [id])
Platform Platform? @relation(fields: [platformId], references: [id])
User User @relation(fields: [userId], references: [id])
Order Order[]
@@id([id, userId])
@ -63,11 +63,12 @@ model AuthDevice {
model AccountBalance {
accountId String
createdAt DateTime @default(now())
date DateTime @default(now())
id String @id @default(uuid())
value Float
updatedAt DateTime @updatedAt
userId String
Account Account @relation(fields: [accountId, userId], references: [id, userId])
Account Account @relation(fields: [accountId, userId], onDelete: Cascade, references: [id, userId])
}
model MarketData {

Loading…
Cancel
Save