mirror of
https://github.com/Steffo99/greed.git
synced 2024-11-25 07:14: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:
parent
611eb7e1db
commit
3caa5ef179
2 changed files with 16 additions and 10 deletions
|
@ -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"<User {self} having {self.credit} credit>"
|
||||
|
||||
|
|
19
worker.py
19
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
|
||||
|
|
Loading…
Reference in a new issue