diff --git a/src/dependencies.py b/src/dependencies.py index 886e798..b00b5f4 100644 --- a/src/dependencies.py +++ b/src/dependencies.py @@ -33,6 +33,16 @@ ) +def is_user_authenticated(request: Request, db: Session = Depends(get_db)) -> bool: + try: + user_info = request.session["user"] + except KeyError: + return False + if get_user(db, user_info["email"]) is None: + return False + return True + + def get_current_user(request: Request, db: Session = Depends(get_db)) -> User: try: user_info = request.session["user"] diff --git a/src/routers/group.py b/src/routers/group.py index 4dbd258..8652d8c 100644 --- a/src/routers/group.py +++ b/src/routers/group.py @@ -522,3 +522,18 @@ def read_group_member_daily_expenses_detail( return services.group_member_daily_expenses_detail( db, current_user.id, group_id, member_id ) + + +@router.get( + "/{group_id}/member/{member_id}/history/", response_model=Page[GroupHistory] +) +def read_group_history( + *, + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user), + group_id: int, + member_id: int, +) -> Page[GroupHistory]: + return paginate( + db, services.group_member_history(db, current_user.id, group_id, member_id) + ) diff --git a/src/routers/user.py b/src/routers/user.py index bf01e46..caf4d7d 100644 --- a/src/routers/user.py +++ b/src/routers/user.py @@ -14,6 +14,7 @@ transform_date_or_422, transform_exact_date_or_422, Page, + is_user_authenticated, ) from models import User, Expense from schemas import ( @@ -33,6 +34,11 @@ ) +@router.get("/check-auth/") +def check_authentication(authenticated: bool = Depends(is_user_authenticated)): + return authenticated + + @router.get("/", response_model=Page[UserModel]) def read_users(db: Session = Depends(get_db)) -> Page[UserModel]: return paginate(db, select(User)) diff --git a/src/services/__init__.py b/src/services/__init__.py index 1909700..646ae6d 100644 --- a/src/services/__init__.py +++ b/src/services/__init__.py @@ -39,6 +39,7 @@ group_member_category_expenses, group_member_daily_expenses, group_member_daily_expenses_detail, + group_member_history, ) from .invitation import create_invitation, read_invitations, response_invitation from .replenishment import ( diff --git a/src/services/group.py b/src/services/group.py index b4ddb0f..58a03a4 100644 --- a/src/services/group.py +++ b/src/services/group.py @@ -1472,3 +1472,67 @@ def group_member_daily_expenses_detail( result_list = list(result_dict.values()) return result_list + + +def group_member_history( + db: Session, + current_user: int, + group_id: int, + member_id: int, +) -> List[GroupHistory]: + try: + ( + db.query(UserGroup) + .filter_by( + user_id=current_user, + group_id=group_id, + ) + .one() + ) + except: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="You are not in this group!", + ) + try: + ( + db.query(UserGroup) + .filter_by( + user_id=member_id, + group_id=group_id, + ) + .one() + ) + except: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="This user is not in this group!", + ) + member_history = ( + select( + Expense.id, + Expense.descriptions, + Expense.amount, + Expense.time, + Expense.category_id, + CategoryGroup.color_code.label("color_code_category"), + Category.title.label("title_category"), + User.id.label("user_id"), + User.login.label("user_login"), + User.first_name.label("user_first_name"), + User.last_name.label("user_last_name"), + User.picture.label("user_picture"), + ) + .join( + CategoryGroup, + and_( + Expense.category_id == CategoryGroup.category_id, + Expense.group_id == CategoryGroup.group_id, + ), + ) + .join(Category, Expense.category_id == Category.id) + .join(User, User.id == Expense.user_id) + .filter(and_(Expense.group_id == group_id, Expense.user_id == member_id)) + .order_by(desc(Expense.time)) + ) + return member_history