From 6628ce3322917ad08e4232c62c64582c47949b57 Mon Sep 17 00:00:00 2001 From: Asher Nzurum Date: Mon, 25 Nov 2024 12:45:54 +0100 Subject: [PATCH] apply change requests --- web_app/api/serializers/user.py | 32 ++++++++++++++++++++++++++++++++ web_app/api/user.py | 2 ++ web_app/db/crud.py | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/web_app/api/serializers/user.py b/web_app/api/serializers/user.py index 2a5d791e..acc3508b 100644 --- a/web_app/api/serializers/user.py +++ b/web_app/api/serializers/user.py @@ -2,6 +2,7 @@ This module defines the serializers for the user data. """ from decimal import Decimal +from datetime import datetime from pydantic import BaseModel, Field @@ -53,6 +54,37 @@ class GetStatsResponse(BaseModel): description="Number of unique users in the database.", ) +class PositionHistoryItem(BaseModel): + """ + Represents a single user position in the trading history. + + ### Attributes: + - **status** (str): The current status of the position (e.g., "OPENED", "CLOSED", "LIQUIDATED"). + - **created_at** (datetime): The timestamp when the position was created. + - **start_price** (float): The price of the asset when the position was opened. + - **amount** (str): The quantity of the asset involved in the position. + - **multiplier** (int): The leverage multiplier applied to the position. + """ + status: str + created_at: datetime + start_price: float + amount: str + multiplier: int + + +class UserHistoryResponse(BaseModel): + """ + Represents the response containing the history of positions for a user. + + ### Attributes: + - **positions** (List[PositionHistoryItem]): A list of positions that include details such as: + - `status`: The status of the position. + - `created_at`: When the position was created. + - `start_price`: The initial price of the asset. + - `amount`: The quantity of the asset involved. + - `multiplier`: The leverage multiplier applied to the position. + """ + positions: list[PositionHistoryItem] class SubscribeToNotificationResponse(BaseModel): """ diff --git a/web_app/api/user.py b/web_app/api/user.py index 5b4f6ac4..523d0b61 100644 --- a/web_app/api/user.py +++ b/web_app/api/user.py @@ -13,6 +13,7 @@ GetUserContractAddressResponse, SubscribeToNotificationResponse, UpdateUserContractResponse, + UserHistoryResponse ) from web_app.contract_tools.mixins.dashboard import DashboardMixin from web_app.db.crud import ( @@ -247,6 +248,7 @@ async def get_stats() -> GetStatsResponse: "/api/get-user-history", tags=["User Operations"], summary="Get user position history", + response_model=UserHistoryResponse, response_description="List of user positions including status,created_at, \ start_price, amount, and multiplier.", ) diff --git a/web_app/db/crud.py b/web_app/db/crud.py index 1eba53ca..60c95b83 100644 --- a/web_app/db/crud.py +++ b/web_app/db/crud.py @@ -257,7 +257,7 @@ def fetch_user_history(self, user_id: int) -> List[dict]: ) .filter(Position.user_id == user_id) .all() - ) + ).scalar() # Transform the query result into a list of dictionaries return [