From b5f90bba8175c9138cdd77c756d0fe8b39ff755c Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Fri, 9 Sep 2022 21:08:10 +0200 Subject: [PATCH] Migrate date range to user settings --- .../src/app/user/update-user-setting.dto.ts | 4 ++ apps/api/src/app/user/user.service.ts | 22 ++++++++--- .../home-holdings/home-holdings.component.ts | 34 ++++++++--------- .../home-holdings/home-holdings.html | 4 +- .../home-overview/home-overview.component.ts | 37 ++++++++++--------- .../home-overview/home-overview.html | 2 +- .../lib/interfaces/user-settings.interface.ts | 4 +- 7 files changed, 62 insertions(+), 45 deletions(-) diff --git a/apps/api/src/app/user/update-user-setting.dto.ts b/apps/api/src/app/user/update-user-setting.dto.ts index 1449bd64f..d29fd6d5a 100644 --- a/apps/api/src/app/user/update-user-setting.dto.ts +++ b/apps/api/src/app/user/update-user-setting.dto.ts @@ -1,6 +1,10 @@ import { IsBoolean, IsNumber, IsOptional, IsString } from 'class-validator'; export class UpdateUserSettingDto { + @IsOptional() + @IsString() // TODO: DateRange + dateRange?: string; + @IsNumber() @IsOptional() emergencyFund?: number; diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts index 49159532b..36ebacb8a 100644 --- a/apps/api/src/app/user/user.service.ts +++ b/apps/api/src/app/user/user.service.ts @@ -4,7 +4,11 @@ import { PrismaService } from '@ghostfolio/api/services/prisma.service'; import { PropertyService } from '@ghostfolio/api/services/property/property.service'; import { TagService } from '@ghostfolio/api/services/tag/tag.service'; import { PROPERTY_IS_READ_ONLY_MODE, locale } from '@ghostfolio/common/config'; -import { User as IUser, UserWithSettings } from '@ghostfolio/common/interfaces'; +import { + User as IUser, + UserSettings, + UserWithSettings +} from '@ghostfolio/common/interfaces'; import { getPermissions, hasRole, @@ -15,7 +19,6 @@ import { Prisma, Role, User, ViewMode } from '@prisma/client'; import { sortBy } from 'lodash'; import { UserSettingsParams } from './interfaces/user-settings-params.interface'; -import { UserSettings } from './interfaces/user-settings.interface'; const crypto = require('crypto'); @@ -68,9 +71,13 @@ export class UserService { }), accounts: Account, settings: { - ...(Settings.settings), + ...((Settings.settings)), baseCurrency: Settings?.currency ?? UserService.DEFAULT_CURRENCY, - locale: (Settings.settings)?.locale ?? aLocale, + dateRange: + Settings?.viewMode === 'ZEN' + ? 'max' + : ((Settings.settings))?.dateRange ?? 'max', + locale: ((Settings.settings))?.locale ?? aLocale, viewMode: Settings?.viewMode ?? ViewMode.DEFAULT } }; @@ -89,7 +96,10 @@ export class UserService { } public isRestrictedView(aUser: UserWithSettings) { - return (aUser.Settings.settings as UserSettings)?.isRestrictedView ?? false; + return ( + (aUser.Settings.settings as unknown as UserSettings)?.isRestrictedView ?? + false + ); } public async user( @@ -295,7 +305,7 @@ export class UserService { userId: string; userSettings: UserSettings; }) { - const settings = userSettings as Prisma.JsonObject; + const settings = userSettings as unknown as Prisma.JsonObject; await this.prismaService.settings.upsert({ create: { diff --git a/apps/client/src/app/components/home-holdings/home-holdings.component.ts b/apps/client/src/app/components/home-holdings/home-holdings.component.ts index fff2529d4..a900a5412 100644 --- a/apps/client/src/app/components/home-holdings/home-holdings.component.ts +++ b/apps/client/src/app/components/home-holdings/home-holdings.component.ts @@ -5,10 +5,6 @@ import { PositionDetailDialog } from '@ghostfolio/client/components/position/pos import { ToggleComponent } from '@ghostfolio/client/components/toggle/toggle.component'; import { DataService } from '@ghostfolio/client/services/data.service'; import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service'; -import { - RANGE, - SettingsStorageService -} from '@ghostfolio/client/services/settings-storage.service'; import { UserService } from '@ghostfolio/client/services/user/user.service'; import { Position, User } from '@ghostfolio/common/interfaces'; import { hasPermission, permissions } from '@ghostfolio/common/permissions'; @@ -26,7 +22,6 @@ import { PositionDetailDialogParams } from '../position/position-detail-dialog/i templateUrl: './home-holdings.html' }) export class HomeHoldingsComponent implements OnDestroy, OnInit { - public dateRange: DateRange; public dateRangeOptions = ToggleComponent.DEFAULT_DATE_RANGE_OPTIONS; public deviceType: string; public hasImpersonationId: boolean; @@ -44,7 +39,6 @@ export class HomeHoldingsComponent implements OnDestroy, OnInit { private impersonationStorageService: ImpersonationStorageService, private route: ActivatedRoute, private router: Router, - private settingsStorageService: SettingsStorageService, private userService: UserService ) { this.route.queryParams @@ -73,7 +67,7 @@ export class HomeHoldingsComponent implements OnDestroy, OnInit { permissions.createOrder ); - this.changeDetectorRef.markForCheck(); + this.update(); } }); } @@ -88,18 +82,24 @@ export class HomeHoldingsComponent implements OnDestroy, OnInit { this.hasImpersonationId = !!aId; }); - this.dateRange = - this.user.settings.viewMode === 'ZEN' - ? 'max' - : this.settingsStorageService.getSetting(RANGE) ?? 'max'; - this.update(); } - public onChangeDateRange(aDateRange: DateRange) { - this.dateRange = aDateRange; - this.settingsStorageService.setSetting(RANGE, this.dateRange); - this.update(); + public onChangeDateRange(dateRange: DateRange) { + this.dataService + .putUserSetting({ dateRange }) + .pipe(takeUntil(this.unsubscribeSubject)) + .subscribe(() => { + this.userService.remove(); + + this.userService + .get() + .pipe(takeUntil(this.unsubscribeSubject)) + .subscribe((user) => { + this.user = user; + this.changeDetectorRef.markForCheck(); + }); + }); } public ngOnDestroy() { @@ -151,7 +151,7 @@ export class HomeHoldingsComponent implements OnDestroy, OnInit { this.positions = undefined; this.dataService - .fetchPositions({ range: this.dateRange }) + .fetchPositions({ range: this.user?.settings?.dateRange }) .pipe(takeUntil(this.unsubscribeSubject)) .subscribe((response) => { this.positions = response.positions; diff --git a/apps/client/src/app/components/home-holdings/home-holdings.html b/apps/client/src/app/components/home-holdings/home-holdings.html index 5fa1ec5c9..3740f0c6b 100644 --- a/apps/client/src/app/components/home-holdings/home-holdings.html +++ b/apps/client/src/app/components/home-holdings/home-holdings.html @@ -1,7 +1,7 @@
diff --git a/apps/client/src/app/components/home-overview/home-overview.component.ts b/apps/client/src/app/components/home-overview/home-overview.component.ts index 5d4fd6aa2..c03110d75 100644 --- a/apps/client/src/app/components/home-overview/home-overview.component.ts +++ b/apps/client/src/app/components/home-overview/home-overview.component.ts @@ -2,10 +2,6 @@ import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; import { ToggleComponent } from '@ghostfolio/client/components/toggle/toggle.component'; import { DataService } from '@ghostfolio/client/services/data.service'; import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service'; -import { - RANGE, - SettingsStorageService -} from '@ghostfolio/client/services/settings-storage.service'; import { UserService } from '@ghostfolio/client/services/user/user.service'; import { LineChartItem, @@ -25,7 +21,6 @@ import { takeUntil } from 'rxjs/operators'; templateUrl: './home-overview.html' }) export class HomeOverviewComponent implements OnDestroy, OnInit { - public dateRange: DateRange; public dateRangeOptions = ToggleComponent.DEFAULT_DATE_RANGE_OPTIONS; public deviceType: string; public errors: UniqueAsset[]; @@ -47,7 +42,6 @@ export class HomeOverviewComponent implements OnDestroy, OnInit { private dataService: DataService, private deviceService: DeviceDetectorService, private impersonationStorageService: ImpersonationStorageService, - private settingsStorageService: SettingsStorageService, private userService: UserService ) { this.userService.stateChanged @@ -61,7 +55,7 @@ export class HomeOverviewComponent implements OnDestroy, OnInit { permissions.createOrder ); - this.changeDetectorRef.markForCheck(); + this.update(); } }); } @@ -78,11 +72,6 @@ export class HomeOverviewComponent implements OnDestroy, OnInit { this.changeDetectorRef.markForCheck(); }); - this.dateRange = - this.user.settings.viewMode === 'ZEN' - ? 'max' - : this.settingsStorageService.getSetting(RANGE) ?? 'max'; - this.showDetails = !this.hasImpersonationId && !this.user.settings.isRestrictedView && @@ -91,10 +80,22 @@ export class HomeOverviewComponent implements OnDestroy, OnInit { this.update(); } - public onChangeDateRange(aDateRange: DateRange) { - this.dateRange = aDateRange; - this.settingsStorageService.setSetting(RANGE, this.dateRange); - this.update(); + public onChangeDateRange(dateRange: DateRange) { + this.dataService + .putUserSetting({ dateRange }) + .pipe(takeUntil(this.unsubscribeSubject)) + .subscribe(() => { + this.userService.remove(); + + this.userService + .get() + .pipe(takeUntil(this.unsubscribeSubject)) + .subscribe((user) => { + this.user = user; + + this.changeDetectorRef.markForCheck(); + }); + }); } public ngOnDestroy() { @@ -107,7 +108,7 @@ export class HomeOverviewComponent implements OnDestroy, OnInit { this.dataService .fetchChart({ - range: this.dateRange, + range: this.user?.settings?.dateRange, version: this.user?.settings?.isExperimentalFeatures ? 2 : 1 }) .pipe(takeUntil(this.unsubscribeSubject)) @@ -125,7 +126,7 @@ export class HomeOverviewComponent implements OnDestroy, OnInit { }); this.dataService - .fetchPortfolioPerformance({ range: this.dateRange }) + .fetchPortfolioPerformance({ range: this.user?.settings?.dateRange }) .pipe(takeUntil(this.unsubscribeSubject)) .subscribe((response) => { this.errors = response.errors; diff --git a/apps/client/src/app/components/home-overview/home-overview.html b/apps/client/src/app/components/home-overview/home-overview.html index a032dedf9..0a47689c8 100644 --- a/apps/client/src/app/components/home-overview/home-overview.html +++ b/apps/client/src/app/components/home-overview/home-overview.html @@ -46,7 +46,7 @@ >