Skip to content

Commit

Permalink
Merge pull request #36 from EyeSeeTea/fix/sort-order
Browse files Browse the repository at this point in the history
Fix sort order of data elements and tracked entity attrubutes
  • Loading branch information
MiquelAdell authored Apr 25, 2024
2 parents b4210d1 + 2e83ad2 commit 200ff35
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/data/entities/D2Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export interface TrackedEntityAttibute {
optionSet?: { id: string };
value?: string;
}

export interface ProgramTrackedEntityAttibute {
trackedEntityAttribute: { id: string };
sortOrder: number;
}
export interface ProgramMetadata {
programs: Program[];
programStageDataElements: ProgramStageDataElement[];
Expand All @@ -81,6 +86,7 @@ export interface ProgramMetadata {
optionSets: OptionSet[];
options: Option[];
trackedEntityAttributes?: TrackedEntityAttibute[];
programTrackedEntityAttributes: ProgramTrackedEntityAttibute[];
programStages: ProgramStage[];
programRules: D2ProgramRule[];
programRuleVariables: D2ProgramRuleVariable[];
Expand Down
40 changes: 22 additions & 18 deletions src/data/repositories/SurveyFormD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
PREVALENCE_SURVEY_NAME_DATAELEMENT_ID,
PPS_COUNTRY_QUESTIONNAIRE_ID,
} from "../entities/D2Survey";
import { ProgramDataElement, ProgramMetadata } from "../entities/D2Program";
import { ProgramMetadata } from "../entities/D2Program";
import {
mapProgramToQuestionnaire,
mapQuestionnaireToEvent,
Expand All @@ -48,24 +48,28 @@ export class SurveyD2Repository implements SurveyRepository {
return apiToFuture(
this.api.request<ProgramMetadata>({
method: "get",
url: `/programs/${programId}/metadata.json?fields=programs,dataElements,programStageDataElements,programStageSections,trackedEntityAttributes,programStages,programRules,programRuleVariables,programRuleActions`,
url: `/programs/${programId}/metadata.json?fields=programs,dataElements,programStageDataElements.dataElement,programStageSections,programTrackedEntityAttributes,trackedEntityAttributes,programStages,programRules,programRuleVariables,programRuleActions`,
})
).flatMap(resp => {
if (resp.programs[0]) {
const programDataElements = resp.programStageDataElements.map(
psde => psde.dataElement
);

const dataElementsWithSortOrder: ProgramDataElement[] = resp.dataElements.map(
de => {
return {
...de,
sortOrder: resp.programStageDataElements.find(
psde => psde.dataElement.id === de.id
)?.sortOrder,
};
}
);
const sortedTrackedentityAttr = resp.programTrackedEntityAttributes
? _(
_(resp.programTrackedEntityAttributes)
.sortBy(te => te.sortOrder)
.value()
.map(pste =>
resp.trackedEntityAttributes?.find(
te => te.id === pste.trackedEntityAttribute.id
)
)
)
.compact()
.value()
: resp.trackedEntityAttributes;

//If event specified,populate the form
if (eventId) {
Expand All @@ -80,11 +84,11 @@ export class SurveyD2Repository implements SurveyRepository {
undefined,
trackedEntity,
programDataElements,
dataElementsWithSortOrder,
resp.dataElements,
resp.options,
resp.programStages,
resp.programStageSections,
resp.trackedEntityAttributes,
sortedTrackedentityAttr,
resp.programRules,
resp.programRuleVariables,
resp.programRuleActions
Expand All @@ -106,11 +110,11 @@ export class SurveyD2Repository implements SurveyRepository {
event,
undefined,
programDataElements,
dataElementsWithSortOrder,
resp.dataElements,
resp.options,
resp.programStages,
resp.programStageSections,
resp.trackedEntityAttributes,
sortedTrackedentityAttr,
resp.programRules,
resp.programRuleVariables,
resp.programRuleActions
Expand All @@ -130,11 +134,11 @@ export class SurveyD2Repository implements SurveyRepository {
undefined,
undefined,
programDataElements,
dataElementsWithSortOrder,
resp.dataElements,
resp.options,
resp.programStages,
resp.programStageSections,
resp.trackedEntityAttributes,
sortedTrackedentityAttr,
resp.programRules,
resp.programRuleVariables,
resp.programRuleActions
Expand Down
3 changes: 0 additions & 3 deletions src/data/utils/questionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ export const mapProgramDataElementToQuestions = (
})
)
.compact()
.sortBy(q => q.sortOrder)
.value();

return questions;
Expand Down Expand Up @@ -320,7 +319,6 @@ export const mapRepeatedStageEventToQuestions = (
})
)
.compact()
.sortBy(q => q.sortOrder)
.value();

return questions;
Expand Down Expand Up @@ -363,7 +361,6 @@ export const mapTrackedAttributesToQuestions = (
})
)
.compact()
.sortBy(q => q.sortOrder)
.value();

return questions;
Expand Down

0 comments on commit 200ff35

Please sign in to comment.