Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Change UX to set an asset profile as a benchmark (#2409)

Manushreshta B L90e18338f638
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 71e888246a..54236f5051 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Harmonized the settings icon of the user account page
+- Improved the usability to set an asset profile as a benchmark
### Fixed
diff --git a/apps/api/src/app/benchmark/benchmark.controller.ts b/apps/api/src/app/benchmark/benchmark.controller.ts
index d59a231ffa..2230ff42bb 100644
--- a/apps/api/src/app/benchmark/benchmark.controller.ts
+++ b/apps/api/src/app/benchmark/benchmark.controller.ts
@@ -10,6 +10,7 @@ import type { RequestWithUser } from '@ghostfolio/common/types';
import {
Body,
Controller,
+ Delete,
Get,
HttpException,
Inject,
@@ -32,35 +33,49 @@ export class BenchmarkController {
@Inject(REQUEST) private readonly request: RequestWithUser
) {}
- @Get()
- @UseInterceptors(TransformDataSourceInRequestInterceptor)
- @UseInterceptors(TransformDataSourceInResponseInterceptor)
- public async getBenchmark(): Promise<BenchmarkResponse> {
- return {
- benchmarks: await this.benchmarkService.getBenchmarks()
- };
- }
-
- @Get(':dataSource/:symbol/:startDateString')
+ @Post()
@UseGuards(AuthGuard('jwt'))
- @UseInterceptors(TransformDataSourceInRequestInterceptor)
- public async getBenchmarkMarketDataBySymbol(
- @Param('dataSource') dataSource: DataSource,
- @Param('startDateString') startDateString: string,
- @Param('symbol') symbol: string
- ): Promise<BenchmarkMarketDataDetails> {
- const startDate = new Date(startDateString);
+ public async addBenchmark(@Body() { dataSource, symbol }: UniqueAsset) {
+ if (
+ !hasPermission(
+ this.request.user.permissions,
+ permissions.accessAdminControl
+ )
+ ) {
+ throw new HttpException(
+ getReasonPhrase(StatusCodes.FORBIDDEN),
+ StatusCodes.FORBIDDEN
+ );
+ }
- return this.benchmarkService.getMarketDataBySymbol({
- dataSource,
- startDate,
- symbol
- });
+ try {
+ const benchmark = await this.benchmarkService.addBenchmark({
+ dataSource,
+ symbol
+ });
+
+ if (!benchmark) {
+ throw new HttpException(
+ getReasonPhrase(StatusCodes.NOT_FOUND),
+ StatusCodes.NOT_FOUND
+ );
+ }
+
+ return benchmark;
+ } catch {
+ throw new HttpException(
+ getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR),
+ StatusCodes.INTERNAL_SERVER_ERROR
+ );
+ }
}
- @Post()
+ @Delete(':dataSource/:symbol')
@UseGuards(AuthGuard('jwt'))
- public async addBenchmark(@Body() { dataSource, symbol }: UniqueAsset) {
+ public async deleteBenchmark(
+ @Param('dataSource') dataSource: DataSource,
+ @Param('symbol') symbol: string
+ ) {
if (
!hasPermission(
this.request.user.permissions,
@@ -74,7 +89,7 @@ export class BenchmarkController {
}
try {
- const benchmark = await this.benchmarkService.addBenchmark({
+ const benchmark = await this.benchmarkService.deleteBenchmark({
dataSource,
symbol
});
@@ -94,4 +109,30 @@ export class BenchmarkController {
);
}
}
+
+ @Get()
+ @UseInterceptors(TransformDataSourceInRequestInterceptor)
+ @UseInterceptors(TransformDataSourceInResponseInterceptor)
+ public async getBenchmark(): Promise<BenchmarkResponse> {
+ return {
+ benchmarks: await this.benchmarkService.getBenchmarks()
+ };
+ }
+
+ @Get(':dataSource/:symbol/:startDateString')
+ @UseGuards(AuthGuard('jwt'))
+ @UseInterceptors(TransformDataSourceInRequestInterceptor)
+ public async getBenchmarkMarketDataBySymbol(
+ @Param('dataSource') dataSource: DataSource,
+ @Param('startDateString') startDateString: string,
+ @Param('symbol') symbol: string
+ ): Promise<BenchmarkMarketDataDetails> {
+ const startDate = new Date(startDateString);
+
+ return this.benchmarkService.getMarketDataBySymbol({
+ dataSource,
+ startDate,
+ symbol
+ });
+ }
}
diff --git a/apps/api/src/app/benchmark/benchmark.service.ts b/apps/api/src/app/benchmark/benchmark.service.ts
index 785c2801a4..7fe1911a48 100644
--- a/apps/api/src/app/benchmark/benchmark.service.ts
+++ b/apps/api/src/app/benchmark/benchmark.service.ts
@@ -245,6 +245,43 @@ export class BenchmarkService {
};
}
+ public async deleteBenchmark({
+ dataSource,
+ symbol
+ }: UniqueAsset): Promise<Partial<SymbolProfile>> {
+ const assetProfile = await this.prismaService.symbolProfile.findFirst({
+ where: {
+ dataSource,
+ symbol
+ }
+ });
+
+ if (!assetProfile) {
+ return null;
+ }
+
+ let benchmarks =
+ ((await this.propertyService.getByKey(
+ PROPERTY_BENCHMARKS
+ )) as BenchmarkProperty[]) ?? [];
+
+ benchmarks = benchmarks.filter(({ symbolProfileId }) => {
+ return symbolProfileId !== assetProfile.id;
+ });
+
+ await this.propertyService.put({
+ key: PROPERTY_BENCHMARKS,
+ value: JSON.stringify(benchmarks)
+ });
+
+ return {
+ dataSource,
+ symbol,
+ id: assetProfile.id,
+ name: assetProfile.name
+ };
+ }
+
private getMarketCondition(aPerformanceInPercent: number) {
return aPerformanceInPercent <= -0.2 ? 'BEAR_MARKET' : 'NEUTRAL_MARKET';
}
diff --git a/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.component.ts b/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.component.ts
index be1892e911..bef984729f 100644
--- a/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.component.ts
+++ b/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.component.ts
@@ -146,9 +146,11 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
.postBenchmark({ dataSource, symbol })
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
- setTimeout(() => {
- window.location.reload();
- }, 300);
+ this.dataService.updateInfo();
+
+ this.isBenchmark = true;
+
+ this.changeDetectorRef.markForCheck();
});
}
@@ -185,6 +187,19 @@ export class AssetProfileDialog implements OnDestroy, OnInit {
});
}
+ public onUnsetBenchmark({ dataSource, symbol }: UniqueAsset) {
+ this.dataService
+ .deleteBenchmark({ dataSource, symbol })
+ .pipe(takeUntil(this.unsubscribeSubject))
+ .subscribe(() => {
+ this.dataService.updateInfo();
+
+ this.isBenchmark = false;
+
+ this.changeDetectorRef.markForCheck();
+ });
+ }
+
public ngOnDestroy() {
this.unsubscribeSubject.next();
this.unsubscribeSubject.complete();
diff --git a/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.html b/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.html
index be99df7cb8..6682d004dc 100644
--- a/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.html
+++ b/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.html
@@ -37,13 +37,6 @@
>
<ng-container i18n>Gather Profile Data</ng-container>
</button>
- <button
- mat-menu-item
- [disabled]="isBenchmark"
- (click)="onSetBenchmark({dataSource: data.dataSource, symbol: data.symbol})"
- >
- <ng-container i18n>Set as Benchmark</ng-container>
- </button>
</mat-menu>
</div>
@@ -151,6 +144,17 @@
</ng-template>
</ng-container>
</div>
+ <div class="d-flex my-3">
+ <div class="w-50">
+ <mat-checkbox
+ color="primary"
+ i18n
+ [checked]="isBenchmark"
+ (change)="isBenchmark ? onUnsetBenchmark({dataSource: data.dataSource, symbol: data.symbol}) : onSetBenchmark({dataSource: data.dataSource, symbol: data.symbol})"
+ >Benchmark</mat-checkbox
+ >
+ </div>
+ </div>
<div class="mt-3">
<mat-form-field appearance="outline" class="w-100">
<mat-label i18n>Symbol Mapping</mat-label>
diff --git a/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.module.ts b/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.module.ts
index 8672342b0e..1911f5a475 100644
--- a/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.module.ts
+++ b/apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.module.ts
@@ -3,6 +3,7 @@ import { CommonModule } from '@angular/common';
import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
+import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatDialogModule } from '@angular/material/dialog';
import { MatInputModule } from '@angular/material/input';
import { MatMenuModule } from '@angular/material/menu';
@@ -21,6 +22,7 @@ import { AssetProfileDialog } from './asset-profile-dialog.component';
GfPortfolioProportionChartModule,
GfValueModule,
MatButtonModule,
+ MatCheckboxModule,
MatDialogModule,
MatInputModule,
MatMenuModule,
diff --git a/apps/client/src/app/services/data.service.ts b/apps/client/src/app/services/data.service.ts
index 82b4acca0d..5cc955af22 100644
--- a/apps/client/src/app/services/data.service.ts
+++ b/apps/client/src/app/services/data.service.ts
@@ -204,6 +204,10 @@ export class DataService {
return this.http.delete<any>(`/api/v1/order/`);
}
+ public deleteBenchmark({ dataSource, symbol }: UniqueAsset) {
+ return this.http.delete<any>(`/api/v1/benchmark/${dataSource}/${symbol}`);
+ }
+
public deleteOrder(aId: string) {
return this.http.delete<any>(`/api/v1/order/${aId}`);
}
@@ -496,4 +500,19 @@ export class DataService {
couponCode
});
}
+
+ public updateInfo() {
+ this.http.get<InfoItem>('/api/v1/info').subscribe((info) => {
+ const utmSource = <'ios' | 'trusted-web-activity'>(
+ window.localStorage.getItem('utm_source')
+ );
+
+ info.globalPermissions = filterGlobalPermissions(
+ info.globalPermissions,
+ utmSource
+ );
+
+ (window as any).info = info;
+ });
+ }
}