2021-07-07 21:23:36 +02:00
|
|
|
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
|
2021-05-01 12:30:52 +02:00
|
|
|
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
|
|
|
|
import { Injectable } from '@nestjs/common';
|
2021-07-07 21:23:36 +02:00
|
|
|
import { Account, Currency, Order, Prisma } from '@prisma/client';
|
2021-05-01 12:30:52 +02:00
|
|
|
|
|
|
|
import { RedisCacheService } from '../redis-cache/redis-cache.service';
|
2021-07-10 14:57:03 +02:00
|
|
|
import { CashDetails } from './interfaces/cash-details.interface';
|
2021-05-01 12:30:52 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AccountService {
|
|
|
|
public constructor(
|
2021-07-07 21:23:36 +02:00
|
|
|
private exchangeRateDataService: ExchangeRateDataService,
|
2021-05-01 12:30:52 +02:00
|
|
|
private readonly redisCacheService: RedisCacheService,
|
|
|
|
private prisma: PrismaService
|
|
|
|
) {}
|
|
|
|
|
|
|
|
public async account(
|
|
|
|
accountWhereUniqueInput: Prisma.AccountWhereUniqueInput
|
|
|
|
): Promise<Account | null> {
|
|
|
|
return this.prisma.account.findUnique({
|
|
|
|
where: accountWhereUniqueInput
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-02 21:18:52 +02:00
|
|
|
public async accountWithOrders(
|
|
|
|
accountWhereUniqueInput: Prisma.AccountWhereUniqueInput,
|
|
|
|
accountInclude: Prisma.AccountInclude
|
|
|
|
): Promise<
|
|
|
|
Account & {
|
|
|
|
Order?: Order[];
|
|
|
|
}
|
|
|
|
> {
|
|
|
|
return this.prisma.account.findUnique({
|
|
|
|
include: accountInclude,
|
|
|
|
where: accountWhereUniqueInput
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-01 12:30:52 +02:00
|
|
|
public async accounts(params: {
|
|
|
|
include?: Prisma.AccountInclude;
|
|
|
|
skip?: number;
|
|
|
|
take?: number;
|
|
|
|
cursor?: Prisma.AccountWhereUniqueInput;
|
|
|
|
where?: Prisma.AccountWhereInput;
|
|
|
|
orderBy?: Prisma.AccountOrderByInput;
|
|
|
|
}): Promise<Account[]> {
|
|
|
|
const { include, skip, take, cursor, where, orderBy } = params;
|
|
|
|
|
|
|
|
return this.prisma.account.findMany({
|
|
|
|
cursor,
|
|
|
|
include,
|
|
|
|
orderBy,
|
|
|
|
skip,
|
|
|
|
take,
|
|
|
|
where
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async createAccount(
|
|
|
|
data: Prisma.AccountCreateInput,
|
|
|
|
aUserId: string
|
|
|
|
): Promise<Account> {
|
|
|
|
return this.prisma.account.create({
|
|
|
|
data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async deleteAccount(
|
|
|
|
where: Prisma.AccountWhereUniqueInput,
|
|
|
|
aUserId: string
|
|
|
|
): Promise<Account> {
|
|
|
|
return this.prisma.account.delete({
|
|
|
|
where
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-10 14:57:03 +02:00
|
|
|
public async getCashDetails(
|
|
|
|
aUserId: string,
|
|
|
|
aCurrency: Currency
|
|
|
|
): Promise<CashDetails> {
|
|
|
|
let totalCashBalance = 0;
|
|
|
|
|
|
|
|
const accounts = await this.accounts({
|
|
|
|
where: { userId: aUserId }
|
|
|
|
});
|
|
|
|
|
|
|
|
accounts.forEach((account) => {
|
|
|
|
totalCashBalance += this.exchangeRateDataService.toCurrency(
|
|
|
|
account.balance,
|
|
|
|
account.currency,
|
|
|
|
aCurrency
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return { accounts, balance: totalCashBalance };
|
|
|
|
}
|
|
|
|
|
2021-05-01 12:30:52 +02:00
|
|
|
public async updateAccount(
|
|
|
|
params: {
|
|
|
|
where: Prisma.AccountWhereUniqueInput;
|
|
|
|
data: Prisma.AccountUpdateInput;
|
|
|
|
},
|
|
|
|
aUserId: string
|
|
|
|
): Promise<Account> {
|
|
|
|
const { data, where } = params;
|
|
|
|
return this.prisma.account.update({
|
|
|
|
data,
|
|
|
|
where
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|