diff --git a/database.py b/database.py index ee51c8b..4bdbfe4 100644 --- a/database.py +++ b/database.py @@ -1,7 +1,7 @@ import typing from sqlalchemy import create_engine, Column, ForeignKey, UniqueConstraint from sqlalchemy import Integer, BigInteger, String, Text, LargeBinary, DateTime, Boolean -from sqlalchemy.orm import sessionmaker, relationship +from sqlalchemy.orm import sessionmaker, relationship, backref from sqlalchemy.ext.declarative import declarative_base import configloader import telegram @@ -69,6 +69,11 @@ class User(TableDeclarativeBase): else: return f"[{self.first_name}](tg://user?id={self.user_id})" + def recalculate_credit(self): + """Recalculate the credit for this user by calculating the sum of the values of all their transactions.""" + valid_transactions: typing.List[Transaction] = [t for t in self.transactions if not t.refunded] + self.credit = sum(map(lambda t: t.value, valid_transactions)) + def __repr__(self): return f"" diff --git a/worker.py b/worker.py index 2361a81..4de75af 100644 --- a/worker.py +++ b/worker.py @@ -505,8 +505,10 @@ class ChatWorker(threading.Thread): value=value, order_id=order.order_id) self.session.add(transaction) - # Subtract credit from the user - self.user.credit += value + # Commit all the changes + self.session.commit() + # Update the user's credit + self.user.recalculate_credit() # Commit all the changes self.session.commit() # Notify the user of the order result @@ -663,10 +665,9 @@ class ChatWorker(threading.Thread): transaction.payment_name = successfulpayment.order_info.name transaction.payment_email = successfulpayment.order_info.email transaction.payment_phone = successfulpayment.order_info.phone_number - # Add the credit to the user account - self.user.credit += successfulpayment.total_amount - int(total_fee) - # Add and commit the transaction - self.session.add(transaction) + # Update the user's credit + self.user.recalculate_credit() + # Commit all the changes self.session.commit() def __bot_info(self): @@ -957,8 +958,8 @@ class ChatWorker(threading.Thread): order.refund_reason = reply # Refund the credit, reverting the old transaction order.transaction.refunded = True - # Restore the credit to the user - order.user.credit -= order.transaction.value + # Update the user's credit + order.user.recalculate_credit() # Commit the changes self.session.commit() # Update the order message @@ -1005,7 +1006,7 @@ class ChatWorker(threading.Thread): notes=reply) self.session.add(transaction) # Change the user credit - user.credit += int(price) + user.recalculate_credit() # Commit the changes self.session.commit() # Notify the user of the credit/debit