Skip to content

Commit

Permalink
Add /:id/users endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
RyEggGit committed Jan 18, 2024
1 parent f66b2fe commit 90f079c
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 83 deletions.
83 changes: 60 additions & 23 deletions backend/routes/partners.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from flask import Blueprint, abort, current_app, request
from flask_jwt_extended import get_jwt
from flask_jwt_extended.view_decorators import jwt_required

from flask_sqlalchemy import Pagination
from ..database import Partner, PartnerMember, MemberRole, db
from ..schemas import (
CreatePartnerSchema,
AddMemberSchema,
partner_orm_to_json,
partner_member_orm_to_json,
user_orm_to_json,
partner_member_to_orm,
partner_to_orm,
validate,
Expand Down Expand Up @@ -54,10 +55,11 @@ def create_partner():
)
make_admin.create()

track_to_mp(request, "create_partner", {
"partner_name": partner.name,
"partner_contact": partner.contact_email
})
track_to_mp(
request,
"create_partner",
{"partner_name": partner.name, "partner_contact": partner.contact_email},
)
return partner_orm_to_json(created)


Expand All @@ -76,9 +78,7 @@ def get_all_partners():
q_per_page = args.get("per_page", 20, type=int)

all_partners = db.session.query(Partner)
results = all_partners.paginate(
page=q_page, per_page=q_per_page, max_per_page=100
)
results = all_partners.paginate(page=q_page, per_page=q_per_page, max_per_page=100)

return {
"results": [partner_orm_to_json(partner) for partner in results.items],
Expand Down Expand Up @@ -106,14 +106,10 @@ def get_partner_members(partner_id: int):
all_members = db.session.query(PartnerMember).filter(
PartnerMember.partner_id == partner_id
)
results = all_members.paginate(
page=q_page, per_page=q_per_page, max_per_page=100)
results = all_members.paginate(page=q_page, per_page=q_per_page, max_per_page=100)

return {
"results": [
partner_member_orm_to_json(member)
for member in results.items
],
"results": [partner_member_orm_to_json(member) for member in results.items],
"page": results.page,
"totalPages": results.pages,
"totalResults": results.total,
Expand All @@ -136,6 +132,39 @@ class Config:
} """


@bp.route("/<int:partner_id>/users", methods=["GET"])
@jwt_required() # type: ignore
@min_role_required(UserRole.PUBLIC)
@validate() # type: ignore
def get_partner_users(partner_id: int):
# Get the page number from the query parameters (default to 1)
page = request.args.get("page", 1, type=int)

# Get the number of items per page from the query parameters (default to 20)
per_page = request.args.get("per_page", 20, type=int)

# Query the PartnerMember table for records with the given partner_id and paginate the results
pagination: Pagination = PartnerMember.query.filter_by(
partner_id=partner_id
).paginate(page=page, per_page=per_page, error_out=False)

# If the partner_id is invalid, return a 404 error
if pagination.total == 0:
return {"message": "Partner not found"}, 404

# Get the User objects associated with the members on the current page
users: list[User] = [User.query.get(member.user_id) for member in pagination.items] # type: ignore

# Convert the User objects to dictionaries and return them as JSON

return {
"results": [user_orm_to_json(user) for user in users],
"page": pagination.page,
"totalPages": pagination.pages,
"totalResults": pagination.total,
}


@bp.route("/<int:partner_id>/members/add", methods=["POST"])
@jwt_required()
@min_role_required(UserRole.PUBLIC)
Expand All @@ -157,10 +186,14 @@ def add_member_to_partner(partner_id: int):
jwt_decoded = get_jwt()

current_user = User.get(jwt_decoded["sub"])
association = db.session.query(PartnerMember).filter(
PartnerMember.user_id == current_user.id,
PartnerMember.partner_id == partner_id,
).first()
association = (
db.session.query(PartnerMember)
.filter(
PartnerMember.user_id == current_user.id,
PartnerMember.partner_id == partner_id,
)
.first()
)

if (
association is None
Expand All @@ -187,9 +220,13 @@ def add_member_to_partner(partner_id: int):

created = partner_member.create()

track_to_mp(request, "add_partner_member", {
"partner_id": partner_id,
"user_id": partner_member.user_id,
"role": partner_member.role,
})
track_to_mp(
request,
"add_partner_member",
{
"partner_id": partner_id,
"user_id": partner_member.user_id,
"role": partner_member.role,
},
)
return partner_member_orm_to_json(created)
15 changes: 11 additions & 4 deletions backend/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ class CreatePartnerMemberSchema(BaseModel):


AddMemberSchema = sqlalchemy_to_pydantic(
PartnerMember,
exclude=["id", "date_joined", "partner", "user"]
PartnerMember, exclude=["id", "date_joined", "partner", "user"]
)


Expand Down Expand Up @@ -297,8 +296,7 @@ def partner_orm_to_json(partner: Partner) -> dict:
)


def partner_member_to_orm(
partner_member: CreatePartnerMemberSchema) -> PartnerMember:
def partner_member_to_orm(partner_member: CreatePartnerMemberSchema) -> PartnerMember:
"""Convert the JSON partner member into an ORM instance"""
orm_attrs = partner_member.dict()
return PartnerMember(**orm_attrs)
Expand All @@ -308,3 +306,12 @@ def partner_member_orm_to_json(partner_member: PartnerMember) -> dict:
return PartnerMemberSchema.from_orm(partner_member).dict(
exclude_none=True,
)


def user_orm_to_json(user: User) -> Dict[str, Any]:
return UserSchema.from_orm(user).dict(
exclude={
"password",
"email_confirmed_at",
}
)
Loading

0 comments on commit 90f079c

Please sign in to comment.