feat: add billing core models
This commit is contained in:
+119
-1
@@ -1,3 +1,121 @@
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
|
||||
# Create your models here.
|
||||
from apps.users.models import ApiKey
|
||||
|
||||
|
||||
class CallRecord(models.Model):
|
||||
class OperationType(models.TextChoices):
|
||||
TITLE = "title", "Generate title"
|
||||
IMAGE = "image", "Generate image"
|
||||
|
||||
class Status(models.TextChoices):
|
||||
PENDING = "pending", "Pending"
|
||||
SUCCESS = "success", "Success"
|
||||
FAILED = "failed", "Failed"
|
||||
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
on_delete=models.PROTECT,
|
||||
related_name="call_records",
|
||||
)
|
||||
api_key = models.ForeignKey(
|
||||
ApiKey,
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.SET_NULL,
|
||||
related_name="call_records",
|
||||
)
|
||||
operation_type = models.CharField(max_length=32, choices=OperationType.choices)
|
||||
alias = models.CharField(max_length=64, blank=True)
|
||||
model_used = models.CharField(max_length=128, blank=True)
|
||||
resolution = models.CharField(max_length=32, blank=True)
|
||||
prompt = models.TextField(blank=True)
|
||||
points_cost = models.BigIntegerField(default=0)
|
||||
status = models.CharField(
|
||||
max_length=20,
|
||||
choices=Status.choices,
|
||||
default=Status.PENDING,
|
||||
)
|
||||
upstream_latency_ms = models.PositiveIntegerField(null=True, blank=True)
|
||||
error_message = models.TextField(blank=True)
|
||||
result_ref = models.TextField(blank=True)
|
||||
result_summary = models.TextField(blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "call_record"
|
||||
ordering = ("-created_at", "-id")
|
||||
constraints = [
|
||||
models.CheckConstraint(
|
||||
condition=Q(points_cost__gte=0),
|
||||
name="call_record_points_cost_non_negative",
|
||||
),
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=("user", "created_at")),
|
||||
models.Index(fields=("api_key", "created_at")),
|
||||
models.Index(fields=("operation_type", "status")),
|
||||
models.Index(fields=("alias",)),
|
||||
]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.operation_type}:{self.alias or '<default>'} {self.status}"
|
||||
|
||||
|
||||
class PointsLedger(models.Model):
|
||||
class ChangeType(models.TextChoices):
|
||||
RECHARGE = "recharge", "Recharge"
|
||||
CONSUME = "consume", "Consume"
|
||||
ADJUST = "adjust", "Adjust"
|
||||
REFUND = "refund", "Refund"
|
||||
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
on_delete=models.PROTECT,
|
||||
related_name="points_ledger_entries",
|
||||
)
|
||||
change_type = models.CharField(max_length=20, choices=ChangeType.choices)
|
||||
points_delta = models.BigIntegerField()
|
||||
balance_after = models.BigIntegerField()
|
||||
ref_order_id = models.PositiveBigIntegerField(null=True, blank=True)
|
||||
ref_call = models.ForeignKey(
|
||||
CallRecord,
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.PROTECT,
|
||||
related_name="ledger_entries",
|
||||
)
|
||||
reason = models.TextField(blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "points_ledger"
|
||||
ordering = ("-created_at", "-id")
|
||||
constraints = [
|
||||
models.CheckConstraint(
|
||||
condition=Q(balance_after__gte=0),
|
||||
name="points_ledger_balance_after_non_negative",
|
||||
),
|
||||
models.CheckConstraint(
|
||||
condition=~Q(points_delta=0),
|
||||
name="points_ledger_points_delta_non_zero",
|
||||
),
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=("user", "created_at")),
|
||||
models.Index(fields=("change_type", "created_at")),
|
||||
models.Index(fields=("ref_order_id",)),
|
||||
models.Index(fields=("ref_call",)),
|
||||
]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.user} {self.change_type} {self.points_delta}"
|
||||
|
||||
def clean(self) -> None:
|
||||
super().clean()
|
||||
if self.change_type == self.ChangeType.ADJUST and not (self.reason or "").strip():
|
||||
raise ValidationError({"reason": "Adjust ledger entries require a reason."})
|
||||
|
||||
Reference in New Issue
Block a user