Skip to content

Commit

Permalink
fix: return onlu active users and remove users_group massive
Browse files Browse the repository at this point in the history
  • Loading branch information
RezenkovD committed Sep 28, 2023
1 parent edaa82b commit fb950f5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/routers/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ def update_group(
return services.update_group(db, current_user.id, group, group_id)


@router.get("/{group_id}/users/", response_model=Page[UsersGroup])
@router.get("/{group_id}/users/", response_model=Page[AboutUser])
def read_users_group(
*,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
group_id: int,
) -> Page[UsersGroup]:
) -> Page[AboutUser]:
return paginate(db, services.read_users_group(db, current_user.id, group_id))


Expand Down
21 changes: 18 additions & 3 deletions src/services/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
UserDailyExpenses,
UserDailyExpensesDetail,
)
from enums import GroupStatusEnum


def user_validate_input_date(
Expand Down Expand Up @@ -127,7 +128,14 @@ def read_group_info(db: Session, user_id: int, group_id: int) -> GroupInfo:
db.query(Group).options(joinedload(Group.admin)).filter_by(id=group_id).one()
)
group_users_count = (
db.query(func.count(UserGroup.user_id)).filter_by(group_id=group_id).scalar()
db.query(func.count(UserGroup.user_id))
.filter(
and_(
UserGroup.group_id == group_id,
UserGroup.status == GroupStatusEnum.ACTIVE,
)
)
.scalar()
)
group_expenses_count = (
db.query(func.count(Expense.id)).filter_by(group_id=group_id).scalar()
Expand Down Expand Up @@ -318,10 +326,17 @@ def add_user_in_group(db: Session, user_id: int, group_id: int) -> None:
db.add(db_user_group)


def read_users_group(db: Session, user_id: int, group_id: int) -> UsersGroup:
def read_users_group(db: Session, user_id: int, group_id: int) -> List[AboutUser]:
user_validate_input_date(db, user_id, group_id)
db_query = (
select(Group).options(joinedload(Group.users_group)).filter_by(id=group_id)
select(UserGroup)
.options(joinedload(UserGroup.user))
.filter(
and_(
UserGroup.group_id == group_id,
UserGroup.status == GroupStatusEnum.ACTIVE,
)
)
)
return db_query

Expand Down
22 changes: 9 additions & 13 deletions tests/test_endpoints/test_group_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,15 @@ def test_read_users_group(self) -> None:
users_group_data = {
"items": [
{
"users_group": [
{
"status": GroupStatusEnum.ACTIVE,
"date_join": datetime.date.today().strftime("%Y-%m-%d"),
"user": {
"id": self.user.id,
"first_name": self.user.first_name,
"last_name": self.user.last_name,
"login": self.user.login,
"picture": self.user.picture,
},
}
],
"status": GroupStatusEnum.ACTIVE,
"date_join": datetime.date.today().strftime("%Y-%m-%d"),
"user": {
"id": self.user.id,
"first_name": self.user.first_name,
"last_name": self.user.last_name,
"login": self.user.login,
"picture": self.user.picture,
},
}
],
"total": 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_endpoints/test_invitation_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def test_response_invitation(self) -> None:
users = [self.second_user, self.first_user]
group_users = client.get(f"/groups/{self.second_group.id}/users/")
assert group_users.status_code == 200
group_users = group_users.json()["items"][0]["users_group"]
group_users = group_users.json()["items"]
assert len(group_users) == len(users)
for group_user, user in zip(group_users, users):
assert group_user["user"]["id"] == user.id
Expand Down

0 comments on commit fb950f5

Please sign in to comment.