Skip to content

Commit

Permalink
Merge pull request #500 from onaio/fix-parentJurisdictionId-filter
Browse files Browse the repository at this point in the history
Fix parent jurisdiction id filter
  • Loading branch information
moshthepitt authored Oct 7, 2019
2 parents c26b2c3 + 5ee03f6 commit 46d2148
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ const mapStateToProps = (state: Partial<Store>, ownProps: any) => {
jurisdiction,
plan,
plansArray: getPlansArray(state, InterventionType.FI, [PlanStatus.ACTIVE, PlanStatus.COMPLETE]),
plansByFocusArea,
plansIdArray: getPlansIdArray(
state,
InterventionType.FI,
Expand Down
41 changes: 22 additions & 19 deletions src/store/ducks/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,64 +425,67 @@ export const fetchPlanRecords = (planList: PlanRecordResponse[] = []): FetchPlan
/** getPlansById - get plansById by intervention type
* @param {Partial<Store>} state - the redux store
* @param {InterventionType} intervention - the intervention type
* @param {string[]} status - the plan status
* @param {string[]} statusList - the plan statuses
* @param {string} reason - the plan reason
*/
export function getPlansById(
state: Partial<Store>,
intervention: InterventionType = InterventionType.FI,
status: string[] = [PlanStatus.ACTIVE],
statusList: string[] = [PlanStatus.ACTIVE],
reason: string | null = null
): { [key: string]: Plan } {
const plansById = (state as any)[reducerName].plansById;
return pickBy(
plansById,
(plan: Plan) =>
plan.plan_intervention_type === intervention &&
(status.length ? status.includes(plan.plan_status) : true) &&
(statusList.length ? statusList.includes(plan.plan_status) : true) &&
(reason ? plan.plan_fi_reason === reason : true)
);
}

/** getPlansArray - get an array of Plans by intervention type
* @param {Partial<Store>} state - the redux store
* @param {InterventionType} intervention - the intervention type
* @param {string[]} status - the plan status
* @param {string[]} statusList - a list of the desired the plan statuses
* @param {string} reason - the plan reason
* @param {string} parentJurisdictionId - The jurisdiction_parent_id of the plan
* @param {string[]} jurisdictions - jurisdiction IDs list to find plan's jurisdiction_id within
*/
export function getPlansArray(
state: Partial<Store>,
intervention: InterventionType = InterventionType.FI,
status: string[] = [],
statusList: string[] = [],
reason: string | null = null,
jurisdictions: string[] = [],
parentJurisdictionId: string | null = null
): Plan[] {
return values((state as any)[reducerName].plansById).filter(
(plan: Plan) =>
plan.plan_intervention_type === intervention &&
(status.length ? status.includes(plan.plan_status) : true) &&
(statusList.length ? statusList.includes(plan.plan_status) : true) &&
(reason ? plan.plan_fi_reason === reason : true) &&
(jurisdictions.length ? jurisdictions.includes(plan.jurisdiction_id) : true) &&
(parentJurisdictionId ? plan.jurisdiction_path.includes(parentJurisdictionId) : true)
(parentJurisdictionId && !plan.jurisdiction_path ? false : true) &&
(parentJurisdictionId && plan.jurisdiction_path
? plan.jurisdiction_path.includes(parentJurisdictionId)
: true)
);
}

/** getPlansIdArray - get an array of Plan ids by intervention type
* @param {Partial<Store>} state - the redux store
* @param {InterventionType} intervention - the intervention type
* @param {string[]} status - the plan status
* @param {string[]} statusList - the plan statuses
* @param {string} reason - the plan reason
*/
export function getPlansIdArray(
state: Partial<Store>,
intervention: InterventionType = InterventionType.FI,
status: string[] = [PlanStatus.ACTIVE],
statusList: string[] = [PlanStatus.ACTIVE],
reason: string | null = null
): string[] {
return keys(getPlansById(state, intervention, status, reason));
return keys(getPlansById(state, intervention, statusList, reason));
}

/** getPlanById - get one Plan by id
Expand All @@ -496,58 +499,58 @@ export function getPlanById(state: Partial<Store>, id: string): Plan | null {
/** getPlanRecordsById - get planRecordsById by intervention type
* @param {Partial<Store>} state - the redux store
* @param {InterventionType} intervention - the intervention type
* @param {string[]} status - the plan status
* @param {string[]} statusList - the plan statuses
* @param {string} reason - the plan reason
*/
export function getPlanRecordsById(
state: Partial<Store>,
intervention: InterventionType = InterventionType.FI,
status: string[] = [PlanStatus.ACTIVE],
statusList: string[] = [PlanStatus.ACTIVE],
reason: string | null = null
): { [key: string]: PlanRecord } {
const planRecordsById = (state as any)[reducerName].planRecordsById;
return pickBy(
planRecordsById,
(plan: PlanRecord) =>
plan.plan_intervention_type === intervention &&
(status.length ? status.includes(plan.plan_status) : true) &&
(statusList.length ? statusList.includes(plan.plan_status) : true) &&
(reason ? plan.plan_fi_reason === reason : true)
);
}

/** getPlanRecordsArray - get an array of PlanRecords by intervention type
* @param {Partial<Store>} state - the redux store
* @param {InterventionType} intervention - the intervention type
* @param {string[]} status - the plan status
* @param {string[]} status - the plan statuses
* @param {string} reason - the plan reason
*/
export function getPlanRecordsArray(
state: Partial<Store>,
intervention: InterventionType = InterventionType.FI,
status: string[] = [PlanStatus.ACTIVE],
statusList: string[] = [PlanStatus.ACTIVE],
reason: string | null = null
): PlanRecord[] {
return values((state as any)[reducerName].planRecordsById).filter(
(plan: PlanRecord) =>
plan.plan_intervention_type === intervention &&
(status.length ? status.includes(plan.plan_status) : true) &&
(statusList.length ? statusList.includes(plan.plan_status) : true) &&
(reason ? plan.plan_fi_reason === reason : true)
);
}

/** getPlanRecordsIdArray - get an array of PlanRecord ids
* @param {Partial<Store>} state - the redux store
* @param {InterventionType} intervention - the intervention type
* @param {string[]} status - the plan status
* @param {string[]} status - the plan statuses
* @param {string} reason - the plan reason
*/
export function getPlanRecordsIdArray(
state: Partial<Store>,
intervention: InterventionType = InterventionType.FI,
status: string[] = [PlanStatus.ACTIVE],
statusList: string[] = [PlanStatus.ACTIVE],
reason: string | null = null
): string[] {
return keys(getPlanRecordsById(state, intervention, status, reason));
return keys(getPlanRecordsById(state, intervention, statusList, reason));
}

/** getPlanRecordById - get one PlanRecord by id
Expand Down
8 changes: 8 additions & 0 deletions src/store/ducks/tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ export const plan6 = {
jurisdiction_path: 'null',
};

export const plan7 = {
...plan2,
id: 'plan-id-7',
jurisdiction_name_path: 'null',
jurisdiction_parent_id: 'null',
jurisdiction_path: 'null',
};

export const plan3 = {
id: '1502e539',
jurisdiction_depth: 4,
Expand Down
27 changes: 27 additions & 0 deletions src/store/ducks/tests/plans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ describe('reducers/plans', () => {
expect(plan3FromStore).toEqual(fixtures.plan3);
}
});

it('resets plansById records', () => {
store.dispatch(removePlansAction);
let numberOfPlansInStore = getPlansArray(store.getState(), undefined, []).length;
Expand Down Expand Up @@ -242,4 +243,30 @@ describe('reducers/plans', () => {
numberOfPlansInStore = getPlansArray(store.getState(), undefined, []).length;
expect(numberOfPlansInStore).toEqual(2);
});

it('getPlansArray works when plan.jurisdiction_path is null', () => {
store.dispatch(removePlansAction);
store.dispatch(fetchPlans([fixtures.plan7] as any));
expect([]).toEqual(
getPlansArray(
store.getState(),
InterventionType.IRS,
[PlanStatus.ACTIVE],
'Case-triggered',
[],
'some-jurisdiction-id'
)
);

expect([getPlanById(store.getState(), 'plan-id-7')]).toEqual(
getPlansArray(
store.getState(),
InterventionType.IRS,
[PlanStatus.ACTIVE],
'Case-triggered',
[],
null
)
);
});
});

0 comments on commit 46d2148

Please sign in to comment.