Skip to content

Commit

Permalink
Merge pull request #1550 from dhis2/TECH-542-v36
Browse files Browse the repository at this point in the history
fix: TECH-542 Switch to new endpoint for org units
  • Loading branch information
JoakimSM authored Mar 10, 2021
2 parents 1e363cf + dba014e commit 7448241
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "capture-app",
"homepage": ".",
"version": "35.0.0",
"cacheVersion": "2",
"version": "36.0.0",
"cacheVersion": "1",
"private": false,
"dependencies": {
"@dhis2/app-runtime": "^2.6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
type TrackedEntityType,
type Category,
} from '../../../../metaData';
import { getUserStorageController } from '../../../../storageControllers';
import { userStores } from '../../../../storageControllers/stores';

import getProgramIconAsync from './getProgramIcon';
import { SearchGroupFactory } from '../../../common/factory';
Expand Down Expand Up @@ -137,7 +139,6 @@ class ProgramFactory {
o.access = cachedProgram.access;
o.name = cachedProgram.displayName;
o.shortName = cachedProgram.displayShortName;
o.organisationUnits = cachedProgram.organisationUnits;
o.categoryCombination = this._buildCategoryCombination(cachedProgram.categoryCombo);
});
const d2Stage = cachedProgram.programStages && cachedProgram.programStages[0];
Expand All @@ -152,7 +153,6 @@ class ProgramFactory {
o.access = cachedProgram.access;
o.name = cachedProgram.displayName;
o.shortName = cachedProgram.displayShortName;
o.organisationUnits = cachedProgram.organisationUnits;
// $FlowFixMe
o.trackedEntityType = this.trackedEntityTypeCollection.get(cachedProgram.trackedEntityTypeId);
});
Expand Down Expand Up @@ -181,6 +181,7 @@ class ProgramFactory {
}
// $FlowFixMe
program.icon = await ProgramFactory._buildProgramIcon(cachedProgram.style);
program.organisationUnits = (await getUserStorageController().get(userStores.ORGANISATION_UNITS_BY_PROGRAM, program.id)).organisationUnits;

return program;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type StoreNames = {
RELATIONSHIP_TYPES: string,
TRACKED_ENTITY_TYPES: string,
PROGRAMS: string,
ORGANISATION_UNITS_BY_PROGRAM: string,
PROGRAM_RULES: string,
PROGRAM_RULES_VARIABLES: string,
PROGRAM_INDICATORS: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getContext } from '../context';
import { queryProgramsOutline } from './queries';
import {
storePrograms,
storeOrganisationUnitsByProgram,
} from './quickStoreOperations';
import { loadRulesCentricMetadata } from './loadRulesCentricMetadata';

Expand Down Expand Up @@ -37,6 +38,7 @@ const removeUnavailablePrograms = async (apiPrograms, cachedPrograms) => {
if (unavailableProgramIds.length > 0) {
const { storageController, storeNames } = getContext();
await storageController.remove(storeNames.PROGRAMS, unavailableProgramIds);
await storageController.remove(storeNames.ORGANISATION_UNITS_BY_PROGRAM, unavailableProgramIds);
}
};

