ipulse-shared-core-ftredge 14.0.1__tar.gz → 16.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ipulse-shared-core-ftredge might be problematic. Click here for more details.

Files changed (41) hide show
  1. {ipulse_shared_core_ftredge-14.0.1/src/ipulse_shared_core_ftredge.egg-info → ipulse_shared_core_ftredge-16.0.1}/PKG-INFO +1 -1
  2. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/setup.py +1 -1
  3. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/cache/shared_cache.py +1 -1
  4. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/services/__init__.py +5 -1
  5. ipulse_shared_core_ftredge-16.0.1/src/ipulse_shared_core_ftredge/services/charging_processors.py +350 -0
  6. ipulse_shared_core_ftredge-14.0.1/src/ipulse_shared_core_ftredge/services/credit_service.py → ipulse_shared_core_ftredge-16.0.1/src/ipulse_shared_core_ftredge/services/charging_service.py +68 -32
  7. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1/src/ipulse_shared_core_ftredge.egg-info}/PKG-INFO +1 -1
  8. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge.egg-info/SOURCES.txt +2 -1
  9. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/tests/test_cache_aware_service.py +5 -4
  10. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/tests/test_shared_cache.py +4 -3
  11. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/LICENCE +0 -0
  12. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/README.md +0 -0
  13. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/pyproject.toml +0 -0
  14. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/setup.cfg +0 -0
  15. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/__init__.py +0 -0
  16. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/cache/__init__.py +0 -0
  17. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/dependencies/__init__.py +0 -0
  18. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/dependencies/auth_firebase_token_validation.py +0 -0
  19. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/dependencies/auth_protected_router.py +0 -0
  20. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/dependencies/authz_for_apis.py +0 -0
  21. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/dependencies/firestore_client.py +0 -0
  22. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/models/__init__.py +0 -0
  23. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/models/base_api_response.py +0 -0
  24. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/models/base_data_model.py +0 -0
  25. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/models/organization_profile.py +0 -0
  26. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/models/subscription.py +0 -0
  27. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/models/user_auth.py +0 -0
  28. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/models/user_profile.py +0 -0
  29. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/models/user_profile_update.py +0 -0
  30. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/models/user_status.py +0 -0
  31. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/services/base_firestore_service.py +0 -0
  32. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/services/base_service_exceptions.py +0 -0
  33. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/services/cache_aware_firestore_service.py +0 -0
  34. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/services/fastapiservicemon.py +0 -0
  35. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/services/servicemon.py +0 -0
  36. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/utils/__init__.py +0 -0
  37. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/utils/custom_json_encoder.py +0 -0
  38. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge/utils/json_encoder.py +0 -0
  39. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge.egg-info/dependency_links.txt +0 -0
  40. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge.egg-info/requires.txt +0 -0
  41. {ipulse_shared_core_ftredge-14.0.1 → ipulse_shared_core_ftredge-16.0.1}/src/ipulse_shared_core_ftredge.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ipulse_shared_core_ftredge
3
- Version: 14.0.1
3
+ Version: 16.0.1
4
4
  Summary: Shared Core models and Logger util for the Pulse platform project. Using AI for financial advisory and investment management.
5
5
  Home-page: https://github.com/TheFutureEdge/ipulse_shared_core
6
6
  Author: Russlan Ramdowar
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
3
3
 
