Skip to content

Commit

Permalink
fix: partner statistics APIs: detect invalid group id
Browse files Browse the repository at this point in the history
  • Loading branch information
bshankar committed Sep 22, 2024
1 parent 174fa89 commit 25c6a1b
Showing 1 changed file with 65 additions and 48 deletions.
113 changes: 65 additions & 48 deletions backend/services/mapswipe_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from backend.exceptions import Conflict
from backend.models.dtos.partner_stats_dto import (
GroupedPartnerStatsDTO,
FilteredPartnerStatsDTO,
Expand Down Expand Up @@ -78,53 +79,56 @@ def __build_query_filtered_user_group_stats(
operationName = "FilteredUserGroupStats"
query = """
query FilteredUserGroupStats($pk: ID!, $fromDate: DateTime!, $toDate: DateTime!) {
userGroup(pk: $pk) {
id
}
userGroupStats(userGroupId: $pk) {
id
filteredStats(dateRange: {fromDate: $fromDate, toDate: $toDate}) {
userStats {
totalMappingProjects
totalSwipeTime
totalSwipes
username
userId
__typename
}
contributionByGeo {
geojson
totalContribution
__typename
}
areaSwipedByProjectType {
totalArea
projectTypeDisplay
projectType
__typename
}
swipeByDate {
taskDate
totalSwipes
__typename
}
swipeTimeByDate {
date
totalSwipeTime
__typename
}
swipeByProjectType {
projectType
projectTypeDisplay
totalSwipes
__typename
}
swipeByOrganizationName {
organizationName
totalSwipes
__typename
}
__typename
}
__typename
}
filteredStats(dateRange: {fromDate: $fromDate, toDate: $toDate}) {
userStats {
totalMappingProjects
totalSwipeTime
totalSwipes
username
userId
__typename
}
contributionByGeo {
geojson
totalContribution
__typename
}
areaSwipedByProjectType {
totalArea
projectTypeDisplay
projectType
__typename
}
swipeByDate {
taskDate
totalSwipes
__typename
}
swipeTimeByDate {
date
totalSwipeTime
__typename
}
swipeByProjectType {
projectType
projectTypeDisplay
totalSwipes
__typename
}
swipeByOrganizationName {
organizationName
totalSwipes
__typename
}
__typename
}
__typename
}
}
"""
variables = {"fromDate": from_date, "toDate": to_date, "pk": group_id}
Expand All @@ -138,6 +142,13 @@ def setup_group_dto(
group_dto.id = partner_id
group_dto.provider = "mapswipe"
group_dto.id_inside_provider = group_id

if group_stats["userGroup"] is None:
raise Conflict(
"INVALID_MAPSWIPE_GROUP_ID",
"The mapswipe group ID linked to this partner is invalid. Please contact an admin.",
)

group_dto.name_inside_provider = group_stats["userGroup"]["name"]
group_dto.description_inside_provider = group_stats["userGroup"]["description"]

Expand Down Expand Up @@ -190,9 +201,15 @@ def setup_filtered_dto(
filtered_stats_dto.from_date = from_date
filtered_stats_dto.to_date = to_date

filtered_stats = json.loads(resp_body)["data"]["userGroupStats"][
"filteredStats"
]
filtered_stats = json.loads(resp_body)["data"]

if filtered_stats["userGroup"] is None:
raise Conflict(
"INVALID_MAPSWIPE_GROUP_ID",
"The mapswipe group ID linked to this partner is invalid. Please contact an admin.",
)

filtered_stats = filtered_stats["userGroupStats"]["filteredStats"]

stats_by_user = []
for user_stats in filtered_stats["userStats"]:
Expand Down

0 comments on commit 25c6a1b

Please sign in to comment.