fixes
This commit is contained in:
@@ -31,15 +31,35 @@ class CreditUsageLogSerializer(serializers.ModelSerializer):
|
||||
source='get_operation_type_display',
|
||||
read_only=True
|
||||
)
|
||||
client_cost = serializers.SerializerMethodField(help_text='Client-facing cost (credits * price_per_credit)')
|
||||
site_name = serializers.SerializerMethodField(help_text='Name of the associated site')
|
||||
|
||||
class Meta:
|
||||
model = CreditUsageLog
|
||||
fields = [
|
||||
'id', 'operation_type', 'operation_type_display', 'credits_used',
|
||||
'cost_usd', 'model_used', 'tokens_input', 'tokens_output',
|
||||
'related_object_type', 'related_object_id', 'metadata', 'created_at'
|
||||
'cost_usd', 'client_cost', 'model_used', 'tokens_input', 'tokens_output',
|
||||
'related_object_type', 'related_object_id', 'site_name', 'metadata', 'created_at'
|
||||
]
|
||||
read_only_fields = ['created_at', 'account']
|
||||
|
||||
def get_client_cost(self, obj) -> str:
|
||||
"""Calculate client-facing cost from credits * default_credit_price_usd"""
|
||||
from igny8_core.business.billing.models import BillingConfiguration
|
||||
try:
|
||||
config = BillingConfiguration.get_config()
|
||||
price_per_credit = config.default_credit_price_usd
|
||||
client_cost = Decimal(obj.credits_used) * price_per_credit
|
||||
return str(client_cost.quantize(Decimal('0.0001')))
|
||||
except Exception:
|
||||
# Fallback to cost_usd if billing config unavailable
|
||||
return str(obj.cost_usd) if obj.cost_usd else '0.0000'
|
||||
|
||||
def get_site_name(self, obj) -> Optional[str]:
|
||||
"""Get the site name if available"""
|
||||
if obj.site:
|
||||
return obj.site.name
|
||||
return None
|
||||
|
||||
|
||||
class CreditBalanceSerializer(serializers.Serializer):
|
||||
|
||||
Reference in New Issue
Block a user