Skip to content

Commit

Permalink
Feature/group member history and auth status
Browse files Browse the repository at this point in the history
* feat: add group member history

* feat: add router check auth for user
  • Loading branch information
RezenkovD authored Sep 3, 2023
1 parent d7d103d commit c1c7a6e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
15 changes: 15 additions & 0 deletions src/routers/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
6 changes: 6 additions & 0 deletions src/routers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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))
Expand Down
1 change: 1 addition & 0 deletions src/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
64 changes: 64 additions & 0 deletions src/services/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c1c7a6e

Please sign in to comment.