Skip to content

Commit

Permalink
♻️ return the counter when withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Burg authored and quentin-burg committed Dec 19, 2023
1 parent 2c027cf commit 58c62b5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,16 @@ def get_user_credits(db: Session, user_id: str):

def update_user_withdraw_counter(db: Session, user_id: str, withdraw_counter: int):
try:
db_user: Optional[models.User] = db.query(models.User).get(user_id)
if db_user is None:
raise UserNotFound()

db.query(models.User).filter(models.User.id == user_id).update(
{"withdraw_counter": withdraw_counter}
)

db.commit()
return db_user.withdraw_counter
except NoResultFound as e:
raise UserNotFound from e

Expand Down
4 changes: 2 additions & 2 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class User(Base):
__tablename__ = "users"

def __repr__(self):
return "User(id='{}', name='{}', address='{}')".format(
self.id, self.name, self.address
return "User(id='{}', name='{}', address='{}', counter='{}')".format(
self.id, self.name, self.address, self.withdraw_counter
)

id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
Expand Down
6 changes: 4 additions & 2 deletions src/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ async def withdraw_credits(
)
# We increment the counter even if the withdraw fails to prevent
# the counter from being used again immediately.
crud.update_user_withdraw_counter(db, str(user.id), withdraw.withdraw_counter + 1)
counter = crud.update_user_withdraw_counter(
db, str(user.id), withdraw.withdraw_counter + 1
)
result = await tezos.withdraw(tezos.tezos_manager, owner_address, withdraw.amount)
if result["result"] == "ok":
logging.debug(f"Start to confirm withdraw for {result['transaction_hash']}")
Expand All @@ -148,7 +150,7 @@ async def withdraw_credits(
)
)

return result
return {**result, "counter": counter}


# Users and credits getters
Expand Down

0 comments on commit 58c62b5

Please sign in to comment.