You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
4.4 KiB
184 lines
4.4 KiB
import { DataGatheringService } from '@ghostfolio/api/services/data-gathering.service';
|
|
import { PropertyDto } from '@ghostfolio/api/services/property/property.dto';
|
|
import { PropertyService } from '@ghostfolio/api/services/property/property.service';
|
|
import { PROPERTY_CURRENCIES } from '@ghostfolio/common/config';
|
|
import {
|
|
AdminData,
|
|
AdminMarketData,
|
|
AdminMarketDataDetails
|
|
} from '@ghostfolio/common/interfaces';
|
|
import {
|
|
getPermissions,
|
|
hasPermission,
|
|
permissions
|
|
} from '@ghostfolio/common/permissions';
|
|
import type { RequestWithUser } from '@ghostfolio/common/types';
|
|
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpException,
|
|
Inject,
|
|
Param,
|
|
Post,
|
|
Put,
|
|
UseGuards
|
|
} from '@nestjs/common';
|
|
import { REQUEST } from '@nestjs/core';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
import { DataSource } from '@prisma/client';
|
|
import { StatusCodes, getReasonPhrase } from 'http-status-codes';
|
|
|
|
import { AdminService } from './admin.service';
|
|
|
|
@Controller('admin')
|
|
export class AdminController {
|
|
public constructor(
|
|
private readonly adminService: AdminService,
|
|
private readonly dataGatheringService: DataGatheringService,
|
|
private readonly propertyService: PropertyService,
|
|
@Inject(REQUEST) private readonly request: RequestWithUser
|
|
) {}
|
|
|
|
@Get()
|
|
@UseGuards(AuthGuard('jwt'))
|
|
public async getAdminData(): Promise<AdminData> {
|
|
if (
|
|
!hasPermission(
|
|
getPermissions(this.request.user.role),
|
|
permissions.accessAdminControl
|
|
)
|
|
) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
return this.adminService.get();
|
|
}
|
|
|
|
@Post('gather/max')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
public async gatherMax(): Promise<void> {
|
|
if (
|
|
!hasPermission(
|
|
getPermissions(this.request.user.role),
|
|
permissions.accessAdminControl
|
|
)
|
|
) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
await this.dataGatheringService.gatherProfileData();
|
|
this.dataGatheringService.gatherMax();
|
|
|
|
return;
|
|
}
|
|
|
|
@Post('gather/:dataSource/:symbol')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
public async gatherSymbol(
|
|
@Param('dataSource') dataSource: DataSource,
|
|
@Param('symbol') symbol: string
|
|
): Promise<void> {
|
|
if (
|
|
!hasPermission(
|
|
getPermissions(this.request.user.role),
|
|
permissions.accessAdminControl
|
|
)
|
|
) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
this.dataGatheringService.gatherSymbol({ dataSource, symbol });
|
|
|
|
return;
|
|
}
|
|
|
|
@Post('gather/profile-data')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
public async gatherProfileData(): Promise<void> {
|
|
if (
|
|
!hasPermission(
|
|
getPermissions(this.request.user.role),
|
|
permissions.accessAdminControl
|
|
)
|
|
) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
this.dataGatheringService.gatherProfileData();
|
|
|
|
return;
|
|
}
|
|
|
|
@Get('market-data')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
public async getMarketData(): Promise<AdminMarketData> {
|
|
if (
|
|
!hasPermission(
|
|
getPermissions(this.request.user.role),
|
|
permissions.accessAdminControl
|
|
)
|
|
) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
return this.adminService.getMarketData();
|
|
}
|
|
|
|
@Get('market-data/:symbol')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
public async getMarketDataBySymbol(
|
|
@Param('symbol') symbol
|
|
): Promise<AdminMarketDataDetails> {
|
|
if (
|
|
!hasPermission(
|
|
getPermissions(this.request.user.role),
|
|
permissions.accessAdminControl
|
|
)
|
|
) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
return this.adminService.getMarketDataBySymbol(symbol);
|
|
}
|
|
|
|
@Put('settings/:key')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
public async updateProperty(
|
|
@Param('key') key: string,
|
|
@Body() data: PropertyDto
|
|
) {
|
|
if (
|
|
!hasPermission(
|
|
getPermissions(this.request.user.role),
|
|
permissions.accessAdminControl
|
|
)
|
|
) {
|
|
throw new HttpException(
|
|
getReasonPhrase(StatusCodes.FORBIDDEN),
|
|
StatusCodes.FORBIDDEN
|
|
);
|
|
}
|
|
|
|
return await this.adminService.putSetting(key, data.value);
|
|
}
|
|
}
|