ghostfolio/apps/api/src/app/admin/admin.service.ts

120 lines
2.9 KiB
TypeScript
Raw Normal View History

import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
import { AdminData } from '@ghostfolio/helper/interfaces';
2021-04-13 21:53:58 +02:00
import { Injectable } from '@nestjs/common';
import { Currency } from '@prisma/client';
@Injectable()
export class AdminService {
public constructor(
private exchangeRateDataService: ExchangeRateDataService,
private prisma: PrismaService
) {}
public async get(): Promise<AdminData> {
return {
exchangeRates: [
{
label1: Currency.EUR,
label2: Currency.CHF,
value: await this.exchangeRateDataService.toCurrency(
1,
Currency.EUR,
Currency.CHF
)
},
{
label1: Currency.GBP,
label2: Currency.CHF,
value: await this.exchangeRateDataService.toCurrency(
1,
Currency.GBP,
Currency.CHF
)
},
{
label1: Currency.USD,
label2: Currency.CHF,
value: await this.exchangeRateDataService.toCurrency(
1,
Currency.USD,
Currency.CHF
)
},
{
label1: Currency.USD,
label2: Currency.EUR,
value: await this.exchangeRateDataService.toCurrency(
1,
Currency.USD,
Currency.EUR
)
},
{
label1: Currency.USD,
label2: Currency.GBP,
value: await this.exchangeRateDataService.toCurrency(
1,
Currency.USD,
Currency.GBP
)
}
],
lastDataGathering: await this.getLastDataGathering(),
transactionCount: await this.prisma.order.count(),
2021-04-18 20:12:58 +02:00
userCount: await this.prisma.user.count(),
users: await this.getUsersWithAnalytics()
2021-04-13 21:53:58 +02:00
};
}
private async getLastDataGathering() {
const lastDataGathering = await this.prisma.property.findUnique({
where: { key: 'LAST_DATA_GATHERING' }
});
if (lastDataGathering?.value) {
return new Date(lastDataGathering.value);
}
const dataGatheringInProgress = await this.prisma.property.findUnique({
where: { key: 'LOCKED_DATA_GATHERING' }
});
if (dataGatheringInProgress) {
return 'IN_PROGRESS';
}
return null;
}
2021-04-18 20:12:58 +02:00
private async getUsersWithAnalytics() {
return await this.prisma.user.findMany({
orderBy: {
Analytics: {
updatedAt: 'desc'
}
},
2021-04-13 21:53:58 +02:00
select: {
2021-04-18 20:12:58 +02:00
_count: {
select: { Account: true, Order: true }
2021-04-18 20:12:58 +02:00
},
alias: true,
Analytics: {
2021-04-13 21:53:58 +02:00
select: {
2021-04-18 20:12:58 +02:00
activityCount: true,
updatedAt: true
2021-04-13 21:53:58 +02:00
}
2021-04-18 20:12:58 +02:00
},
createdAt: true,
id: true
2021-04-13 21:53:58 +02:00
},
2021-04-22 20:55:05 +02:00
take: 20,
where: {
NOT: {
Analytics: null
}
}
2021-04-13 21:53:58 +02:00
});
}
}