|
|
@ -8,83 +8,72 @@ import { DataSource } from '@prisma/client'; |
|
|
|
export class WatchlistService { |
|
|
|
public constructor(private readonly prismaService: PrismaService) {} |
|
|
|
|
|
|
|
public async getWatchlistItems( |
|
|
|
userId: string |
|
|
|
): Promise<AssetProfileIdentifier[]> { |
|
|
|
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<void> { |
|
|
|
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<AssetProfileIdentifier[]> { |
|
|
|
const user = await this.prismaService.user.findUnique({ |
|
|
|
select: { |
|
|
|
watchlist: { |
|
|
|
select: { dataSource: true, symbol: true } |
|
|
|
} |
|
|
|
}, |
|
|
|
where: { id: userId } |
|
|
|
}); |
|
|
|
|
|
|
|
return user.watchlist ?? []; |
|
|
|
} |
|
|
|
} |