Skip to content

Commit

Permalink
merging :D
Browse files Browse the repository at this point in the history
  • Loading branch information
saantim committed Jun 17, 2024
2 parents cded844 + 154334c commit e856a8b
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 62 deletions.
47 changes: 41 additions & 6 deletions src/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ def get_groups_by_user_id(db: Session, user_id: int):
db.execute(
select(models.Group)
.where(models.Group.members.any(models.User.id == user_id))
.join(
models.Balance,
(models.Group.id == models.Balance.group_id)
& (models.Balance.user_id == user_id),
)
.filter(models.Balance.left != True)
.limit(100)
)
.scalars()
Expand All @@ -119,6 +125,11 @@ def get_group_by_id(db: Session, group_id: int):
return db.query(models.Group).filter(models.Group.id == group_id).first()


def get_active_members(db: Session, group: models.Group):
balances = set(b.user_id for b in get_balances_by_group_id(db, group.id))
return list(filter(lambda u: u.id in balances, group.members))


def update_group_status(db: Session, group: models.Group, status: bool):
group.is_archived = status
db.commit()
Expand All @@ -133,6 +144,21 @@ def add_user_to_group(db: Session, user: models.User, group: models.Group):
return group


def readd_user_to_group(db: Session, user: models.User, group: models.Group):
balance = get_user_balance(db, user.id, group.id)
balance.left = False
db.commit()
db.refresh(balance)
return balance


def leave_group(db: Session, balance: models.Balance):
balance.left = True
db.commit()
db.refresh(balance)
return balance


################################################
# ALL SPENDINGS
################################################
Expand Down Expand Up @@ -277,7 +303,7 @@ def put_recurring_spendings(
):
db_recurring_spending.amount = put_recurring_spending.amount
db_recurring_spending.description = put_recurring_spending.description
db_recurring_spending.category_id = put_recurring_spending.categiry_id
db_recurring_spending.category_id = put_recurring_spending.category_id
db.commit()
db.refresh(db_recurring_spending)
return db_recurring_spending
Expand Down Expand Up @@ -446,10 +472,9 @@ def update_balances_from_spending(db: Session, spending: models.UniqueSpending,
balances = sorted(
get_balances_by_group_id(db, spending.group_id), key=lambda x: x.user_id
)
members = sorted(group.members, key=lambda x: x.id)
# TODO: implement division strategy
# TODO: this truncates decimals
# TODO: Refactor to POO

members = sorted(get_active_members(db, group), key=lambda x: x.id)
# TODO: Refactor to OOP
if strategy == models.Strategy.EQUALPARTS:
update_balances_equals(db, spending, members, balances)
elif strategy == models.Strategy.PERCENTAGE:
Expand Down Expand Up @@ -481,7 +506,17 @@ def update_balances_from_payment(db: Session, payment: models.Payment):
def get_balances_by_group_id(db: Session, group_id: int) -> List[models.Balance]:
return (
db.query(models.Balance)
.filter(models.Balance.group_id == group_id)
.filter(models.Balance.group_id == group_id, models.Balance.left != True)
.limit(100)
.all()
)


def get_user_balance(
db: Session, user_id: int, group_id: int
) -> Optional[models.Balance]:
return (
db.query(models.Balance)
.filter(models.Balance.user_id == user_id, models.Balance.group_id == group_id)
.first()
)
Loading

0 comments on commit e856a8b

Please sign in to comment.