diff options
Diffstat (limited to 'src/FundLab.Api/App.fs')
| -rw-r--r-- | src/FundLab.Api/App.fs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/FundLab.Api/App.fs b/src/FundLab.Api/App.fs index 38debc3..fc174e5 100644 --- a/src/FundLab.Api/App.fs +++ b/src/FundLab.Api/App.fs @@ -193,6 +193,27 @@ type CapitalDepositResponse = createdAt: string } +type FundReturnsPointResponse = + { + date: string + pending: bool + totalAssets: string option + unitNav: string option + cash: string + reservedCash: string + holdingsValue: string option + cumulativeReturn: string option + netExternalFlow: string + } + +type FundReturnsResponse = + { + fundId: Guid + pending: bool + dataUpdatedAt: string option + points: FundReturnsPointResponse list + } + type ApiErrorResponse = { error: string @@ -677,6 +698,40 @@ module App = with _ -> errorResponse 500 "PERSISTENCE_ERROR" "dividend persistence failed" next ctx + let private returnsPointResponse (point: FundReturnsPoint) : FundReturnsPointResponse = + { + date = dateText point.Date + pending = point.Pending + totalAssets = point.TotalAssets |> Option.map cashText + unitNav = point.UnitNav |> Option.map unitNavText + cash = cashText point.Cash + reservedCash = cashText point.ReservedCash + holdingsValue = point.HoldingsValue |> Option.map cashText + cumulativeReturn = point.CumulativeReturn |> Option.map cashText + netExternalFlow = cashText point.NetExternalFlow + } + + let private getFundReturns (repository: FundRepository) (fundIdText: string) : HttpHandler = + fun next ctx -> + match Guid.TryParse fundIdText with + | false, _ -> errorResponse 400 "INVALID_FUND_ID" "fund id must be a UUID" next ctx + | true, fundId -> + try + match repository.GetFundReturns fundId with + | None -> errorResponse 404 "FUND_NOT_FOUND" "fund was not found" next ctx + | Some returns -> + json + ({ + fundId = returns.FundId + pending = returns.Pending + dataUpdatedAt = returns.DataUpdatedAt |> Option.map timestampText + points = returns.Points |> List.map returnsPointResponse + } : FundReturnsResponse) + next + ctx + with _ -> + errorResponse 500 "PERSISTENCE_ERROR" "returns persistence failed" next ctx + @@ -1291,6 +1346,7 @@ module App = POST >=> routef "/funds/%s/rebalance/plans/%s/execute" (fun (fundId, planId) -> executeRebalancePlan repository fundId planId) POST >=> routef "/funds/%s/dividends" (createDividend repository) GET >=> routef "/funds/%s/dividends" (getDividends repository) + GET >=> routef "/funds/%s/returns" (getFundReturns repository) GET >=> routef "/funds/%s" (getFund repository) ] @ (marketData |> Option.map marketDataRoutes |> Option.defaultValue []) |
