Skip to content

Commit e99460a

Browse files
committed
added repo to enricher
1 parent afc4d5b commit e99460a

8 files changed

Lines changed: 51 additions & 134 deletions

File tree

packages/backend/src/enrichment/asset.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,35 @@ import {
44
type Action,
55
type ChartRange,
66
type EnrichedAsset,
7-
type EnrichedTx,
87
type GetAsset,
98
type Optional
109
} from "@darkruby/assets-core";
1110
import * as A from "fp-ts/lib/Array";
1211
import { pipe } from "fp-ts/lib/function";
1312
import * as TE from "fp-ts/lib/TaskEither";
13+
import type { Repository } from "../repository";
1414
import { type YahooApi } from "../yahoo/client";
1515
import { $enrichedAssetBase, $enrichedAssetCcy, txsWithRates } from "./returns";
16+
import { getTxsEnricher } from "./tx";
1617

1718
export const getAssetEnricher =
18-
(yahooApi: YahooApi) =>
19+
(yahooApi: YahooApi, repo: Repository) =>
1920
(
2021
asset: GetAsset,
21-
// getFinalStrecthTxs: () => Action<EnrichedTx[]>,
22-
getAllTxs: () => Action<EnrichedTx[]>,
2322
range: ChartRange = DEFAULT_CHART_RANGE
2423
): Action<EnrichedAsset> => {
24+
const enrichTxs = getTxsEnricher(yahooApi);
2525
return pipe(
2626
TE.Do,
2727
TE.bind("chart", () => yahooApi.chart(asset.ticker, range)),
28-
TE.bind("txs", getAllTxs),
28+
TE.bind("txs", () => repo.tx.getAll(asset.id, asset.user_id, false)),
29+
TE.bind("enrichedTxs", ({ txs }) => enrichTxs(txs)),
2930
TE.bind("fxRates", ({ chart }) =>
3031
yahooApi.fxRates(chart.meta.currency, asset.base_ccy)
3132
),
32-
TE.map(({ txs, fxRates, chart }) => {
33+
TE.map(({ enrichedTxs, fxRates, chart }) => {
3334
const { meta } = chart;
34-
const $txsWithRate = txsWithRates(txs, fxRates);
35+
const $txsWithRate = txsWithRates(enrichedTxs, fxRates);
3536
const ccy = $enrichedAssetCcy(asset, chart, $txsWithRate);
3637
const base = $enrichedAssetBase(
3738
asset,
@@ -53,31 +54,25 @@ export const getAssetEnricher =
5354
};
5455

5556
export const getAssetsEnricher =
56-
(yahooApi: YahooApi) =>
57-
(
58-
assets: GetAsset[],
59-
getEnrichedTxs: (asset: GetAsset) => Action<EnrichedTx[]>,
60-
range?: ChartRange
61-
): Action<EnrichedAsset[]> => {
62-
const assetTxs = (asset: GetAsset) => () => getEnrichedTxs(asset);
63-
const enrichAsset = getAssetEnricher(yahooApi);
57+
(yahooApi: YahooApi, repo: Repository) =>
58+
(assets: GetAsset[], range?: ChartRange): Action<EnrichedAsset[]> => {
59+
const enrichAsset = getAssetEnricher(yahooApi, repo);
6460
return pipe(
6561
assets,
66-
TE.traverseArray((asset) => enrichAsset(asset, assetTxs(asset), range)),
62+
TE.traverseArray((asset) => enrichAsset(asset, range)),
6763
TE.map((assets) => calcAssetWeights(assets as EnrichedAsset[]))
6864
) as Action<EnrichedAsset[]>;
6965
};
7066

7167
export const getOptionalAssetEnricher =
72-
(yahooApi: YahooApi) =>
68+
(yahooApi: YahooApi, repo: Repository) =>
7369
(
7470
asset: Optional<GetAsset>,
75-
getEnrichedTxs: () => Action<EnrichedTx[]>,
7671
range?: ChartRange
7772
): Action<Optional<EnrichedAsset>> => {
7873
if (asset) {
79-
const enrichAsset = getAssetEnricher(yahooApi);
80-
return enrichAsset(asset, getEnrichedTxs, range);
74+
const enrichAsset = getAssetEnricher(yahooApi, repo);
75+
return enrichAsset(asset, range);
8176
}
8277
return TE.of(null);
8378
};

packages/backend/src/enrichment/portfolio.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,36 @@ import {
77
type Action,
88
type ChartRange,
99
type EnrichedPortfolio,
10-
type EnrichedTx,
11-
type GetAsset,
1210
type GetPortfolio,
1311
type Optional,
1412
type PeriodChanges,
1513
type Totals,
1614
type UnixDate
1715
} from "@darkruby/assets-core";
1816
import * as A from "fp-ts/lib/Array";
19-
import { pipe, type FunctionN, type LazyArg } from "fp-ts/lib/function";
17+
import { pipe } from "fp-ts/lib/function";
2018
import * as Ord from "fp-ts/lib/Ord";
2119
import * as TE from "fp-ts/lib/TaskEither";
20+
import type { Repository } from "../repository";
2221
import type { YahooApi } from "../yahoo/client";
2322
import { calcAssetWeights, getAssetsEnricher } from "./asset";
2423
import { combineAssetCharts, commonAssetRanges } from "./chart";
2524

2625
export const getPortfolioEnricher =
27-
(yahooApi: YahooApi) =>
26+
(yahooApi: YahooApi, repo: Repository) =>
2827
(
2928
portfolio: GetPortfolio,
30-
getAssets: LazyArg<Action<GetAsset[]>>,
31-
getEnrichedTxs: (asset: GetAsset) => Action<EnrichedTx[]>,
3229
range: ChartRange = DEFAULT_CHART_RANGE
3330
): Action<EnrichedPortfolio> => {
34-
const enrichAssets = getAssetsEnricher(yahooApi);
31+
const enrichAssets = getAssetsEnricher(yahooApi, repo);
3532

3633
return pipe(
3734
TE.Do,
3835
TE.apS("portfolio", TE.of(portfolio)),
3936
TE.bind("assets", () =>
4037
pipe(
41-
getAssets(),
42-
TE.chain((a) => enrichAssets(a, getEnrichedTxs, range)),
38+
repo.asset.getAll(portfolio.id, portfolio.user_id),
39+
TE.chain((assets) => enrichAssets(assets, range)),
4340
TE.map(A.filter((a) => Boolean(a.invested))),
4441
TE.map(calcAssetWeights)
4542
)
@@ -154,35 +151,28 @@ export const getPortfolioEnricher =
154151
};
155152

156153
export const getPortfoliosEnricher =
157-
(yahooApi: YahooApi) =>
154+
(yahooApi: YahooApi, repo: Repository) =>
158155
(
159156
portfolios: GetPortfolio[],
160-
getAssets: FunctionN<[GetPortfolio], Action<GetAsset[]>>,
161-
getEnrichedTxs: FunctionN<[GetAsset, GetPortfolio], Action<EnrichedTx[]>>,
162157
range?: ChartRange
163158
): Action<EnrichedPortfolio[]> => {
164-
const enrichPortfolio = getPortfolioEnricher(yahooApi);
159+
const enrichPortfolio = getPortfolioEnricher(yahooApi, repo);
165160
return pipe(
166161
portfolios,
167-
TE.traverseArray((p) => {
168-
const getTxs = (asset: GetAsset) => getEnrichedTxs(asset, p);
169-
return enrichPortfolio(p, () => getAssets(p), getTxs, range);
170-
}),
162+
TE.traverseArray((p) => enrichPortfolio(p, range)),
171163
TE.map((ps) => calcPortfolioWeights(ps as EnrichedPortfolio[]))
172164
);
173165
};
174166

175167
export const getOptionalPorfolioEnricher =
176-
(yahooApi: YahooApi) =>
168+
(yahooApi: YahooApi, repo: Repository) =>
177169
(
178170
portfolio: Optional<GetPortfolio>,
179-
getAssets: () => Action<GetAsset[]>,
180-
getEnrichedTxs: (asset: GetAsset) => Action<EnrichedTx[]>,
181171
range?: ChartRange
182172
): Action<Optional<EnrichedPortfolio>> => {
183173
if (portfolio) {
184-
const enrichPortfolio = getPortfolioEnricher(yahooApi);
185-
return enrichPortfolio(portfolio, getAssets, getEnrichedTxs, range);
174+
const enrichPortfolio = getPortfolioEnricher(yahooApi, repo);
175+
return enrichPortfolio(portfolio, range);
186176
}
187177
return TE.of(null);
188178
};

packages/backend/src/enrichment/summary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const enrichSummary = (portfolios: EnrichedPortfolio[]): Summary => {
2525
);
2626
const validRanges = commonPortfolioRanges(portfolios);
2727
return { range, validRanges };
28-
})();
28+
})() satisfies EnrichedPortfolio["meta"];
2929

3030
const value: PeriodChanges = (() => {
3131
const startPrice = pipe(

packages/backend/src/enrichment/tx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const getTxEnricher =
3333

3434
export const getTxsEnricher =
3535
(yahooApi: YahooApi) =>
36-
(txs: GetTx[]): Action<readonly EnrichedTx[]> => {
36+
(txs: GetTx[]): Action<EnrichedTx[]> => {
3737
const enrichTx = getTxEnricher(yahooApi);
38-
return pipe(txs, TE.traverseSeqArray(enrichTx)); // <-- sequential to take advantage of yahoo cache
38+
return pipe(txs, TE.traverseSeqArray(enrichTx)) as Action<EnrichedTx[]>; // <-- sequential to take advantage of yahoo cache
3939
};

packages/backend/src/services/asset.ts

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import {
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";
1511
import { liftTE } from "@darkruby/assets-core/src/decoders/util";
16-
import { flow, pipe } from "fp-ts/function";
12+
import { pipe } from "fp-ts/function";
1713
import * as TE from "fp-ts/TaskEither";
1814
import { mapWebError } from "../domain/error";
1915
import {
@@ -24,17 +20,9 @@ import {
2420
import type { WebAction } from "../fp-express";
2521
import type { Repository } from "../repository";
2622
import type { YahooApi } from "../yahoo/client";
27-
import { getFinalStretchTxs } from "./tx";
2823

2924
const 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-
3826
export 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

Comments
 (0)