Skip to content

Commit

Permalink
feat: add group member dashboard
Browse files Browse the repository at this point in the history
* feat: add group member info

* ref: replace member_id with user_id

* feat: add category expenses for group member

* feat: add daily expenses for group member

* feat: add group member daily expenses
  • Loading branch information
RezenkovD authored Aug 31, 2023
1 parent ef86474 commit d7d103d
Show file tree
Hide file tree
Showing 5 changed files with 769 additions and 11 deletions.
185 changes: 185 additions & 0 deletions src/routers/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
CategoryExpenses,
GroupDailyExpenses,
GroupDailyExpensesDetail,
GroupMember,
UserDailyExpenses,
UserDailyExpensesDetail,
)
from dependencies import Page, transform_date_or_422, transform_exact_date_or_422

Expand Down Expand Up @@ -337,3 +340,185 @@ def read_group_daily_expenses(
)
else:
return services.read_group_daily_expenses_detail(db, current_user.id, group_id)


@router.get(
"/{group_id}/member/{member_id}/info/",
response_model=GroupMember,
)
def read_group_member_info(
*,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
group_id: int,
member_id: int,
year_month: Optional[str] = None,
start_date: Optional[str] = None,
end_date: Optional[str] = None,
) -> GroupMember:
if year_month and (start_date or end_date):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Cannot use filter_date with start_date or end_date",
)
elif (start_date and not end_date) or (end_date and not start_date):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Both start_date and end_date are required",
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_member_info(
db, current_user.id, group_id, member_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_member_info(
db,
current_user.id,
group_id,
member_id,
start_date=start_date,
end_date=end_date,
)
else:
return services.group_member_info(db, current_user.id, group_id, member_id)


@router.get(
"/{group_id}/member/{member_id}/category-expenses/",
response_model=List[CategoryExpenses],
)
def read_group_category_expenses(
*,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
group_id: int,
member_id: int,
year_month: Optional[str] = None,
start_date: Optional[str] = None,
end_date: Optional[str] = None,
) -> List[CategoryExpenses]:
if year_month and (start_date or end_date):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Cannot use filter_date with start_date or end_date",
)
elif (start_date and not end_date) or (end_date and not start_date):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Both start_date and end_date are required",
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_member_category_expenses(
db, current_user.id, group_id, member_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_member_category_expenses(
db,
current_user.id,
group_id,
member_id,
start_date=start_date,
end_date=end_date,
)
else:
return services.group_member_category_expenses(
db, current_user.id, group_id, member_id
)


@router.get(
"/{group_id}/member/{member_id}/daily-expenses/",
response_model=List[UserDailyExpenses],
)
def read_group_member_daily_expenses(
*,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
group_id: int,
member_id: int,
year_month: Optional[str] = None,
start_date: Optional[str] = None,
end_date: Optional[str] = None,
) -> List[UserDailyExpenses]:
if year_month and (start_date or end_date):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Cannot use filter_date with start_date or end_date",
)
elif (start_date and not end_date) or (end_date and not start_date):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Both start_date and end_date are required",
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_member_daily_expenses(
db, current_user.id, group_id, member_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_member_daily_expenses(
db,
current_user.id,
group_id,
member_id,
start_date=start_date,
end_date=end_date,
)
else:
return services.group_member_daily_expenses(
db, current_user.id, group_id, member_id
)


@router.get(
"/{group_id}/member/{member_id}/daily-expenses-detail/",
response_model=List[UserDailyExpensesDetail],
)
def read_group_member_daily_expenses_detail(
*,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
group_id: int,
member_id: int,
year_month: Optional[str] = None,
start_date: Optional[str] = None,
end_date: Optional[str] = None,
) -> List[UserDailyExpensesDetail]:
if year_month and (start_date or end_date):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Cannot use filter_date with start_date or end_date",
)
elif (start_date and not end_date) or (end_date and not start_date):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Both start_date and end_date are required",
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_member_daily_expenses_detail(
db, current_user.id, group_id, member_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_member_daily_expenses_detail(
db,
current_user.id,
group_id,
member_id,
start_date=start_date,
end_date=end_date,
)
else:
return services.group_member_daily_expenses_detail(
db, current_user.id, group_id, member_id
)
2 changes: 2 additions & 0 deletions src/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
UserCategoryExpenses,
UserGroupExpenses,
CategoryExpenses,
GroupMember,
UserDailyExpensesDetail,
)
from .group import (
AboutCategory,
Expand Down
12 changes: 12 additions & 0 deletions src/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ class UserGroupExpenses(BaseModel):
group_id: int
group_title: str
categories: List[CategoryExpenses]


class GroupMember(UserModel):
count_expenses: int
total_expenses: UserTotalExpenses
best_category: Optional[CategoryExpenses] = None


class UserDailyExpensesDetail(BaseModel):
date: datetime.date
amount: float
categories: List[CategoryExpenses]
24 changes: 14 additions & 10 deletions src/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
read_expenses_by_group_month,
read_expenses_by_group_time_range,
)
from .user import (
get_user,
calculate_user_balance,
user_total_expenses,
user_total_replenishments,
user_history,
read_user_daily_expenses,
read_category_expenses,
read_group_expenses,
)
from .group import (
add_user_in_group,
create_group,
Expand All @@ -25,6 +35,10 @@
group_category_expenses,
read_group_daily_expenses,
read_group_daily_expenses_detail,
group_member_info,
group_member_category_expenses,
group_member_daily_expenses,
group_member_daily_expenses_detail,
)
from .invitation import create_invitation, read_invitations, response_invitation
from .replenishment import (
Expand All @@ -33,13 +47,3 @@
update_replenishment,
delete_replenishment,
)
from .user import (
get_user,
calculate_user_balance,
user_total_expenses,
user_total_replenishments,
user_history,
read_user_daily_expenses,
read_category_expenses,
read_group_expenses,
)
Loading

0 comments on commit d7d103d

Please sign in to comment.