1
Fork 0
mirror of https://github.com/Steffo99/greed.git synced 2024-11-22 05:54:18 +00:00

#18: Recalculate credit on every new transaction

Might slow down the bot if there are a lot of transactions... Let's see what happens.
This commit is contained in:
Steffo 2020-03-27 18:14:37 +01:00
parent 611eb7e1db
commit 3caa5ef179
2 changed files with 16 additions and 10 deletions

View file

@ -1,7 +1,7 @@
import typing import typing
from sqlalchemy import create_engine, Column, ForeignKey, UniqueConstraint from sqlalchemy import create_engine, Column, ForeignKey, UniqueConstraint
from sqlalchemy import Integer, BigInteger, String, Text, LargeBinary, DateTime, Boolean 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 from sqlalchemy.ext.declarative import declarative_base
import configloader import configloader
import telegram import telegram
@ -69,6 +69,11 @@ class User(TableDeclarativeBase):
else: else:
return f"[{self.first_name}](tg://user?id={self.user_id})" 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): def __repr__(self):
return f"<User {self} having {self.credit} credit>" return f"<User {self} having {self.credit} credit>"

View file

@ -505,8 +505,10 @@ class ChatWorker(threading.Thread):
value=value, value=value,
order_id=order.order_id) order_id=order.order_id)
self.session.add(transaction) self.session.add(transaction)
# Subtract credit from the user # Commit all the changes
self.user.credit += value self.session.commit()
# Update the user's credit
self.user.recalculate_credit()
# Commit all the changes # Commit all the changes
self.session.commit() self.session.commit()
# Notify the user of the order result # Notify the user of the order result
@ -663,10 +665,9 @@ class ChatWorker(threading.Thread):
transaction.payment_name = successfulpayment.order_info.name transaction.payment_name = successfulpayment.order_info.name
transaction.payment_email = successfulpayment.order_info.email transaction.payment_email = successfulpayment.order_info.email
transaction.payment_phone = successfulpayment.order_info.phone_number transaction.payment_phone = successfulpayment.order_info.phone_number
# Add the credit to the user account # Update the user's credit
self.user.credit += successfulpayment.total_amount - int(total_fee) self.user.recalculate_credit()
# Add and commit the transaction # Commit all the changes
self.session.add(transaction)
self.session.commit() self.session.commit()
def __bot_info(self): def __bot_info(self):
@ -957,8 +958,8 @@ class ChatWorker(threading.Thread):
order.refund_reason = reply order.refund_reason = reply
# Refund the credit, reverting the old transaction # Refund the credit, reverting the old transaction
order.transaction.refunded = True order.transaction.refunded = True
# Restore the credit to the user # Update the user's credit
order.user.credit -= order.transaction.value order.user.recalculate_credit()
# Commit the changes # Commit the changes
self.session.commit() self.session.commit()
# Update the order message # Update the order message
@ -1005,7 +1006,7 @@ class ChatWorker(threading.Thread):
notes=reply) notes=reply)
self.session.add(transaction) self.session.add(transaction)
# Change the user credit # Change the user credit
user.credit += int(price) user.recalculate_credit()
# Commit the changes # Commit the changes
self.session.commit() self.session.commit()
# Notify the user of the credit/debit # Notify the user of the credit/debit