|
|
@ -10,6 +10,7 @@ import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; |
|
|
import { TagService } from '@ghostfolio/api/services/tag/tag.service'; |
|
|
import { TagService } from '@ghostfolio/api/services/tag/tag.service'; |
|
|
import { DATE_FORMAT } from '@ghostfolio/common/helper'; |
|
|
import { DATE_FORMAT } from '@ghostfolio/common/helper'; |
|
|
import { Filter } from '@ghostfolio/common/interfaces'; |
|
|
import { Filter } from '@ghostfolio/common/interfaces'; |
|
|
|
|
|
import { AccountWithBalance } from '@ghostfolio/common/types'; |
|
|
|
|
|
|
|
|
import { Injectable } from '@nestjs/common'; |
|
|
import { Injectable } from '@nestjs/common'; |
|
|
import { EventEmitter2 } from '@nestjs/event-emitter'; |
|
|
import { EventEmitter2 } from '@nestjs/event-emitter'; |
|
|
@ -24,7 +25,7 @@ import { |
|
|
} from '@prisma/client'; |
|
|
} from '@prisma/client'; |
|
|
import { Big } from 'big.js'; |
|
|
import { Big } from 'big.js'; |
|
|
import { endOfToday, format } from 'date-fns'; |
|
|
import { endOfToday, format } from 'date-fns'; |
|
|
import { groupBy } from 'lodash'; |
|
|
import { groupBy, isNil } from 'lodash'; |
|
|
|
|
|
|
|
|
import { CashDetails } from './interfaces/cash-details.interface'; |
|
|
import { CashDetails } from './interfaces/cash-details.interface'; |
|
|
|
|
|
|
|
|
@ -40,7 +41,7 @@ export class AccountService { |
|
|
|
|
|
|
|
|
public async account({ |
|
|
public async account({ |
|
|
id_userId |
|
|
id_userId |
|
|
}: Prisma.AccountWhereUniqueInput): Promise<Account | null> { |
|
|
}: Prisma.AccountWhereUniqueInput): Promise<AccountWithBalance | null> { |
|
|
const account = await this.prismaService.account.findUnique({ |
|
|
const account = await this.prismaService.account.findUnique({ |
|
|
include: { |
|
|
include: { |
|
|
balances: { |
|
|
balances: { |
|
|
@ -87,7 +88,7 @@ export class AccountService { |
|
|
where?: Prisma.AccountWhereInput; |
|
|
where?: Prisma.AccountWhereInput; |
|
|
orderBy?: Prisma.AccountOrderByWithRelationInput; |
|
|
orderBy?: Prisma.AccountOrderByWithRelationInput; |
|
|
}): Promise< |
|
|
}): Promise< |
|
|
(Account & { |
|
|
(AccountWithBalance & { |
|
|
activities?: (Order & { SymbolProfile?: SymbolProfile })[]; |
|
|
activities?: (Order & { SymbolProfile?: SymbolProfile })[]; |
|
|
balances?: AccountBalance[]; |
|
|
balances?: AccountBalance[]; |
|
|
platform?: Platform; |
|
|
platform?: Platform; |
|
|
@ -160,12 +161,18 @@ export class AccountService { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async createAccount( |
|
|
public async createAccount({ |
|
|
data: Prisma.AccountCreateInput, |
|
|
balance, |
|
|
aUserId: string, |
|
|
data, |
|
|
tagIds?: string[] |
|
|
tagIds, |
|
|
): Promise<Account> { |
|
|
userId |
|
|
await this.tagService.validateTagIds({ tagIds, userId: aUserId }); |
|
|
}: { |
|
|
|
|
|
balance?: number; |
|
|
|
|
|
data: Prisma.AccountCreateInput; |
|
|
|
|
|
tagIds?: string[]; |
|
|
|
|
|
userId: string; |
|
|
|
|
|
}): Promise<Account> { |
|
|
|
|
|
await this.tagService.validateTagIds({ tagIds, userId }); |
|
|
|
|
|
|
|
|
const account = await this.prismaService.account.create({ |
|
|
const account = await this.prismaService.account.create({ |
|
|
data: { |
|
|
data: { |
|
|
@ -182,12 +189,14 @@ export class AccountService { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
await this.accountBalanceService.createOrUpdateAccountBalance({ |
|
|
if (!isNil(balance)) { |
|
|
accountId: account.id, |
|
|
await this.accountBalanceService.createOrUpdateAccountBalance({ |
|
|
balance: data.balance, |
|
|
balance, |
|
|
date: format(new Date(), DATE_FORMAT), |
|
|
userId, |
|
|
userId: aUserId |
|
|
accountId: account.id, |
|
|
}); |
|
|
date: format(new Date(), DATE_FORMAT) |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.eventEmitter.emit( |
|
|
this.eventEmitter.emit( |
|
|
PortfolioChangedEvent.getName(), |
|
|
PortfolioChangedEvent.getName(), |
|
|
@ -216,7 +225,7 @@ export class AccountService { |
|
|
return account; |
|
|
return account; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async getAccounts(aUserId: string): Promise<Account[]> { |
|
|
public async getAccounts(aUserId: string): Promise<AccountWithBalance[]> { |
|
|
const accounts = await this.accounts({ |
|
|
const accounts = await this.accounts({ |
|
|
include: { |
|
|
include: { |
|
|
activities: true, |
|
|
activities: true, |
|
|
@ -295,17 +304,20 @@ export class AccountService { |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async updateAccount( |
|
|
public async updateAccount({ |
|
|
params: { |
|
|
balance, |
|
|
data: Prisma.AccountUpdateInput; |
|
|
data, |
|
|
where: Prisma.AccountWhereUniqueInput; |
|
|
tagIds, |
|
|
}, |
|
|
userId, |
|
|
aUserId: string, |
|
|
where |
|
|
tagIds?: string[] |
|
|
}: { |
|
|
): Promise<Account> { |
|
|
balance?: number; |
|
|
const { data, where } = params; |
|
|
data: Prisma.AccountUpdateInput; |
|
|
|
|
|
tagIds?: string[]; |
|
|
await this.tagService.validateTagIds({ tagIds, userId: aUserId }); |
|
|
userId: string; |
|
|
|
|
|
where: Prisma.AccountWhereUniqueInput; |
|
|
|
|
|
}): Promise<Account> { |
|
|
|
|
|
await this.tagService.validateTagIds({ tagIds, userId }); |
|
|
|
|
|
|
|
|
const account = await this.prismaService.account.update({ |
|
|
const account = await this.prismaService.account.update({ |
|
|
data: { |
|
|
data: { |
|
|
@ -324,12 +336,14 @@ export class AccountService { |
|
|
where |
|
|
where |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
await this.accountBalanceService.createOrUpdateAccountBalance({ |
|
|
if (!isNil(balance)) { |
|
|
accountId: account.id, |
|
|
await this.accountBalanceService.createOrUpdateAccountBalance({ |
|
|
balance: data.balance as number, |
|
|
balance, |
|
|
date: format(new Date(), DATE_FORMAT), |
|
|
userId, |
|
|
userId: aUserId |
|
|
accountId: account.id, |
|
|
}); |
|
|
date: format(new Date(), DATE_FORMAT) |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.eventEmitter.emit( |
|
|
this.eventEmitter.emit( |
|
|
PortfolioChangedEvent.getName(), |
|
|
PortfolioChangedEvent.getName(), |
|
|
|