@@ -77,45 +77,76 @@ def get_usage_setting(self, db_session: Session) -> List[QueryUsage]:
7777 def get_user_max_query_allowance (self , db_session : Session , user_id : UUID ) -> int :
7878 """
7979 Retrieves the maximum number of queries a user is allowed to make.
80+ This is calculated as the sum of:
81+ 1. User's whitelist allowance
82+ 2. Allowance based on staked Morpheus tokens
8083
8184 Args:
8285 db_session (Session): The database session.
8386 user_id (UUID): The ID of the user.
8487
8588 Returns:
8689 int: The maximum number of queries the user is allowed to make.
87-
88- Raises:
89- Exception: If the user has not staked any Morpheus tokens.
9090 """
9191 user = self .user_service .get_user (db_session , user_id )
9292
93- whitelist = self .user_service .get_whitelist (db_session )
93+ # Get the user's base whitelist allowance using the dedicated method
94+ whitelist_allowance = self .get_user_whitelist_allowance (
95+ db_session , user_id )
96+
97+ # Calculate additional allowance based on staked tokens
98+ token_based_allowance = self .get_user_token_based_allowance (
99+ db_session , user_id )
100+
101+ # Total allowance is the sum of whitelist allowance and token-based allowance
102+ max_allowed_query = whitelist_allowance + token_based_allowance
103+
104+ return max_allowed_query
105+
106+ def get_user_token_based_allowance (self , db_session : Session , user_id : UUID ) -> int :
107+ """
108+ Calculates a user's token-based query allowance based on their staked MOR tokens.
94109
95- # check user payment
96- for wl in whitelist :
97- if str (user_id ) == str (wl .user_id ):
98- max_allowed_query = wl .max_query
99- return max_allowed_query
110+ Args:
111+ db_session (Session): The database session.
112+ user_id (UUID): The ID of the user.
100113
114+ Returns:
115+ int: The token-based query allowance (0 if no tokens are staked)
116+ """
117+ user = self .user_service .get_user (db_session , user_id )
118+
119+ token_based_allowance = 0
101120 staked_morpheus = get_user_staked_tokens (
102121 wallet_address = user .wallet_address .lower (), provider = "morpheus" )
103122
104- if staked_morpheus == 0 :
105- raise RateLimitError (
106- "You need to stake Morpheus tokens to use this service" )
123+ if staked_morpheus > 0 :
124+ usage_setting = self .get_usage_setting (db_session )
125+ for setting in usage_setting :
126+ if setting .provider == "morpheus" :
127+ token_based_allowance = setting .max_query * \
128+ int (int (staked_morpheus ) / 1e18 )
129+ break
107130
108- usage_setting = self .get_usage_setting (
109- db_session )
131+ return token_based_allowance
110132
111- max_allowed_query = 0
112- for setting in usage_setting :
113- if setting .provider == "morpheus" :
114- max_allowed_query = setting .max_query * \
115- int (int (staked_morpheus ) / 1e18 )
116- break
133+ def get_user_whitelist_allowance (self , db_session : Session , user_id : UUID ) -> int :
134+ """
135+ Retrieves the base query allowance for a user from the whitelist using a direct database query.
117136
118- return max_allowed_query
137+ Args:
138+ db_session (Session): The database session.
139+ user_id (UUID): The ID of the user.
140+
141+ Returns:
142+ int: The base query allowance from the whitelist (default: 0 if not whitelisted)
143+ """
144+ # Use the repository to directly query the database for whitelist allowance
145+ allowance = self .repository .get_user_whitelist_allowance (
146+ db_session , user_id )
147+
148+ # Return the allowance if found, otherwise return 0
149+ return allowance if allowance is not None else 0
119150
120151 def get_user_query_count_for_today (self , db_session : Session , user_id : UUID , provider : Optional [str ] = None ) -> Tuple [int , Optional [datetime ]]:
121152 """
0 commit comments