diff --git a/CHANGELOG.md b/CHANGELOG.md index 742e3bdaa..7c898c20d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Changed + +- Removed the `currency` and `viewMode` from the `User` database schema + ### Fixed - Allowed the date range change for the demo user diff --git a/apps/api/src/app/order/order.controller.ts b/apps/api/src/app/order/order.controller.ts index 1d62e79a3..e3cafefca 100644 --- a/apps/api/src/app/order/order.controller.ts +++ b/apps/api/src/app/order/order.controller.ts @@ -103,7 +103,7 @@ export class OrderController { impersonationId, this.request.user.id ); - const userCurrency = this.request.user.Settings.currency; + const userCurrency = this.request.user.Settings.settings.baseCurrency; let activities = await this.orderService.getOrders({ filters, diff --git a/apps/api/src/app/portfolio/portfolio.controller.ts b/apps/api/src/app/portfolio/portfolio.controller.ts index 2f2ae0841..fc443ab93 100644 --- a/apps/api/src/app/portfolio/portfolio.controller.ts +++ b/apps/api/src/app/portfolio/portfolio.controller.ts @@ -40,7 +40,6 @@ import { } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { AuthGuard } from '@nestjs/passport'; -import { ViewMode } from '@prisma/client'; import { StatusCodes, getReasonPhrase } from 'http-status-codes'; import { PortfolioPositionDetail } from './interfaces/portfolio-position-detail.interface'; @@ -196,7 +195,7 @@ export class PortfolioController { return this.exchangeRateDataService.toCurrency( portfolioPosition.quantity * portfolioPosition.marketPrice, portfolioPosition.currency, - this.request.user.Settings.currency + this.request.user.Settings.settings.baseCurrency ); }) .reduce((a, b) => a + b, 0); @@ -299,7 +298,7 @@ export class PortfolioController { if ( impersonationId || - this.request.user.Settings.viewMode === ViewMode.ZEN || + this.request.user.Settings.settings.viewMode === 'ZEN' || this.userService.isRestrictedView(this.request.user) ) { performanceInformation.performance = nullifyValuesInObject( @@ -379,7 +378,8 @@ export class PortfolioController { return this.exchangeRateDataService.toCurrency( portfolioPosition.quantity * portfolioPosition.marketPrice, portfolioPosition.currency, - this.request.user?.Settings?.currency ?? this.baseCurrency + this.request.user?.Settings?.settings.baseCurrency ?? + this.baseCurrency ); }) .reduce((a, b) => a + b, 0); diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index d5b938283..d8aed4c17 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -124,7 +124,7 @@ export class PortfolioService { this.getDetails(aUserId, aUserId, undefined, aFilters) ]); - const userCurrency = this.request.user.Settings.currency; + const userCurrency = this.request.user.Settings.settings.baseCurrency; return accounts.map((account) => { let transactionCount = 0; @@ -199,7 +199,7 @@ export class PortfolioService { }); const portfolioCalculator = new PortfolioCalculator({ - currency: this.request.user.Settings.currency, + currency: this.request.user.Settings.settings.baseCurrency, currentRateService: this.currentRateService, orders: portfolioOrders }); @@ -279,7 +279,7 @@ export class PortfolioService { }); const portfolioCalculator = new PortfolioCalculator({ - currency: this.request.user.Settings.currency, + currency: this.request.user.Settings.settings.baseCurrency, currentRateService: this.currentRateService, orders: portfolioOrders }); @@ -368,7 +368,7 @@ export class PortfolioService { }); const portfolioCalculator = new PortfolioCalculator({ - currency: this.request.user.Settings.currency, + currency: this.request.user.Settings.settings.baseCurrency, currentRateService: this.currentRateService, orders: portfolioOrders }); @@ -441,8 +441,8 @@ export class PortfolioService { (user.Settings?.settings as UserSettings)?.emergencyFund ?? 0 ); const userCurrency = - user.Settings?.currency ?? - this.request.user?.Settings?.currency ?? + user.Settings?.settings.baseCurrency ?? + this.request.user?.Settings?.settings.baseCurrency ?? this.baseCurrency; const { orders, portfolioOrders, transactionPoints } = @@ -602,7 +602,7 @@ export class PortfolioService { aImpersonationId: string, aSymbol: string ): Promise { - const userCurrency = this.request.user.Settings.currency; + const userCurrency = this.request.user.Settings.settings.baseCurrency; const userId = await this.getUserId(aImpersonationId, this.request.user.id); const orders = ( @@ -855,7 +855,7 @@ export class PortfolioService { }); const portfolioCalculator = new PortfolioCalculator({ - currency: this.request.user.Settings.currency, + currency: this.request.user.Settings.settings.baseCurrency, currentRateService: this.currentRateService, orders: portfolioOrders }); @@ -931,7 +931,7 @@ export class PortfolioService { }); const portfolioCalculator = new PortfolioCalculator({ - currency: this.request.user.Settings.currency, + currency: this.request.user.Settings.settings.baseCurrency, currentRateService: this.currentRateService, orders: portfolioOrders }); @@ -991,7 +991,7 @@ export class PortfolioService { } public async getReport(impersonationId: string): Promise { - const currency = this.request.user.Settings.currency; + const currency = this.request.user.Settings.settings.baseCurrency; const userId = await this.getUserId(impersonationId, this.request.user.id); const { orders, portfolioOrders, transactionPoints } = @@ -1083,7 +1083,7 @@ export class PortfolioService { } public async getSummary(aImpersonationId: string): Promise { - const userCurrency = this.request.user.Settings.currency; + const userCurrency = this.request.user.Settings.settings.baseCurrency; const userId = await this.getUserId(aImpersonationId, this.request.user.id); const user = await this.userService.user({ id: userId }); @@ -1257,7 +1257,7 @@ export class PortfolioService { return this.exchangeRateDataService.toCurrency( new Big(order.quantity).mul(order.unitPrice).toNumber(), order.SymbolProfile.currency, - this.request.user.Settings.currency + this.request.user.Settings.settings.baseCurrency ); }) .reduce( @@ -1276,7 +1276,7 @@ export class PortfolioService { return this.exchangeRateDataService.toCurrency( order.fee, order.SymbolProfile.currency, - this.request.user.Settings.currency + this.request.user.Settings.settings.baseCurrency ); }) .reduce( @@ -1298,7 +1298,7 @@ export class PortfolioService { return this.exchangeRateDataService.toCurrency( new Big(order.quantity).mul(order.unitPrice).toNumber(), order.SymbolProfile.currency, - this.request.user.Settings.currency + this.request.user.Settings.settings.baseCurrency ); }) .reduce( @@ -1339,7 +1339,7 @@ export class PortfolioService { portfolioOrders: PortfolioOrder[]; }> { const userCurrency = - this.request.user?.Settings?.currency ?? this.baseCurrency; + this.request.user?.Settings?.settings.baseCurrency ?? this.baseCurrency; const orders = await this.orderService.getOrders({ filters, 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 7b850238d..0f51f43fd 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,5 @@ import { UniqueAsset } from '@ghostfolio/common/interfaces'; -import type { DateRange } from '@ghostfolio/common/types'; -import { ViewMode } from '@prisma/client'; +import type { DateRange, ViewMode } from '@ghostfolio/common/types'; import { IsBoolean, IsIn, diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts index 9ff37de89..13f82aa47 100644 --- a/apps/api/src/app/user/user.service.ts +++ b/apps/api/src/app/user/user.service.ts @@ -69,8 +69,8 @@ export class UserService { }), accounts: Account, settings: { - ...((Settings.settings)), - locale: ((Settings.settings))?.locale ?? aLocale + ...(Settings.settings), + locale: (Settings.settings)?.locale ?? aLocale } }; } @@ -88,10 +88,7 @@ export class UserService { } public isRestrictedView(aUser: UserWithSettings) { - return ( - (aUser.Settings.settings as unknown as UserSettings)?.isRestrictedView ?? - false - ); + return aUser.Settings.settings.isRestrictedView ?? false; } public async user( @@ -134,11 +131,9 @@ export class UserService { } else if (user) { // Set default settings if needed user.Settings = { - currency: null, settings: {}, updatedAt: new Date(), - userId: user?.id, - viewMode: 'DEFAULT' + userId: user?.id }; } @@ -239,7 +234,9 @@ export class UserService { }, Settings: { create: { - currency: this.baseCurrency + settings: { + currency: this.baseCurrency + } } } } diff --git a/apps/api/src/services/exchange-rate-data.service.ts b/apps/api/src/services/exchange-rate-data.service.ts index 8808698f5..7900fc781 100644 --- a/apps/api/src/services/exchange-rate-data.service.ts +++ b/apps/api/src/services/exchange-rate-data.service.ts @@ -166,21 +166,6 @@ export class ExchangeRateDataService { currencies.push(account.currency); }); - ( - await this.prismaService.settings.findMany({ - distinct: ['currency'], - orderBy: [{ currency: 'asc' }], - select: { currency: true }, - where: { - currency: { - not: null - } - } - }) - ).forEach((userSettings) => { - currencies.push(userSettings.currency); - }); - ( await this.prismaService.symbolProfile.findMany({ distinct: ['currency'], diff --git a/apps/client/src/app/core/auth.guard.ts b/apps/client/src/app/core/auth.guard.ts index 2daf2fab7..1087fa4a3 100644 --- a/apps/client/src/app/core/auth.guard.ts +++ b/apps/client/src/app/core/auth.guard.ts @@ -7,7 +7,6 @@ import { } from '@angular/router'; import { SettingsStorageService } from '@ghostfolio/client/services/settings-storage.service'; import { UserService } from '@ghostfolio/client/services/user/user.service'; -import { ViewMode } from '@prisma/client'; import { EMPTY } from 'rxjs'; import { catchError } from 'rxjs/operators'; @@ -80,13 +79,13 @@ export class AuthGuard implements CanActivate { return; } else if ( state.url.startsWith('/home') && - user.settings.viewMode === ViewMode.ZEN + user.settings.viewMode === 'ZEN' ) { this.router.navigate(['/zen']); resolve(false); return; } else if (state.url.startsWith('/start')) { - if (user.settings.viewMode === ViewMode.ZEN) { + if (user.settings.viewMode === 'ZEN') { this.router.navigate(['/zen']); } else { this.router.navigate(['/home']); @@ -96,7 +95,7 @@ export class AuthGuard implements CanActivate { return; } else if ( state.url.startsWith('/zen') && - user.settings.viewMode === ViewMode.DEFAULT + user.settings.viewMode === 'DEFAULT' ) { this.router.navigate(['/home']); resolve(false); diff --git a/libs/common/src/lib/interfaces/user-settings.interface.ts b/libs/common/src/lib/interfaces/user-settings.interface.ts index a88b88192..ac120aa15 100644 --- a/libs/common/src/lib/interfaces/user-settings.interface.ts +++ b/libs/common/src/lib/interfaces/user-settings.interface.ts @@ -1,5 +1,4 @@ -import { DateRange } from '@ghostfolio/common/types'; -import { ViewMode } from '@prisma/client'; +import { DateRange, ViewMode } from '@ghostfolio/common/types'; import { UniqueAsset } from './unique-asset.interface'; diff --git a/libs/common/src/lib/interfaces/user-with-settings.ts b/libs/common/src/lib/interfaces/user-with-settings.ts index 8c6fce1fb..443acba40 100644 --- a/libs/common/src/lib/interfaces/user-with-settings.ts +++ b/libs/common/src/lib/interfaces/user-with-settings.ts @@ -1,10 +1,11 @@ import { SubscriptionType } from '@ghostfolio/common/types/subscription.type'; import { Account, Settings, User } from '@prisma/client'; +import { UserSettings } from './user-settings.interface'; export type UserWithSettings = User & { Account: Account[]; permissions?: string[]; - Settings: Settings; + Settings: Settings & { settings: UserSettings }; subscription?: { expiresAt?: Date; type: SubscriptionType; diff --git a/libs/common/src/lib/types/index.ts b/libs/common/src/lib/types/index.ts index 30504dedf..54ef29ef4 100644 --- a/libs/common/src/lib/types/index.ts +++ b/libs/common/src/lib/types/index.ts @@ -8,6 +8,7 @@ import { Market } from './market.type'; import type { OrderWithAccount } from './order-with-account.type'; import type { RequestWithUser } from './request-with-user.type'; import { ToggleOption } from './toggle-option.type'; +import type { ViewMode } from './view-mode.type'; export type { AccessWithGranteeUser, @@ -19,5 +20,6 @@ export type { MarketState, OrderWithAccount, RequestWithUser, - ToggleOption + ToggleOption, + ViewMode }; diff --git a/libs/common/src/lib/types/view-mode.type.ts b/libs/common/src/lib/types/view-mode.type.ts new file mode 100644 index 000000000..ad38adb0a --- /dev/null +++ b/libs/common/src/lib/types/view-mode.type.ts @@ -0,0 +1 @@ +export type ViewMode = 'DEFAULT' | 'ZEN'; diff --git a/prisma/migrations/20220910135140_removed_currency_and_view_mode_from_user/migration.sql b/prisma/migrations/20220910135140_removed_currency_and_view_mode_from_user/migration.sql new file mode 100644 index 000000000..60da96357 --- /dev/null +++ b/prisma/migrations/20220910135140_removed_currency_and_view_mode_from_user/migration.sql @@ -0,0 +1,3 @@ +-- AlterTable +ALTER TABLE "Settings" DROP COLUMN "currency", +DROP COLUMN "viewMode"; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 34cbc9003..bc7ca28f7 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -102,10 +102,8 @@ model Property { } model Settings { - currency String? /// @deprecated settings Json? updatedAt DateTime @updatedAt - viewMode ViewMode? /// @deprecated userId String @id User User @relation(fields: [userId], references: [id]) }