11import {
2- handleError ,
32 PostAssetDecoder ,
4- type Action ,
53 type AssetId ,
64 type ChartRange ,
75 type EnrichedAsset ,
8- type EnrichedTx ,
9- type GetAsset ,
106 type Id ,
117 type Optional ,
128 type PortfolioId ,
139 type UserId
1410} from "@darkruby/assets-core" ;
1511import { liftTE } from "@darkruby/assets-core/src/decoders/util" ;
16- import { flow , pipe } from "fp-ts/function" ;
12+ import { pipe } from "fp-ts/function" ;
1713import * as TE from "fp-ts/TaskEither" ;
1814import { mapWebError } from "../domain/error" ;
1915import {
@@ -24,17 +20,9 @@ import {
2420import type { WebAction } from "../fp-express" ;
2521import type { Repository } from "../repository" ;
2622import type { YahooApi } from "../yahoo/client" ;
27- import { getFinalStretchTxs } from "./tx" ;
2823
2924const assetDecoder = liftTE ( PostAssetDecoder ) ;
3025
31- const getEnrichedTxs = ( repo : Repository , yahooApi : YahooApi ) =>
32- flow ( getFinalStretchTxs ( repo , yahooApi ) , TE . mapLeft ( handleError ( ) ) ) as (
33- assetId : AssetId ,
34- portfolioId : PortfolioId ,
35- userId : UserId
36- ) => Action < EnrichedTx [ ] > ;
37-
3826export const getAsset =
3927 ( repo : Repository , yahooApi : YahooApi ) =>
4028 (
@@ -43,13 +31,11 @@ export const getAsset =
4331 userId : UserId ,
4432 range : ChartRange
4533 ) : WebAction < Optional < EnrichedAsset > > => {
46- const enrichAsset = getOptionalAssetEnricher ( yahooApi ) ;
47- const getTxs = ( ) =>
48- getEnrichedTxs ( repo , yahooApi ) ( assetId , portfolioId , userId ) ;
34+ const enrichAsset = getOptionalAssetEnricher ( yahooApi , repo ) ;
4935 return pipe (
5036 TE . Do ,
5137 TE . bind ( "asset" , ( ) => repo . asset . get ( assetId , portfolioId , userId ) ) ,
52- TE . chain ( ( { asset } ) => enrichAsset ( asset , getTxs , range ) ) ,
38+ TE . chain ( ( { asset } ) => enrichAsset ( asset , range ) ) ,
5339 mapWebError
5440 ) ;
5541 } ;
@@ -61,14 +47,11 @@ export const getAssets =
6147 portfolioId : PortfolioId ,
6248 range : ChartRange
6349 ) : WebAction < readonly EnrichedAsset [ ] > => {
64- const enrichAssets = getAssetsEnricher ( yahooApi ) ;
65- const getTxs = ( asset : GetAsset ) =>
66- getEnrichedTxs ( repo , yahooApi ) ( asset . id , portfolioId , userId ) ;
67-
50+ const enrichAssets = getAssetsEnricher ( yahooApi , repo ) ;
6851 return pipe (
6952 TE . Do ,
7053 TE . bind ( "assets" , ( ) => repo . asset . getAll ( portfolioId , userId ) ) ,
71- TE . chain ( ( { assets } ) => enrichAssets ( assets , getTxs , range ) ) ,
54+ TE . chain ( ( { assets } ) => enrichAssets ( assets , range ) ) ,
7255 mapWebError
7356 ) ;
7457 } ;
@@ -94,17 +77,15 @@ export const createAsset =
9477 userId : UserId ,
9578 payload : unknown
9679 ) : WebAction < EnrichedAsset > => {
97- const enrichAsset = getAssetEnricher ( yahooApi ) ;
98- const getTxs = ( assetId : AssetId ) => ( ) =>
99- getEnrichedTxs ( repo , yahooApi ) ( assetId , portfolioId , userId ) ;
80+ const enrichAsset = getAssetEnricher ( yahooApi , repo ) ;
10081 return pipe (
10182 TE . Do ,
10283 TE . bind ( "asset" , ( ) => assetDecoder ( payload ) ) ,
10384 TE . tap ( ( { asset } ) => yahooApi . checkTickerExists ( asset . ticker ) ) ,
10485 TE . bind ( "created" , ( { asset } ) =>
10586 repo . asset . create ( asset , portfolioId , userId )
10687 ) ,
107- TE . chain ( ( { created } ) => enrichAsset ( created , getTxs ( created . id ) ) ) ,
88+ TE . chain ( ( { created } ) => enrichAsset ( created ) ) ,
10889 mapWebError
10990 ) ;
11091 } ;
@@ -117,17 +98,15 @@ export const updateAsset =
11798 userId : UserId ,
11899 payload : unknown
119100 ) : WebAction < EnrichedAsset > => {
120- const enrichAsset = getAssetEnricher ( yahooApi ) ;
121- const getTxs = ( ) =>
122- getEnrichedTxs ( repo , yahooApi ) ( assetId , portfolioId , userId ) ;
101+ const enrichAsset = getAssetEnricher ( yahooApi , repo ) ;
123102 return pipe (
124103 TE . Do ,
125104 TE . bind ( "asset" , ( ) => assetDecoder ( payload ) ) ,
126105 TE . tap ( ( { asset } ) => yahooApi . checkTickerExists ( asset . ticker ) ) ,
127106 TE . bind ( "updated" , ( { asset } ) =>
128107 repo . asset . update ( assetId , portfolioId , userId , asset )
129108 ) ,
130- TE . chain ( ( { updated } ) => enrichAsset ( updated , getTxs ) ) ,
109+ TE . chain ( ( { updated } ) => enrichAsset ( updated ) ) ,
131110 mapWebError
132111 ) ;
133112 } ;
0 commit comments