Skip to content

Commit

Permalink
Add roles property to details object in each returned respondent
Browse files Browse the repository at this point in the history
  • Loading branch information
sultanofcardio committed Jan 10, 2025
1 parent b99075d commit d6c35fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/apps/workspaces/crud/user_applet_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,31 @@ async def get_workspace_respondents(
.exists()
)

# Subquery to get the list of roles for each user and applet
roles_subquery = (
select(
UserAppletAccessSchema.user_id,
UserAppletAccessSchema.applet_id,
func.array_agg(
aggregate_order_by(
UserAppletAccessSchema.role,
case(
(UserAppletAccessSchema.role == Role.OWNER, 1),
(UserAppletAccessSchema.role == Role.MANAGER, 2),
(UserAppletAccessSchema.role == Role.COORDINATOR, 3),
(UserAppletAccessSchema.role == Role.EDITOR, 4),
(UserAppletAccessSchema.role == Role.REVIEWER, 5),
(UserAppletAccessSchema.role == Role.RESPONDENT, 6),
else_=10,
).asc(),
)
).label("roles"),
)
.where(UserAppletAccessSchema.soft_exists())
.group_by(UserAppletAccessSchema.applet_id, UserAppletAccessSchema.user_id)
.subquery()
)

query: Query = select(
# fmt: off
UserSchema.id,
Expand Down Expand Up @@ -518,6 +543,8 @@ async def get_workspace_respondents(
SubjectSchema.last_name,
text("'subject_created_at'"),
SubjectSchema.created_at,
text("'roles'"),
func.coalesce(roles_subquery.c.roles, []),
)
).label("details"),
)
Expand All @@ -534,6 +561,14 @@ async def get_workspace_respondents(
),
isouter=True,
)
query = query.join(
roles_subquery,
and_(
roles_subquery.c.user_id == SubjectSchema.user_id,
roles_subquery.c.applet_id == SubjectSchema.applet_id,
),
isouter=True,
)

query = query.where(
has_access,
Expand Down
2 changes: 2 additions & 0 deletions src/apps/workspaces/domain/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class WorkspaceRespondentDetails(InternalModel):
subject_last_name: str
subject_created_at: datetime.datetime
invitation: InvitationDetail | None = None
roles: list[Role]

@validator("respondent_nickname", "subject_first_name", "subject_last_name", pre=True)
def decrypt_fields(cls, value):
Expand Down Expand Up @@ -202,6 +203,7 @@ class PublicWorkspaceRespondentDetails(PublicModel):
subject_last_name: str
subject_created_at: datetime.datetime
invitation: InvitationResponse | None = None
roles: list[Role]


class PublicWorkspaceRespondent(PublicModel):
Expand Down

0 comments on commit d6c35fc

Please sign in to comment.