From 90e18338f6383958cd1ce0427de1e6ca0025ac8d Mon Sep 17 00:00:00 2001 From: Manushreshta B L <77065548+M27afk@users.noreply.github.com> Date: Tue, 3 Oct 2023 00:20:12 +0530 Subject: [PATCH] Change UX to set an asset profile as a benchmark (#2409) * Add checkbox functionality to set / unset benchmark * Update changelog --------- Co-authored-by: Thomas <4159106+dtslvr@users.noreply.github.com> --- CHANGELOG.md | 1 + .../src/app/benchmark/benchmark.controller.ts | 91 ++++++++++++++----- .../src/app/benchmark/benchmark.service.ts | 37 ++++++++ .../asset-profile-dialog.component.ts | 21 ++++- .../asset-profile-dialog.html | 18 ++-- .../asset-profile-dialog.module.ts | 2 + apps/client/src/app/services/data.service.ts | 19 ++++ 7 files changed, 154 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71e888246..54236f505 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 d59a231ff..2230ff42b 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 { - 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 { - 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 { + 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 { + 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 785c2801a..7fe1911a4 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> { + 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 be1892e91..bef984729 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 be99df7cb..6682d004d 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 @@ > Gather Profile Data - @@ -151,6 +144,17 @@ +
+
+ Benchmark +
+
Symbol Mapping 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 8672342b0..1911f5a47 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 82b4acca0..5cc955af2 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(`/api/v1/order/`); } + public deleteBenchmark({ dataSource, symbol }: UniqueAsset) { + return this.http.delete(`/api/v1/benchmark/${dataSource}/${symbol}`); + } + public deleteOrder(aId: string) { return this.http.delete(`/api/v1/order/${aId}`); } @@ -496,4 +500,19 @@ export class DataService { couponCode }); } + + public updateInfo() { + this.http.get('/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; + }); + } }