from django.db import models from django.conf import settings from django.utils.translation import gettext_lazy as _ class CreditTransaction(models.Model): class TransactionType(models.TextChoices): BONUS = "bonus", _("Bonus") RECHARGE = "recharge", _("Recharge") REDEEM_LAB = "redeem_lab", _("Redeem Lab") REDEEM_EXAM = "redeem_exam", _("Redeem Exam") # Lab was failed, refund the credit. REDEEM_LAB_REFUND = "redeem_lab_refund", _("Redeem Lab Refund") BUY_CREDITS = "buy_credits", _("Buy Credits") OTHER = "other", _("Other") user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING) transaction_type = models.CharField(max_length=20, choices=TransactionType.choices) paid_credits = models.IntegerField(default=0) free_credits = models.IntegerField(default=0) # Credit transaction log for debugging purposes. # { # "paid_credit": { # "id": 1, # "amount": -100, # }, # "free_credit": [ # {"id": 2, "amount": -100, "expires_at": "2025-01-01 00:00:00"}, # {"id": 3, "amount": -50, "expires_at": "2025-01-01 00:00:00"}, # ] # } log = models.JSONField( default=dict, help_text=("The log of the credit transaction.") ) redemption = models.ForeignKey( "labs.UserLabRedemption", on_delete=models.DO_NOTHING, null=True, blank=True, help_text="The lab redemption of the credit transaction.", ) exam_redemption = models.ForeignKey( "labs.UserExamRedemption", on_delete=models.DO_NOTHING, null=True, blank=True, help_text="The exam redemption of the credit transaction.", ) reference_transaction = models.ForeignKey( "self", on_delete=models.DO_NOTHING, null=True, blank=True, help_text="The reference transaction of the credit transaction.", ) remark = models.CharField(max_length=255, null=True, blank=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = "billing_credit_transactions"