diff --git a/apigateway/get_missions/main.py b/apigateway/get_missions/main.py index 5bad497..a7e73f8 100644 --- a/apigateway/get_missions/main.py +++ b/apigateway/get_missions/main.py @@ -2,11 +2,13 @@ from flask import abort from infra import auth, authenticated_user_id # type: ignore +from infra.alpaca_action import get_account_balance from infra.data.missions import Mission, Missions from infra.logger import log_error +from infra.stytch_actions import get_alpaca_account_id -def process_mission(mission_details: Mission) -> dict: +def process_mission(account_balance: int, mission_details: Mission) -> dict: """create mission payload""" payload: dict = { @@ -15,6 +17,7 @@ def process_mission(mission_details: Mission) -> dict: "forecast": mission_details.forecaster(), "name": mission_details.data["name"], "strategy": mission_details.data["strategy"], + "milestones": mission_details.calculate_milestones(account_balance), } return payload @@ -31,10 +34,20 @@ def get_missions(request): "get_missions()", "could not load authenticated user_id, this shouldn't have happened", ) - return ("Something went wrong", 403) + abort(403) + + if not (alpaca_account_id := get_alpaca_account_id(user_id)): + log_error("get_missions()", "alpaca account not ready") + abort(400) + + # TODO: account balance is not per mission! + if not (account_balance := get_account_balance(alpaca_account_id)): + abort(400) missions = Missions(user_id=user_id) - payload = [process_mission(mission) for mission in missions] + payload = [ + process_mission(account_balance, mission) for mission in missions + ] if not payload: log_error( "get_missions()", f"{user_id} does not have any active missions" diff --git a/apigateway/get_missions/requirements.txt b/apigateway/get_missions/requirements.txt index 04acc64..cc4dc16 100644 --- a/apigateway/get_missions/requirements.txt +++ b/apigateway/get_missions/requirements.txt @@ -1623,5 +1623,5 @@ zipp==3.15.0 \ --extra-index-url https://us-east4-python.pkg.dev/development-380917/python-repos/simple/ -infra==0.1.106 \ +infra==0.1.107 \ --hash=sha256:0e81d2b54b3d5449789ab70a3c493c9bbd147a2365999c909e32e14489fe1fd2 diff --git a/infra/infra/alpaca_action.py b/infra/infra/alpaca_action.py index cb28241..a494002 100644 --- a/infra/infra/alpaca_action.py +++ b/infra/infra/alpaca_action.py @@ -127,6 +127,34 @@ def validate_before_transfer(account_id: str) -> bool: return True +def get_account_balance(alpaca_account_id: str) -> int | None: + r = alpaca_proxy( + method="GET", + url=f"v1/accounts/{alpaca_account_id}", + args=None, + headers=None, + payload=None, + ) + + if r.status_code != 200: + log_error( + "get_account_balance()", + f"failed to get account details {alpaca_account_id}: {r.text}", + ) + return None + + usd = r.json().get("usd") + + portfolio_value = int(usd.get("portfolio_value", 0)) + cash = int(usd.get("cash", 0)) + + print( + f"account balances for {alpaca_account_id}: {portfolio_value}, {cash}" + ) + + return portfolio_value + cash + + def transfer_amount( alpaca_account_id: str, relationship_id: str, amount: int ) -> dict | None: diff --git a/infra/infra/data/missions.py b/infra/infra/data/missions.py index 893bff2..332dc19 100644 --- a/infra/infra/data/missions.py +++ b/infra/infra/data/missions.py @@ -5,6 +5,17 @@ class Mission: + level_step: dict = { + 200: 25, + 1000: 50, + 2500: 100, + 5000: 250, + 15000: 500, + 20000: 750, + 40000: 1000, + 150000: 2000, + } + def __init__(self, data: dict): self.data = data @@ -47,6 +58,15 @@ def forecaster(self, bins: int = 30) -> list: return new_df.values.tolist() + def calculate_milestones(self, account_size: int) -> list[int]: + level = min( + self.level_step.keys(), key=lambda x: abs(x - account_size) + ) + + print(level) + + return [] + class Missions: def __init__(self, user_id: str | None = None) -> None: diff --git a/infra/pyproject.toml b/infra/pyproject.toml index e1974f8..a93d350 100644 --- a/infra/pyproject.toml +++ b/infra/pyproject.toml @@ -14,7 +14,7 @@ license = {text = "MIT"} name = "infra" readme = "README.md" requires-python = ">=3.11" -version = "0.1.106" +version = "0.1.107" [build-system] build-backend = "pdm.backend"