Skip to content

Commit

Permalink
Merge pull request #1839 from BLSQ/POLIO-1750_fix_file_filter_pdf_repo
Browse files Browse the repository at this point in the history
POLIO-1750: Rearrange PDF repository filters
  • Loading branch information
beygorghor authored Dec 4, 2024
2 parents 7f7caa5 + 745c568 commit 4263485
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 171 deletions.
33 changes: 10 additions & 23 deletions plugins/polio/api/vaccines/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,10 @@ class VaccineReportingFilterBackend(filters.BaseFilterBackend):
"""Filter backend for vaccine reporting that handles campaign status, country, and file type filtering."""

def filter_queryset(self, request, queryset, view):
# Filter by campaign status
campaign_status = request.query_params.get("campaign_status", None)

if campaign_status:
today = datetime.now().date()
if campaign_status.upper() == "ONGOING":
queryset = queryset.filter(campaign_started_at__lte=today, campaign_ended_at__gte=today)
elif campaign_status.upper() == "PAST":
queryset = queryset.filter(campaign_started_at__lt=today, campaign_ended_at__lt=today)
elif campaign_status.upper() == "PREPARING":
queryset = queryset.filter(campaign_started_at__gte=today)
# Filter by vaccine name (single)
vaccine_name = request.query_params.get("vaccine_name", None)
if vaccine_name:
queryset = queryset.filter(vaccine_name=vaccine_name)

# Filter by country block
country_block = request.query_params.get("country_block", None)
Expand All @@ -57,15 +50,6 @@ def filter_queryset(self, request, queryset, view):
except ValueError:
raise ValidationError("countries must be a comma-separated list of integers")

# Filter by campaign category
campaign_category = request.query_params.get("campaignCategory", None)
if campaign_category == "test":
queryset = queryset.filter(campaign__is_test=True)
if campaign_category == "preventive":
queryset = queryset.filter(campaign__is_preventive=True)
if campaign_category == "regular":
queryset = queryset.filter(campaign__is_preventive=False).filter(campaign__is_test=False)

# Filter by campaign
campaign = request.query_params.get("campaign", None)
if campaign:
Expand All @@ -76,14 +60,18 @@ def filter_queryset(self, request, queryset, view):
if file_type:
file_type = file_type.upper()
if file_type == "VRF":
queryset = queryset.filter(campaign__vaccinerequestform__isnull=False)
queryset = queryset.filter(
campaign__vaccinerequestform__isnull=False,
)
elif file_type == "PRE_ALERT":
queryset = queryset.filter(
campaign__vaccinerequestform__isnull=False,
campaign__vaccinerequestform__vaccineprealert__isnull=False,
).distinct("id")
elif file_type == "FORM_A":
queryset = queryset.filter(outgoingstockmovement__isnull=False)
queryset = queryset.filter(
outgoingstockmovement__isnull=False,
)

# Filter by VRF type
vrf_type = request.query_params.get("vrf_type", None)
Expand Down Expand Up @@ -303,7 +291,6 @@ def get_queryset(self):
"campaign_ended_at",
)

# 393 results without filter
rounds_queryset = rounds_queryset.annotate(
vaccine_name=Case(
When(campaign__separate_scopes_per_round=False, then="campaign__scopes__vaccine"),
Expand Down
6 changes: 2 additions & 4 deletions plugins/polio/js/src/constants/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ export const polioRouteConfigs: Record<string, RouteConfig> = {
...paginationPathParams,
'countries',
'campaignType',
'campaignCategory',
'file_type',
'country_block',
'campaignStatus',
'vaccine_name',
],
},
embeddedCalendar: {
Expand All @@ -115,10 +114,9 @@ export const polioRouteConfigs: Record<string, RouteConfig> = {
...paginationPathParams,
'countries',
'campaignType',
'campaignCategory',
'file_type',
'country_block',
'campaignStatus',
'vaccine_name',
],
},
lqasCountry: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export const VaccineRepository: FunctionComponent = () => {
)}
<VaccineRepositoryFilters
params={params}
isEmbedded={isEmbedded}
redirectUrl={redirectUrl}
/>
<TableWithDeepLink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ import MESSAGES from '../../../constants/messages';
import { useGetCountries } from '../../../hooks/useGetCountries';

import { appId } from '../../../constants/app';
import { useCampaignCategoryOptions } from '../../Campaigns/hooks/useCampaignCategoryOptions';
import { useGetCampaignStatus } from './hooks/useGetCampaignStatus';
import { useGetFileTypes } from './hooks/useGetFileTypes';
import { VaccineRepositoryParams } from './types';
import { defaultVaccineOptions } from '../SupplyChain/constants';

type Props = {
params: VaccineRepositoryParams;
disableDates?: boolean;
isEmbedded?: boolean;
redirectUrl: string;
};