Expand Down Expand Up @@ -66,6 +68,7 @@ const getStaleProgramIds = (apiPrograms, cachedPrograms) => {
* The side effects data is used later when determining what other metadata to load.
*/
const loadProgramBatch = async (programIds) => {
await storeOrganisationUnitsByProgram(programIds);
const { convertedData: programs = [] } = await storePrograms(programIds);
await loadRulesCentricMetadata(programIds);
return programs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const queryProgramsOutline = async (): Promise<Array<Object>> => {
const specification = {
resource: 'programs',
params: {
restrictToCaptureScope: true,
fields: 'id,version,programTrackedEntityAttributes[trackedEntityAttribute[id,optionSet[id,version]]],' +
'programStages[id,programStageDataElements[dataElement[id,optionSet[id,version]]]]',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
export { storePrograms } from './storePrograms';
export { storeOrganisationUnitsByProgram } from './storeOrganisationUnitsByProgram';
export { storeProgramRules } from './storeProgramRules';
export { storeProgramRulesVariables } from './storeProgramRulesVariables';
export { storeProgramIndicators } from './storeProgramIndicators';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @flow
import { quickStore } from '../../IOUtils';
import { getContext } from '../../context';
import type { ApiOrganisationUnitsByProgram } from './types';

const convert = (response: ApiOrganisationUnitsByProgram) =>
Object
.keys(response)
.map(id => ({
id,
organisationUnits: response[id].reduce((acc, orgUnitId) => {
acc[orgUnitId] = true;
return acc;
}, {}),
}));

export const storeOrganisationUnitsByProgram = (programIds: Array<string>) => {
const query = {
resource: 'programs/orgUnits',
params: {
programs: programIds.join(','),
},
};

return quickStore({
query,
storeName: getContext().storeNames.ORGANISATION_UNITS_BY_PROGRAM,
convertQueryResponse: convert,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ const convert = (() => {
return programStages;
};

const getOrganisationUnits = apiOrganisationUnits =>
(apiOrganisationUnits || [])
.reduce((accOrganisationUnits, organisationUnit) => {
accOrganisationUnits[organisationUnit.id] = organisationUnit;
return accOrganisationUnits;
}, {});

const getProgramTrackedEntityAttribute = programAttribute => ({
...programAttribute,
trackedEntityAttribute: undefined,
Expand All @@ -76,7 +69,6 @@ const convert = (() => {
trackedEntityType: undefined,
trackedEntityTypeId: apiProgram.trackedEntityType && apiProgram.trackedEntityType.id,
programStages: getProgramStages(apiProgram.programStages),
organisationUnits: getOrganisationUnits(apiProgram.organisationUnits),
programTrackedEntityAttributes:
getProgramTrackedEntityAttributes(apiProgram.programTrackedEntityAttributes),
}));
Expand All @@ -90,7 +82,6 @@ const fieldsParam = 'id,version,displayName,displayShortName,description,program
'access[*],' +
'trackedEntityType[id],' +
'categoryCombo[id,displayName,isDefault,categories[id,displayName]],' +
'organisationUnits[id,displayName],' +
'userRoles[id,displayName],' +
'programStages[id,access,autoGenerateEvent,openAfterEnrollment,generatedByEnrollmentDate,reportDateToUse,standardInterval,displayName,description,executionDateLabel,formType,featureType,validationStrategy,enableUserAssignment,dataEntryForm[id,htmlCode],' +
'programStageSections[id,displayName,sortOrder,dataElements[id]],programStageDataElements[compulsory,displayInReports,renderOptionsAsRadio,allowFutureDate,renderType[*],' +
Expand All @@ -101,7 +92,6 @@ export const storePrograms = (programIds: Array<string>) => {
const query = {
resource: 'programs',
params: {
restrictToCaptureScope: true,
fields: fieldsParam,
filter: `id:in:[${programIds.join(',')}]`,
pageSize: programIds.length,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow

export type ApiOrganisationUnitsByProgram = {
[programId: string]: Array<string>,
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// @flow
export type { apiProgramsResponse } from './apiPrograms.types';
export * from './apiPrograms.types';
export * from './apiOrganisationUnitsByProgram.types';
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
RELATIONSHIP_TYPES: 'relationshipTypes',
TRACKED_ENTITY_TYPES: 'trackedEntityTypes',
PROGRAMS: 'programs',
ORGANISATION_UNITS_BY_PROGRAM: 'organisationUnitsByProgram',
PROGRAM_RULES: 'programRules',
PROGRAM_RULES_VARIABLES: 'programRulesVariables',
PROGRAM_INDICATORS: 'programIndicators',
Expand Down

0 comments on commit 7448241

Please sign in to comment.