diff options
Diffstat (limited to 'src/FundLab.Api/App.fs')
| -rw-r--r-- | src/FundLab.Api/App.fs | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/src/FundLab.Api/App.fs b/src/FundLab.Api/App.fs index 138230f..5ff7fdd 100644 --- a/src/FundLab.Api/App.fs +++ b/src/FundLab.Api/App.fs @@ -1505,6 +1505,29 @@ module App = with _ -> errorResponse 500 "PERSISTENCE_ERROR" "sip plan persistence failed" next ctx + let private setSipPlanStatus + (repository: FundRepository) + (targetStatus: string) + (fundIdText: string) + (planIdText: string) + : HttpHandler = + fun next ctx -> + match Guid.TryParse fundIdText, Guid.TryParse planIdText with + | (false, _), _ + | _, (false, _) -> + errorResponse 400 "INVALID_SIP_REQUEST" "fund id and plan id must be UUIDs" next ctx + | (true, fundId), (true, planId) -> + try + match repository.GetFund fundId with + | None -> errorResponse 404 "FUND_NOT_FOUND" "fund was not found" next ctx + | Some _ -> + match repository.SetSipPlanStatus(fundId, planId, targetStatus) with + | SipPlanStatusResult.SipPlanStatusChanged plan -> json (sipPlanResponse plan) next ctx + | SipPlanStatusResult.SipPlanStatusNotFound -> + errorResponse 404 "PLAN_NOT_FOUND" "sip plan does not belong to this fund" next ctx + with _ -> + errorResponse 500 "PERSISTENCE_ERROR" "sip plan status update failed" next ctx + let private createInvestmentPlan (repository: FundRepository) (fundIdText: string) : HttpHandler = fun next ctx -> task { @@ -1735,6 +1758,24 @@ module App = with _ -> errorResponse 500 "PERSISTENCE_ERROR" "rebalance plan persistence failed" next ctx + let private archiveRebalancePlan (repository: FundRepository) (fundIdText: string) (planIdText: string) : HttpHandler = + fun next ctx -> + match Guid.TryParse fundIdText, Guid.TryParse planIdText with + | (false, _), _ + | _, (false, _) -> + errorResponse 400 "INVALID_REBALANCE_REQUEST" "fund id and plan id must be UUIDs" next ctx + | (true, fundId), (true, planId) -> + try + match repository.GetFund fundId with + | None -> errorResponse 404 "FUND_NOT_FOUND" "fund was not found" next ctx + | Some _ -> + match repository.ArchiveRebalancePlan(fundId, planId) with + | RebalanceArchiveResult.RebalancePlanArchived plan -> json (rebalancePlanResponse plan) next ctx + | RebalanceArchiveResult.RebalanceArchiveNotFound -> + errorResponse 404 "PLAN_NOT_FOUND" "rebalance plan does not belong to this fund" next ctx + with _ -> + errorResponse 500 "PERSISTENCE_ERROR" "rebalance plan archive failed" next ctx + let private executeRebalancePlan (repository: FundRepository) (fundIdText: string) (planIdText: string) : HttpHandler = fun next ctx -> match Guid.TryParse fundIdText, Guid.TryParse planIdText with @@ -1749,7 +1790,10 @@ module App = | Some _ -> match repository.ExecuteRebalancePlan planId with | Error message -> - errorResponse (if message.Contains("was not found") then 404 else 400) "INVALID_REBALANCE_REQUEST" message next ctx + if message.Contains("is archived") then + errorResponse 409 "PLAN_ARCHIVED" "rebalance plan is archived" next ctx + else + errorResponse (if message.Contains("was not found") then 404 else 400) "INVALID_REBALANCE_REQUEST" message next ctx | Ok result -> if result.PlanId <> Guid.Empty && repository.GetRebalancePlans fundId |> List.exists (fun plan -> plan.Id = result.PlanId) |> not then errorResponse 404 "PLAN_NOT_FOUND" "rebalance plan does not belong to this fund" next ctx @@ -1787,7 +1831,11 @@ module App = errorResponse 404 "PLAN_NOT_FOUND" "rebalance plan does not belong to this fund" next ctx else match repository.PreviewRebalancePlan planId with - | Error message -> errorResponse 400 "INVALID_REBALANCE_REQUEST" message next ctx + | Error message -> + if message.Contains("is archived") then + errorResponse 409 "PLAN_ARCHIVED" "rebalance plan is archived" next ctx + else + errorResponse 400 "INVALID_REBALANCE_REQUEST" message next ctx | Ok preview -> json (rebalancePreviewResponse preview) next ctx with _ -> errorResponse 500 "PERSISTENCE_ERROR" "rebalance preview failed" next ctx @@ -2275,8 +2323,11 @@ module App = POST >=> routef "/funds/%s/sip/plans" (createSipPlan repository) GET >=> routef "/funds/%s/sip/plans" (getSipPlans repository) POST >=> routef "/funds/%s/sip/advance" (advanceSipPlans repository) + POST >=> routef "/funds/%s/sip/plans/%s/pause" (fun (fundId, planId) -> setSipPlanStatus repository "paused" fundId planId) + POST >=> routef "/funds/%s/sip/plans/%s/resume" (fun (fundId, planId) -> setSipPlanStatus repository "active" fundId planId) POST >=> routef "/funds/%s/rebalance/plans" (createRebalancePlan repository) GET >=> routef "/funds/%s/rebalance/plans" (getRebalancePlans repository) + POST >=> routef "/funds/%s/rebalance/plans/%s/archive" (fun (fundId, planId) -> archiveRebalancePlan repository fundId planId) POST >=> routef "/funds/%s/rebalance/plans/%s/execute" (fun (fundId, planId) -> executeRebalancePlan repository fundId planId) GET >=> routef "/funds/%s/rebalance/plans/%s/preview" (fun (fundId, planId) -> previewRebalancePlan repository fundId planId) GET >=> routef "/funds/%s/rebalance/executions" (getRebalanceExecutions repository) |