export const VaccineRepositoryFilters: FunctionComponent<Props> = ({
params,
isEmbedded = false,
redirectUrl,
}) => {
const redirectToReplace = useRedirectToReplace();
Expand All @@ -38,35 +35,29 @@ export const VaccineRepositoryFilters: FunctionComponent<Props> = ({
const [fileType, setFileType] = useState(
params.file_type || 'VRF,PRE_ALERT,FORM_A',
);
const [campaignStatus, setCampaignStatus] = useState(params.campaignStatus);
const [vaccineName, setVaccineName] = useState(params.vaccine_name);
const [countryBlocks, setCountryBlocks] = useState(params.country_block);
const [campaignCategory, setCampaignCategory] = useState(
isEmbedded
? (params.campaignCategory ?? 'all')
: params.campaignCategory,
);

const handleSearch = useCallback(() => {
if (filtersUpdated) {
setFiltersUpdated(false);
const urlParams = {
...params,
countries,
page: undefined,
campaignCategory,
country_block: countryBlocks,
file_type: fileType,
campaignStatus,
vaccine_name: vaccineName,
};
redirectToReplace(redirectUrl, urlParams);
}
}, [
filtersUpdated,
params,
countries,
campaignCategory,
countryBlocks,
vaccineName,
fileType,
campaignStatus,
redirectToReplace,
redirectUrl,
]);
Expand All @@ -77,94 +68,74 @@ export const VaccineRepositoryFilters: FunctionComponent<Props> = ({

const countriesList = (data && data.orgUnits) || [];

const campaignCategoryOptions = useCampaignCategoryOptions();

const fileTypes = useGetFileTypes();
const campaignStatusOptions = useGetCampaignStatus();
useEffect(() => {
setFiltersUpdated(true);
}, [countries, campaignCategory, countryBlocks, fileType, campaignStatus]);
}, [countries, countryBlocks, fileType, vaccineName]);

useEffect(() => {
setFiltersUpdated(false);
}, []);

return (
<>
<Grid container spacing={2}>
<Grid item xs={12} md={3}>
<InputComponent
keyValue="campaignStatus"
clearable
onChange={(key, value) => {
setCampaignStatus(value);
}}
value={campaignStatus}
type="select"
options={campaignStatusOptions}
label={MESSAGES.campaignStatus}
/>
<InputComponent
loading={isFetchingGroupedOrgUnits}
keyValue="country_block"
multi
clearable
onChange={(key, value) => {
setCountryBlocks(value);
}}
value={countryBlocks}
type="select"
options={groupedOrgUnits}
label={MESSAGES.countryBlock}
/>
</Grid>
<Grid item xs={12} md={3}>
<InputComponent
loading={isFetchingCountries}
keyValue="countries"
multi
clearable
onChange={(_, value) => {
setCountries(value);
}}
value={countries}
type="select"
options={countriesList.map(c => ({
label: c.name,
value: c.id,
}))}
label={MESSAGES.country}
/>
</Grid>
<Grid item xs={12} md={3}>
<InputComponent
keyValue="campaignCategory"
clearable
onChange={(_key, value) => {
setCampaignCategory(value);
}}
value={campaignCategory}
type="select"
options={campaignCategoryOptions}
label={MESSAGES.campaignCategory}
/>
</Grid>
<Grid item xs={12} md={3}>
<InputComponent
keyValue="file_type"
clearable
onChange={(_key, value) => {
setFileType(value);
}}
value={fileType}
type="select"
options={fileTypes}
label={MESSAGES.fileType}
/>
</Grid>
<Grid container spacing={2}>
<Grid item xs={12} md={3}>
<InputComponent
loading={isFetchingCountries}
keyValue="countries"
multi
clearable
onChange={(_, value) => {
setCountries(value);
}}
value={countries}
type="select"
options={countriesList.map(c => ({
label: c.name,
value: c.id,
}))}
label={MESSAGES.country}
/>
<InputComponent
keyValue="vaccine_name"
clearable
onChange={(key, value) => {
setVaccineName(value);
}}
value={vaccineName}
type="select"
options={defaultVaccineOptions}
label={MESSAGES.vaccine}
/>
</Grid>

<Grid container item xs={12} justifyContent="flex-end">
<Grid item xs={12} md={3}>
<InputComponent
loading={isFetchingGroupedOrgUnits}
keyValue="country_block"
multi
clearable
onChange={(key, value) => {
setCountryBlocks(value);
}}
value={countryBlocks}
type="select"
options={groupedOrgUnits}
label={MESSAGES.countryBlock}
/>
<InputComponent
keyValue="file_type"
clearable
onChange={(_key, value) => {
setFileType(value);
}}
value={fileType}
type="select"
options={fileTypes}
label={MESSAGES.fileType}
/>
</Grid>
<Grid container item xs={12} md={6} justifyContent="flex-end">
<Box mt={2}>
<Button
disabled={!filtersUpdated}
Expand All @@ -179,6 +150,6 @@ export const VaccineRepositoryFilters: FunctionComponent<Props> = ({
</Button>
</Box>
</Grid>
</>
</Grid>
);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { PaginationParams } from '../../../../../../../hat/assets/js/apps/Iaso/types/general';
import { CampaignCategory } from '../../Campaigns/hooks/api/useGetCampaigns';
import { Vaccine } from '../../../constants/types';

export type VaccineRepositoryParams = PaginationParams & {
countries?: string;
campaignType?: string;
campaignCategory?: CampaignCategory;
country_block?: string;
campaignGroups?: string;
file_type?: string;
campaignStatus?: string;
vaccine_name?: Vaccine;
};

export type DocumentData = {
Expand Down
3 changes: 2 additions & 1 deletion plugins/polio/tests/api/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def create_campaign(
district_ou_type,
country_name="Groland",
district_name="Groville",
vaccine=pm.VACCINES[0][0],
):
country = m.OrgUnit.objects.create(
org_unit_type=country_ou_type,
Expand All @@ -36,7 +37,7 @@ def create_campaign(
)
scope_group = m.Group.objects.create(name="campaign_scope", source_version=source_version)
scope_group.org_units.set([district]) # FIXME: we should actually have children org units
scope = pm.CampaignScope.objects.create(campaign=campaign, vaccine=pm.VACCINES[0][0], group=scope_group)
scope = pm.CampaignScope.objects.create(campaign=campaign, vaccine=vaccine, group=scope_group)

round_1 = pm.Round.objects.create(
campaign=campaign,
Expand Down
Loading

0 comments on commit 4263485

Please sign in to comment.