diff --git a/apps/api/src/app/app.module.ts b/apps/api/src/app/app.module.ts index 73f33128a..0aca4e62c 100644 --- a/apps/api/src/app/app.module.ts +++ b/apps/api/src/app/app.module.ts @@ -38,7 +38,7 @@ import { GhostfolioModule } from './endpoints/data-providers/ghostfolio/ghostfol import { MarketDataModule } from './endpoints/market-data/market-data.module'; import { PublicModule } from './endpoints/public/public.module'; import { TagsModule } from './endpoints/tags/tags.module'; -import { WatchlistModule } from './endpoints/watchlists/watchlist.module'; +import { WatchlistModule } from './endpoints/watchlist/watchlist.module'; import { ExchangeRateModule } from './exchange-rate/exchange-rate.module'; import { ExportModule } from './export/export.module'; import { HealthModule } from './health/health.module'; diff --git a/apps/api/src/app/endpoints/watchlists/create-watchlist-item.dto.ts b/apps/api/src/app/endpoints/watchlist/create-watchlist-item.dto.ts similarity index 83% rename from apps/api/src/app/endpoints/watchlists/create-watchlist-item.dto.ts rename to apps/api/src/app/endpoints/watchlist/create-watchlist-item.dto.ts index 663965ef1..ee2286727 100644 --- a/apps/api/src/app/endpoints/watchlists/create-watchlist-item.dto.ts +++ b/apps/api/src/app/endpoints/watchlist/create-watchlist-item.dto.ts @@ -2,7 +2,7 @@ import { DataSource } from '@prisma/client'; import { IsEnum, IsString } from 'class-validator'; export class CreateWatchlistItemDto { - @IsEnum(DataSource) + @IsEnum(DataSource, { each: true }) dataSource: DataSource; @IsString() diff --git a/apps/api/src/app/endpoints/watchlists/watchlist.controller.ts b/apps/api/src/app/endpoints/watchlist/watchlist.controller.ts similarity index 100% rename from apps/api/src/app/endpoints/watchlists/watchlist.controller.ts rename to apps/api/src/app/endpoints/watchlist/watchlist.controller.ts index 0e319d22e..a60a75b9e 100644 --- a/apps/api/src/app/endpoints/watchlists/watchlist.controller.ts +++ b/apps/api/src/app/endpoints/watchlist/watchlist.controller.ts @@ -29,13 +29,6 @@ export class WatchlistController { private readonly watchlistService: WatchlistService ) {} - @Get() - @HasPermission(permissions.readWatchlistItems) - @UseGuards(AuthGuard('jwt'), HasPermissionGuard) - public async getWatchlistItems(): Promise { - return this.watchlistService.getWatchlistItems(this.request.user.id); - } - @Post() @HasPermission(permissions.createWatchlistItem) @UseGuards(AuthGuard('jwt')) @@ -70,9 +63,16 @@ export class WatchlistController { } return this.watchlistService.deleteWatchlistItem({ - dataSource: watchlistItem.dataSource, symbol, + dataSource: watchlistItem.dataSource, userId: this.request.user.id }); } + + @Get() + @HasPermission(permissions.readWatchlistItems) + @UseGuards(AuthGuard('jwt'), HasPermissionGuard) + public async getWatchlistItems(): Promise { + return this.watchlistService.getWatchlistItems(this.request.user.id); + } } diff --git a/apps/api/src/app/endpoints/watchlists/watchlist.module.ts b/apps/api/src/app/endpoints/watchlist/watchlist.module.ts similarity index 100% rename from apps/api/src/app/endpoints/watchlists/watchlist.module.ts rename to apps/api/src/app/endpoints/watchlist/watchlist.module.ts diff --git a/apps/api/src/app/endpoints/watchlists/watchlist.service.ts b/apps/api/src/app/endpoints/watchlist/watchlist.service.ts similarity index 69% rename from apps/api/src/app/endpoints/watchlists/watchlist.service.ts rename to apps/api/src/app/endpoints/watchlist/watchlist.service.ts index 408b6fba0..2c7a743cb 100644 --- a/apps/api/src/app/endpoints/watchlists/watchlist.service.ts +++ b/apps/api/src/app/endpoints/watchlist/watchlist.service.ts @@ -8,83 +8,72 @@ import { DataSource } from '@prisma/client'; export class WatchlistService { public constructor(private readonly prismaService: PrismaService) {} - public async getWatchlistItems( - userId: string - ): Promise { - const user = await this.prismaService.user.findUnique({ - where: { - id: userId - }, - select: { - watchlist: { - select: { - dataSource: true, - symbol: true - } - } - } - }); - - if (!user) { - throw new NotFoundException( - `User watchlist with ID ${userId} not found.` - ); - } - - return user.watchlist ?? []; - } - public async createWatchlistItem({ - userId, dataSource, - symbol + symbol, + userId }: { - userId: string; dataSource: DataSource; symbol: string; + userId: string; }): Promise { const symbolProfile = await this.prismaService.symbolProfile.findUnique({ - where: { dataSource_symbol: { dataSource, symbol } } + where: { + dataSource_symbol: { dataSource, symbol } + } }); + if (!symbolProfile) { - throw new NotFoundException(`Symbol ${symbol} not found.`); + throw new NotFoundException( + `Asset profile not found for ${symbol} (${dataSource}).` + ); } + await this.prismaService.user.update({ - where: { id: userId }, data: { watchlist: { connect: { - dataSource_symbol: { - dataSource, - symbol - } + dataSource_symbol: { dataSource, symbol } } } - } + }, + where: { id: userId } }); } public async deleteWatchlistItem({ - userId, dataSource, - symbol + symbol, + userId }: { - userId: string; dataSource: DataSource; symbol: string; + userId: string; }) { await this.prismaService.user.update({ - where: { id: userId }, data: { watchlist: { disconnect: { - dataSource_symbol: { - dataSource, - symbol - } + dataSource_symbol: { dataSource, symbol } } } - } + }, + where: { id: userId } }); } + + public async getWatchlistItems( + userId: string + ): Promise { + const user = await this.prismaService.user.findUnique({ + select: { + watchlist: { + select: { dataSource: true, symbol: true } + } + }, + where: { id: userId } + }); + + return user.watchlist ?? []; + } }