4
4
  setup(
5
5
  name='ipulse_shared_core_ftredge',
6
- version='14.0.1',
6
+ version='16.0.1',
7
7
  package_dir={'': 'src'}, # Specify the source directory
8
8
  packages=find_packages(where='src'), # Look for packages in 'src'
9
9
  install_requires=[
@@ -39,7 +39,7 @@ class SharedCache(Generic[T]):
39
39
  self._timestamps: Dict[str, float] = {}
40
40
 
41
41
  # Thread-safe attributes
42
- self.lock = threading.Lock()
42
+ self.lock = threading.RLock() # Changed from threading.Lock()
43
43
  self.hits = 0
44
44
  self.misses = 0
45
45
  self.sets = 0
@@ -7,8 +7,12 @@ from ipulse_shared_core_ftredge.services.servicemon import Servicemon
7
7
  from ipulse_shared_core_ftredge.services.base_firestore_service import BaseFirestoreService
8
8
  from ipulse_shared_core_ftredge.services.cache_aware_firestore_service import CacheAwareFirestoreService
9
9
 
10
+ from ipulse_shared_core_ftredge.services.charging_processors import (ChargingProcessor)
11
+ from ipulse_shared_core_ftredge.services.charging_service import ChargingService
12
+
10
13
  __all__ = [
11
14
  'AuthorizationError', 'BaseServiceException', 'ServiceError', 'ValidationError',
12
15
  'ResourceNotFoundError', 'BaseFirestoreService',
13
- 'CacheAwareFirestoreService'
16
+ 'CacheAwareFirestoreService', 'Servicemon',
17
+ 'ChargingProcessor'
14
18
  ]
@@ -0,0 +1,350 @@
1
+ """Reusable credit checking and charging utilities for services."""
2
+ import os
3
+ from typing import Dict, Any, List, Optional, Callable, Awaitable, TypeVar
4
+ from ipulse_shared_core_ftredge.services.charging_service import ChargingService, ValidationError
5
+ import logging
6
+
7
+ T = TypeVar('T')
8
+
9
+ class ChargingProcessor:
10
+ """Handles credit checking and charging for both single item and batch access."""
11
+
12
+ def __init__(self, charging_service: ChargingService, logger: logging.Logger):
13
+ self.charging_service = charging_service
14
+ self.logger = logger
15
+
16
+ async def process_single_item_charging(
17
+ self,
18
+ user_uid: str,
19
+ item_id: str,
20
+ get_cost_func: Callable[[], Awaitable[Optional[float]]],
21
+ pre_fetched_credits: Optional[Dict[str, float]] = None,
22
+ operation_description: str = "Resource access"
23
+ ) -> Dict[str, Any]:
24
+ """
25
+ Process credit check and charging for a single item.
26
+
27
+ Args:
28
+ user_uid: User's UID
29
+ item_id: ID of the item being accessed
30
+ get_cost_func: Async function that returns the cost for the item
31
+ pre_fetched_credits: Optional pre-fetched credit information
32
+ operation_description: Description for the charging operation
33
+
34
+ Returns:
35
+ Dict with keys:
36
+ - access_granted: bool
37
+ - charge_successful: bool (only meaningful if access_granted is True)
38
+ - cost: Optional[float]
39
+ - reason: str (explanation if access denied)
40
+ - updated_user_credits: Optional[Dict] (credits after charging, if applicable)
41
+ """
42
+ self.logger.info(f"Processing single item credit check for user {user_uid}, item {item_id}")
43
+ updated_user_credits = None # Initialize
44
+
45
+ try:
46
+ # Get the credit cost for this item
47
+ credit_cost = await get_cost_func()
48
+
49
+ # If item is free or cost not configured, allow access
50
+ if credit_cost is None or credit_cost <= 0:
51
+ if credit_cost is None:
52
+ self.logger.info(f"Item {item_id} has no configured credit cost, treating as free.")
53
+
54
+ # For free items, provide current credits if available
55
+ if pre_fetched_credits:
56
+ updated_user_credits = pre_fetched_credits
57
+ elif self.charging_service: # Attempt to get current credits if not pre-fetched
58
+ try:
59
+ _, current_user_credits_from_verify = await self.charging_service.verify_credits(user_uid, 0, None)
60
+ updated_user_credits = current_user_credits_from_verify
61
+ except Exception: # pylint: disable=broad-except
62
+ self.logger.warning(f"Could not fetch current credits for user {user_uid} for free item.")
63
+
64
+ return {
65
+ 'access_granted': True,
66
+ 'charge_successful': True, # No charge needed
67
+ 'cost': credit_cost if credit_cost is not None else 0.0,
68
+ 'reason': 'free_item',
69
+ 'updated_user_credits': updated_user_credits
70
+ }
71
+
72
+ # Check for debug mode bypass
73
+ if os.getenv("BYPASS_CREDIT_CHECK", "").lower() == "true":
74
+ self.logger.info(f"Bypassing credit check for item {item_id} due to debug mode")
75
+ # Similar to free items, provide current credits if available
76
+ if pre_fetched_credits:
77
+ updated_user_credits = pre_fetched_credits
78
+ elif self.charging_service:
79
+ try:
80
+ _, current_user_credits_from_verify = await self.charging_service.verify_credits(user_uid, 0, None)
81
+ updated_user_credits = current_user_credits_from_verify
82
+ except Exception: # pylint: disable=broad-except
83
+ self.logger.warning(f"Could not fetch current credits for user {user_uid} during debug bypass.")
84
+ return {
85
+ 'access_granted': True,
86
+ 'charge_successful': True,
87
+ 'cost': credit_cost,
88
+ 'reason': 'debug_bypass',
89
+ 'updated_user_credits': updated_user_credits
90
+ }
91
+
92
+ # Verify credit service is available
93
+ if not self.charging_service:
94
+ self.logger.error("ChargingService not initialized.")
95
+ return {
96
+ 'access_granted': False,
97
+ 'charge_successful': False,
98
+ 'cost': credit_cost,
99
+ 'reason': 'service_unavailable',
100
+ 'updated_user_credits': None
101
+ }
102
+
103
+ # Verify user has enough credits
104
+ has_credits, current_user_credits_from_verify = await self.charging_service.verify_credits(
105
+ user_uid,
106
+ credit_cost,
107
+ pre_fetched_user_credits=pre_fetched_credits
108
+ )
109
+ # Store current credits from verification, might be used if charge fails
110
+ updated_user_credits = current_user_credits_from_verify
111
+
112
+
113
+ if not has_credits:
114
+ self.logger.warning(f"User {user_uid} has insufficient credits for item {item_id} (cost: {credit_cost})")
115
+ return {
116
+ 'access_granted': False,
117
+ 'charge_successful': False,
118
+ 'cost': credit_cost,
119
+ 'reason': 'insufficient_credits',
120
+ 'updated_user_credits': updated_user_credits # Return credits state at time of failure
121
+ }
122
+
123
+ # Charge the user - this now returns (bool, Optional[Dict])
124
+ charged, calculated_updated_credits = await self.charging_service.charge_credits_transaction(
125
+ user_uid,
126
+ credit_cost,
127
+ operation_description
128
+ )
129
+
130
+ # Use the credits returned by charge_credits if successful
131
+ if calculated_updated_credits is not None:
132
+ updated_user_credits = calculated_updated_credits
133
+
134
+
135
+ return {
136
+ 'access_granted': True, # Access granted because verify_credits passed
137
+ 'charge_successful': charged,
138
+ 'cost': credit_cost,
139
+ 'reason': 'charged' if charged else 'charge_failed',
140
+ 'updated_user_credits': updated_user_credits
141
+ }
142
+
143
+ except ValidationError as ve:
144
+ self.logger.error(f"Validation error for item {item_id}, user {user_uid}: {str(ve)}")
145
+ # Try to get current credits to return
146
+ if self.charging_service:
147
+ try:
148
+ _, updated_user_credits = await self.charging_service.verify_credits(user_uid, 0, pre_fetched_credits)
149
+ except Exception: # pylint: disable=broad-except
150
+ pass # Keep updated_user_credits as None
151
+ ve.additional_info = ve.additional_info or {}
152
+ ve.additional_info['updated_user_credits'] = updated_user_credits
153
+ raise
154
+ except Exception as e:
155
+ self.logger.error(f"Unexpected error during credit processing for item {item_id}, user {user_uid}: {str(e)}", exc_info=True)
156
+ # Try to get current credits to return
157
+ current_user_credits_on_error = None
158
+ if self.charging_service:
159
+ try:
160
+ _, current_user_credits_on_error = await self.charging_service.verify_credits(user_uid, 0, pre_fetched_credits)
161
+ except Exception: # pylint: disable=broad-except
162
+ pass
163
+ return {
164
+ 'access_granted': False,
165
+ 'charge_successful': False,
166
+ 'cost': None, # Cost might not be determined if error was early
167
+ 'reason': f'error: {str(e)}',
168
+ 'updated_user_credits': current_user_credits_on_error
169
+ }
170
+ async def process_batch_items_charging(
171
+ self,
172
+ user_uid: str,
173
+ items: List[Dict[str, Any]],
174
+ pre_fetched_credits: Optional[Dict[str, float]] = None,
175
+ operation_description: str = "Batch resource access"
176
+ ) -> Dict[str, Any]:
177
+ """
178
+ Process credit check and charging for a batch of items.
179
+
180
+ Args:
181
+ user_uid: User's UID
182
+ items: List of dicts with keys: 'id', 'data', 'get_cost_func'
183
+ pre_fetched_credits: Optional pre-fetched credit information
184
+ operation_description: Description for the charging operation
185
+
186
+ Returns:
187
+ Dict with keys:
188
+ - accessible_items: List[Dict] (items user can access)
189
+ - charge_successful: bool
190
+ - total_cost: float
191
+ - paid_items_count: int
192
+ - free_items_count: int
193
+ - updated_user_credits: Optional[Dict] (credits after charging, if applicable)
194
+ """
195
+ self.logger.info(f"Processing batch credit check for user {user_uid}, {len(items)} items")
196
+ updated_user_credits = None # Initialize
197
+
198
+ if not items:
199
+ return {
200
+ 'accessible_items': [],
201
+ 'charge_successful': True,
202
+ 'total_cost': 0.0,
203
+ 'paid_items_count': 0,
204
+ 'free_items_count': 0,
205
+ 'updated_user_credits': None
206
+ }
207
+
208
+ # Separate free and paid items
209
+ free_items = []
210
+ paid_items = []
211
+ total_cost = 0.0
212
+
213
+ for item in items:
214
+ try:
215
+ cost = await item['get_cost_func']()
216
+ if cost is None or cost <= 0:
217
+ free_items.append(item)
218
+ else:
219
+ paid_items.append(item)
220
+ total_cost += cost
221
+ except Exception as cost_err:
222
+ self.logger.error(f"Error getting cost for item {item.get('id', 'unknown')}: {cost_err}")
223
+ free_items.append(item)
224
+
225
+ self.logger.info(f"User {user_uid}: {len(free_items)} free items, {len(paid_items)} paid items (total cost: {total_cost})")
226
+
227
+ # If no paid items, return all free items
228
+ if not paid_items:
229
+ if pre_fetched_credits:
230
+ updated_user_credits = pre_fetched_credits
231
+ elif self.charging_service:
232
+ try:
233
+ _, current_user_credits_from_verify = await self.charging_service.verify_credits(user_uid, 0, None)
234
+ updated_user_credits = current_user_credits_from_verify
235
+
236
+ except Exception: # pylint: disable=broad-except
237
+ self.logger.warning(f"Could not fetch current credits for user {user_uid} for free batch.")
238
+
239
+ return {
240
+ 'accessible_items': free_items,
241
+ 'charge_successful': True,
242
+ 'total_cost': 0.0,
243
+ 'paid_items_count': 0,
244
+ 'free_items_count': len(free_items),
245
+ 'updated_user_credits': updated_user_credits
246
+ }
247
+
248
+ # Check for debug mode bypass
249
+ if os.getenv("BYPASS_CREDIT_CHECK", "").lower() == "true":
250
+ self.logger.info(f"Bypassing credit check for {len(paid_items)} paid items due to debug mode")
251
+ if pre_fetched_credits:
252
+ updated_user_credits = pre_fetched_credits
253
+ elif self.charging_service:
254
+ try:
255
+ _, current_user_credits_from_verify = await self.charging_service.verify_credits(user_uid, 0, None)
256
+ updated_user_credits = current_user_credits_from_verify
257
+ except Exception: # pylint: disable=broad-except
258
+ self.logger.warning(f"Could not fetch current credits for user {user_uid} during debug bypass for batch.")
259
+
260
+ return {
261
+ 'accessible_items': free_items + paid_items,
262
+ 'charge_successful': True,
263
+ 'total_cost': total_cost,
264
+ 'paid_items_count': len(paid_items),
265
+ 'free_items_count': len(free_items),
266
+ 'updated_user_credits': updated_user_credits
267
+ }
268
+
269
+ # Verify credit service is available
270
+ if not self.charging_service:
271
+ self.logger.error("ChargingService not initialized for batch processing.")
272
+ return {
273
+ 'accessible_items': free_items,
274
+ 'charge_successful': False,
275
+ 'total_cost': total_cost,
276
+ 'paid_items_count': len(paid_items),
277
+ 'free_items_count': len(free_items),
278
+ 'updated_user_credits': None
279
+ }
280
+
281
+ try:
282
+ # Verify user has enough credits for total cost
283
+ has_credits, current_user_credits_from_verify = await self.charging_service.verify_credits(
284
+ user_uid,
285
+ total_cost,
286
+ pre_fetched_user_credits=pre_fetched_credits
287
+ )
288
+ updated_user_credits = current_user_credits_from_verify
289
+
290
+
291
+ if not has_credits:
292
+ self.logger.warning(f"User {user_uid} has insufficient credits for batch (cost: {total_cost}). Returning free items only.")
293
+ return {
294
+ 'accessible_items': free_items,
295
+ 'charge_successful': False,
296
+ 'total_cost': total_cost,
297
+ 'paid_items_count': len(paid_items),
298
+ 'free_items_count': len(free_items),
299
+ 'updated_user_credits': updated_user_credits # Return credits state at time of failure
300
+ }
301
+
302
+ # Charge the user for all paid items
303
+ charged, calculated_updated_credits = await self.charging_service.charge_credits_transaction(
304
+ user_uid,
305
+ total_cost,
306
+ f"{operation_description} ({len(paid_items)} items, total cost: {total_cost})"
307
+ )
308
+
309
+ if calculated_updated_credits is not None:
310
+ updated_user_credits = calculated_updated_credits
311
+
312
+
313
+ # Return all items (free + paid) since credits were verified (even if charge failed post-verification)
314
+ return {
315
+ 'accessible_items': free_items + paid_items,
316
+ 'charge_successful': charged,
317
+ 'total_cost': total_cost,
318
+ 'paid_items_count': len(paid_items),
319
+ 'free_items_count': len(free_items),
320
+ 'updated_user_credits': updated_user_credits
321
+ }
322
+
323
+ except ValidationError as ve:
324
+ self.logger.error(f"Validation error during batch credit check for user {user_uid}: {str(ve)}")
325
+ if self.charging_service:
326
+ try:
327
+ _, current_user_credits_from_verify = await self.charging_service.verify_credits(user_uid, 0, pre_fetched_credits)
328
+ updated_user_credits = current_user_credits_from_verify
329
+ except Exception: # pylint: disable=broad-except
330
+ pass
331
+ ve.additional_info = ve.additional_info or {}
332
+ ve.additional_info['updated_user_credits'] = updated_user_credits
333
+ raise
334
+ except Exception as e:
335
+ self.logger.error(f"Unexpected error during batch credit check for user {user_uid}: {str(e)}", exc_info=True)
336
+ current_credits_on_error = None
337
+ if self.charging_service:
338
+ try:
339
+ _, current_credits_on_error = await self.charging_service.verify_credits(user_uid, 0, pre_fetched_credits)
340
+ updated_user_credits = current_credits_on_error
341
+ except Exception: # pylint: disable=broad-except
342
+ pass
343
+ return {
344
+ 'accessible_items': free_items, # Only free items if error
345
+ 'charge_successful': False,
346
+ 'total_cost': total_cost, # This is the cost of paid items that were attempted
347
+ 'paid_items_count': len(paid_items),
348
+ 'free_items_count': len(free_items),
349
+ 'updated_user_credits': updated_user_credits
350
+ }
@@ -9,9 +9,9 @@ from ipulse_shared_core_ftredge.models.user_status import UserStatus
9
9
  # Default Firestore timeout if not provided by the consuming application
10
10
  DEFAULT_FIRESTORE_TIMEOUT = 15.0
11
11
 
12
- class CreditService:
12
+ class ChargingService:
13
13
  """
14
- Service class for credit operations.
14
+ Service class for charging operations.
15
15
  Designed to be project-agnostic and directly uses UserStatus model constants.
16
16
  """
17
17
 
@@ -22,7 +22,7 @@ class CreditService:
22
22
  firestore_timeout: float = DEFAULT_FIRESTORE_TIMEOUT
23
23
  ):
24
24
  """
25
- Initialize the credit service.
25
+ Initialize the charging service.
26
26
 
27
27
  Args:
28
28
  db: Firestore client.
@@ -37,7 +37,7 @@ class CreditService:
37
37
  self.timeout = firestore_timeout
38
38
 
39
39
  self.logger.info(
40
- f"CreditService initialized using UserStatus constants. Collection: {self.users_status_collection_name}, "
40
+ f"ChargingService initialized using UserStatus constants. Collection: {self.users_status_collection_name}, "
41
41
  f"Doc Prefix: {self.user_status_doc_prefix}, Timeout: {self.timeout}s"
42
42
  )
43
43
 
@@ -93,13 +93,13 @@ class CreditService:
93
93
  f"(subscription: {subscription_credits}, extra: {extra_credits})"
94
94
  )
95
95
 
96
- userstatus_data_to_return = {
96
+ user_credits = {
97
97
  "sbscrptn_based_insight_credits": subscription_credits,
98
98
  "extra_insight_credits": extra_credits
99
99
  }
100
100
 
101
101
  has_enough_credits = total_credits >= required_credits_for_resource
102
- return has_enough_credits, userstatus_data_to_return
102
+ return has_enough_credits, user_credits
103
103
 
104
104
  try:
105
105
  self.logger.info(
@@ -118,12 +118,12 @@ class CreditService:
118
118
 
119
119
  has_enough_credits = total_credits >= required_credits_for_resource
120
120
 
121
- userstatus_data_to_return = {
121
+ user_credits = {
122
122
  "sbscrptn_based_insight_credits": subscription_credits,
123
123
  "extra_insight_credits": extra_credits
124
124
  }
125
125
 
126
- return has_enough_credits, userstatus_data_to_return
126
+ return has_enough_credits, user_credits
127
127
 
128
128
  except ResourceNotFoundError:
129
129
  self.logger.warning(f"User status not found for {user_uid} in {self.users_status_collection_name}. Assuming no credits.")
@@ -138,7 +138,7 @@ class CreditService:
138
138
  additional_info={"credits_to_charge": required_credits_for_resource}
139
139
  ) from e
140
140
 
141
- async def charge_credits(self, user_uid: str, credits_to_charge: Optional[float], operation_details: str) -> bool:
141
+ async def charge_credits_transaction(self, user_uid: str, credits_to_charge: Optional[float], operation_details: str) -> Tuple[bool, Optional[Dict[str, float]]]:
142
142
  """
143
143
  Charge a user's credits for an operation.
144
144
 
@@ -148,7 +148,9 @@ class CreditService:
148
148
  operation_details: Details about the operation (for logging).
149
149
 
150
150
  Returns:
151
- Whether the charging was successful.
151
+ Tuple of (success_status: bool, updated_credits: Optional[Dict[str, float]])
152
+ where updated_credits contains 'sbscrptn_based_insight_credits' and 'extra_insight_credits'
153
+ if charging was successful.
152
154
 
153
155
  Raises:
154
156
  ValidationError: If credits_to_charge is None (pricing not properly configured).
@@ -164,7 +166,12 @@ class CreditService:
164
166
 
165
167
  if credits_to_charge == 0:
166
168
  self.logger.info(f"No credits to charge for user {user_uid}, operation: {operation_details}")
167
- return True
169
+ # If no charge, current credits are unchanged. Fetch them if needed by caller.
170
+ # For simplicity here, returning None for updated_credits as no transaction occurred.
171
+ # The caller (CreditProcessor) will handle fetching if it needs the current state.
172
+ # Alternatively, we could fetch here, but it's cleaner if CreditProcessor handles it.
173
+ return True, None
174
+
168
175
 
169
176
  try:
170
177
  userstatus_id = f"{self.user_status_doc_prefix}{user_uid}"
@@ -173,60 +180,89 @@ class CreditService:
173
180
  transaction = self.db.transaction()
174
181
 
175
182
  @firestore.transactional
176
- def update_credits_transaction(transaction_obj, current_user_ref):
183
+ def update_credits_transaction(transaction_obj, current_user_ref) -> Tuple[bool, Optional[Dict[str, float]]]:
177
184
  user_doc = current_user_ref.get(transaction=transaction_obj)
178
185
  if not user_doc.exists:
179
186
  self.logger.warning(
180
187
  f"Cannot charge credits - user status not found for {user_uid} in {self.users_status_collection_name}"
181
188
  )
182
- return False
189
+ return False, None
183
190
 
184
191
  userstatus = user_doc.to_dict()
185
192
 
186
- subscription_credits = userstatus.get("sbscrptn_based_insight_credits", 0)
187
- extra_credits = userstatus.get("extra_insight_credits", 0)
188
- total_credits = subscription_credits + extra_credits
193
+ current_subscription_credits = userstatus.get("sbscrptn_based_insight_credits", 0.0)
194
+ current_extra_credits = userstatus.get("extra_insight_credits", 0.0)
195
+ total_available_credits = current_subscription_credits + current_extra_credits
189
196
 
190
- if total_credits < credits_to_charge:
197
+ if total_available_credits < credits_to_charge:
191
198
  self.logger.warning(
192
199
  f"Insufficient credits for user {user_uid} during transaction: "
193
- f"has {total_credits}, needs {credits_to_charge}"
200
+ f"has {total_available_credits}, needs {credits_to_charge}"
201
+ )
202
+ # Return current credits if charge fails due to insufficient funds
203
+ return False, {
204
+ "sbscrptn_based_insight_credits": current_subscription_credits,
205
+ "extra_insight_credits": current_extra_credits
206
+ }
207
+
208
+ # Calculate how much to deduct from each type of credit
209
+ subscription_credits_deducted = min(current_subscription_credits, credits_to_charge)
210
+ remaining_charge = credits_to_charge - subscription_credits_deducted
211
+ extra_credits_deducted = min(current_extra_credits, remaining_charge)
212
+
213
+ # This check should ideally not be needed if total_available_credits was sufficient,
214
+ # but as a safeguard:
215
+ if (subscription_credits_deducted + extra_credits_deducted) < credits_to_charge:
216
+ self.logger.error(
217
+ f"Credit calculation error for user {user_uid}. "
218
+ f"Required: {credits_to_charge}, Calculated deduction: {subscription_credits_deducted + extra_credits_deducted}"
194
219
  )
195
- return False
220
+ # This case implies a logic flaw or race condition if not for the initial check.
221
+ # Return current credits as charge effectively failed.
222
+ return False, {
223
+ "sbscrptn_based_insight_credits": current_subscription_credits,
224
+ "extra_insight_credits": current_extra_credits
225
+ }
226
+
196
227
 
197
- subscription_credits_to_charge = min(subscription_credits, credits_to_charge)
198
- extra_credits_to_charge = credits_to_charge - subscription_credits_to_charge
228
+ new_subscription_credits = current_subscription_credits - subscription_credits_deducted
229
+ new_extra_credits = current_extra_credits - extra_credits_deducted
199
230
 
200
- update_data = {
231
+ update_data: Dict[str, Any] = {
201
232
  "updated_at": datetime.now(timezone.utc).isoformat(),
202
- "updated_by": "credit_service" # Consider making this configurable or more generic
233
+ "updated_by": "charging_service__charge_credits_transaction" # Static identifier for this operation
203
234
  }
204
235
 
205
- if subscription_credits_to_charge > 0:
206
- update_data["sbscrptn_based_insight_credits"] = firestore.Increment(-subscription_credits_to_charge)
236
+ if subscription_credits_deducted > 0:
237
+ update_data["sbscrptn_based_insight_credits"] = firestore.Increment(-subscription_credits_deducted)
207
238
  update_data["sbscrptn_based_insight_credits_updtd_on"] = datetime.now(timezone.utc).isoformat()
208
239
 
209
- if extra_credits_to_charge > 0:
210
- update_data["extra_insight_credits"] = firestore.Increment(-extra_credits_to_charge)
240
+ if extra_credits_deducted > 0:
241
+ update_data["extra_insight_credits"] = firestore.Increment(-extra_credits_deducted)
211
242
  update_data["extra_insight_credits_updtd_on"] = datetime.now(timezone.utc).isoformat()
212
243
 
213
244
  transaction_obj.update(current_user_ref, update_data)
214
- return True
215
245
 
216
- success = update_credits_transaction(transaction, user_ref)
246
+ # Return success and the calculated new credit balances
247
+ return True, {
248
+ "sbscrptn_based_insight_credits": new_subscription_credits,
249
+ "extra_insight_credits": new_extra_credits
250
+ }
251
+
252
+ success, updated_credits_dict = update_credits_transaction(transaction, user_ref)
217
253
 
218
254
  if success:
219
255
  self.logger.info(
220
256
  f"Successfully charged {credits_to_charge} credits for user {user_uid}. "
221
- f"Operation: {operation_details}"
257
+ f"Operation: {operation_details}. New balances: {updated_credits_dict}"
222
258
  )
223
259
  else:
224
260
  self.logger.warning(
225
261
  f"Failed to charge {credits_to_charge} credits for user {user_uid} (transaction outcome). "
226
- f"Operation: {operation_details}"
262
+ f"Operation: {operation_details}. Current balances: {updated_credits_dict}"
227
263
  )
228
264
 
229
- return success
265
+ return success, updated_credits_dict
230
266
 
231
267
  except Exception as e:
232
268
  self.logger.error(f"Error charging credits for user {user_uid}: {str(e)}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ipulse_shared_core_ftredge
3
- Version: 14.0.1
3
+ Version: 16.0.1
4
4
  Summary: Shared Core models and Logger util for the Pulse platform project. Using AI for financial advisory and investment management.
5
5
  Home-page: https://github.com/TheFutureEdge/ipulse_shared_core
6
6
  Author: Russlan Ramdowar
@@ -28,7 +28,8 @@ src/ipulse_shared_core_ftredge/services/__init__.py
28
28
  src/ipulse_shared_core_ftredge/services/base_firestore_service.py
29
29
  src/ipulse_shared_core_ftredge/services/base_service_exceptions.py
30
30
  src/ipulse_shared_core_ftredge/services/cache_aware_firestore_service.py
31
- src/ipulse_shared_core_ftredge/services/credit_service.py
31
+ src/ipulse_shared_core_ftredge/services/charging_processors.py
32
+ src/ipulse_shared_core_ftredge/services/charging_service.py
32
33
  src/ipulse_shared_core_ftredge/services/fastapiservicemon.py
33
34
  src/ipulse_shared_core_ftredge/services/servicemon.py
34
35
  src/ipulse_shared_core_ftredge/utils/__init__.py
@@ -208,13 +208,14 @@ class TestCacheAwareFirestoreService(unittest.TestCase):
208
208
  """Test _invalidate_collection_cache method."""
209
209
  # Prepare cached data
210
210
  test_docs = [{"id": "doc1", "name": "Doc 1"}, {"id": "doc2", "name": "Doc 2"}]
211
- self.collection_cache.set("all_documents", test_docs)
211
+ cache_key_to_invalidate = "all_documents"
212
+ self.collection_cache.set(cache_key_to_invalidate, test_docs)
212
213
 
213
214
  # Execute invalidation
214
- self.service._invalidate_collection_cache()
215
+ self.service._invalidate_collection_cache(cache_key_to_invalidate)
215
216
 
216
- # Verify cache was invalidated
217
- self.assertIsNone(self.collection_cache.get("all_documents"))
217
+ # Verify cache is empty for that key
218
+ self.assertIsNone(self.collection_cache.get(cache_key_to_invalidate))
218
219
 
219
220
  def test_invalidate_collection_cache_custom_key(self):
220
221
  """Test _invalidate_collection_cache method with custom key."""
@@ -42,7 +42,7 @@ class TestSharedCache(unittest.TestCase):
42
42
  self.assertEqual(self.cache.get("expiring_key"), "expiring_value")
43
43
 
44
44
  # Wait for TTL to expire
45
- time.sleep(0.6) # Slightly longer than TTL
45
+ time.sleep(0.6) # Slightly longer than TTL (0.5s)
46
46
 
47
47
  # Verify value is no longer cached
48
48
  self.assertIsNone(self.cache.get("expiring_key"))
@@ -138,8 +138,9 @@ class TestSharedCache(unittest.TestCase):
138
138
  self.assertEqual(stats["enabled"], True)
139
139
  self.assertEqual(stats["ttl_seconds"], 0.5)
140
140
  self.assertEqual(stats["item_count"], 2)
141
- self.assertIn("stats_key1", stats["keys"])
142
- self.assertIn("stats_key2", stats["keys"])
141
+ self.assertIn("stats_key1", stats["first_20_keys"])
142
+ self.assertIn("stats_key2", stats["first_20_keys"])
143
+ self.assertEqual(stats["total_keys"], 2)
143
144
 
144
145
 
145
146
  if __name__ == "__main__":