diff --git a/autotx/db.py b/autotx/db.py index a4932ae..5b69712 100644 --- a/autotx/db.py +++ b/autotx/db.py @@ -171,6 +171,35 @@ def get_all(self) -> list[models.Task]: return tasks + def get_from_user(self, app_user_id: str) -> list[models.Task]: + client = get_db_client("public") + + result = client.table("tasks").select("*").eq("app_id", self.app_id).eq("app_user_id", app_user_id).execute() + + tasks = [] + + for task_data in result.data: + tasks.append( + models.Task( + id=task_data["id"], + app_user_id=task_data["app_user_id"], + prompt=task_data["prompt"], + address=task_data["address"], + chain_id=task_data["chain_id"], + created_at=task_data["created_at"], + updated_at=task_data["updated_at"], + running=task_data["running"], + error=task_data["error"], + messages=json.loads(task_data["messages"]), + logs=[models.TaskLog(**log) for log in json.loads(task_data["logs"])] if task_data["logs"] else None, + intents=[load_intent(intent) for intent in json.loads(task_data["intents"])], + previous_task_id=task_data["previous_task_id"], + feedback=task_data["feedback"] + ) + ) + + return tasks + def get_app_by_api_key(api_key: str) -> models.App | None: client = get_db_client("public") @@ -288,7 +317,32 @@ def get_transactions(app_id: str, app_user_id: str, task_id: str, address: str, [TransactionBase(**tx) for tx in json.loads(result.data[0]["transactions"])], result.data[0]["task_id"] ) - + +def get_submitted_transactions_from_user( + app_id: str, + app_user_id: str, +) -> list[list[TransactionBase]]: + client = get_db_client("public") + result = ( + client.table("submitted_batches") + .select("transactions") + .eq("app_id", app_id) + .eq("app_user_id", app_user_id) + .not_.is_("submitted_on", "null") + .execute() + ) + + if len(result.data) == 0: + return [] + + submitted_batches = [] + for batch in result.data: + submitted_batches.append( + [TransactionBase(**tx) for tx in json.loads(batch["transactions"])] + ) + + return submitted_batches + def submit_transactions(app_id: str, app_user_id: str, submitted_batch_id: str) -> None: client = get_db_client("public") diff --git a/autotx/server.py b/autotx/server.py index 64c751a..3c95cf0 100644 --- a/autotx/server.py +++ b/autotx/server.py @@ -294,6 +294,29 @@ def get_task(task_id: str, authorization: Annotated[str | None, Header()] = None task = get_task_or_404(task_id, tasks) return task +@app_router.get("/api/v1/tasks/user/{user_id}") +def get_user_tasks( + user_id: str, authorization: Annotated[str | None, Header()] = None +) -> dict[str, list[Any]]: + (app, app_user) = authorize_app_and_user(authorization, user_id) + tasks = db.TasksRepository(app_id=app.id).get_from_user(app_user.id) + user_tasks = [ + { + "id": task.id, + "prompt": task.prompt, + "address": task.address, + "chain_id": task.chain_id, + "intents": task.intents, + } + for task in tasks + ] + user_submitted_transactions = [ + batch for batch in db.get_submitted_transactions_from_user(app.id, app_user.id) + ] + + return { "tasks": user_tasks, "submitted_transactions": user_submitted_transactions } + + @app_router.get("/api/v1/tasks/{task_id}/intents", response_model=List[Intent]) def get_intents(task_id: str, authorization: Annotated[str | None, Header()] = None) -> Any: app = authorize(authorization) diff --git a/autotx/tests/api/test_send_transactions.py b/autotx/tests/api/test_send_transactions.py index 19fbbbf..3914a9f 100644 --- a/autotx/tests/api/test_send_transactions.py +++ b/autotx/tests/api/test_send_transactions.py @@ -182,10 +182,10 @@ def test_send_transactions(): assert batches[1].created_at is not None assert batches[1].submitted_on is None - assert batch1["batch_id"] == batches[0].id + assert batch1["batch_id"] in [batch.id for batch in batches] assert len(batch1["transactions"]) == 1 - assert batch2["batch_id"] == batches[1].id + assert batch2["batch_id"] in [batch.id for batch in batches] assert len(batch2["transactions"]) == 1 response = client.post(f"/api/v1/tasks/{task_id}/transactions", params={