Skip to content

Commit

Permalink
fix: attempt to edit existing tracker program instance
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Dec 14, 2023
1 parent 47fb4eb commit 7756c56
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
84 changes: 54 additions & 30 deletions src/data/repositories/SurveyFormD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export class SurveyD2Repository implements SurveyRepository {
const sections: QuestionnaireSection[] = programStageSections
? programStageSections.map(section => {
const { questions, sectionAddQuestion } = this.mapProgramDataElementToQuestions(
this.isTrackerProgram(program.id),
section.dataElements,
dataElements,
options,
Expand All @@ -208,6 +209,7 @@ export class SurveyD2Repository implements SurveyRepository {
title: "Survey Info",
code: "default",
questions: this.mapProgramDataElementToQuestions(
this.isTrackerProgram(program.id),
programDataElements,
dataElements,
options,
Expand Down Expand Up @@ -257,6 +259,9 @@ export class SurveyD2Repository implements SurveyRepository {
code: stage.id,
sections: processedSections,
isVisible: true,
instanceId: trackedEntity?.enrollments
?.at(0)
?.events.find(e => e.programStage === stage.id)?.event,
};
} else {
// no need for grouping and hiding logic
Expand All @@ -265,6 +270,9 @@ export class SurveyD2Repository implements SurveyRepository {
code: stage.id,
sections: currentProgramStageSections,
isVisible: true,
instanceId: trackedEntity?.enrollments
?.at(0)
?.events.find(e => e.programStage === stage.id)?.event,
};
}
})
Expand Down Expand Up @@ -292,6 +300,11 @@ export class SurveyD2Repository implements SurveyRepository {
isMandatory: false,
rules: [],
stages: stages.sort((a, b) => a.title.localeCompare(b.title, "en", { numeric: true })),
subLevelDetails: {
enrollmentId: trackedEntity
? trackedEntity.enrollments?.at(0)?.enrollment ?? ""
: "",
},
};

if (trackedEntityAttributes) {
Expand All @@ -314,6 +327,7 @@ export class SurveyD2Repository implements SurveyRepository {
}

private mapProgramDataElementToQuestions(
isTrackerProgram: boolean,
sectionDataElements: { id: string }[],
dataElements: ProgramDataElement[],
options: Option[],
Expand All @@ -323,35 +337,45 @@ export class SurveyD2Repository implements SurveyRepository {
let sectionAddQuestion = "";
const questions: Question[] = _(
sectionDataElements.map(dataElement => {
const curDataEleemnt = dataElements.filter(de => de.id === dataElement.id);

if (curDataEleemnt[0]) {
const dataElement = curDataEleemnt[0];

const eventDataValue = event
? event.dataValues.find(dv => dv.dataElement === dataElement.id)
: undefined;
const curDataElement = dataElements.find(de => de.id === dataElement.id);

if (curDataElement) {
let dataValue: string | undefined;
if (isTrackerProgram) {
const dataValues = trackedEntity?.enrollments?.flatMap(enrollment => {
return _(
enrollment.events.map(e => {
return e.dataValues.find(
dv => dv.dataElement === curDataElement.id
);
})
)
.compact()
.value();
});

const trackedEntityDataValue = trackedEntity?.enrollments?.flatMap(
enrollment => {
return enrollment.events.flatMap(e => {
return e.dataValues.find(dv => dv.dataElement === dataElement.id);
});
if (dataValues && dataValues.length > 1) {
console.debug(
"ERROR : There should never be more than one instance of a dataelement in an event"
);
}
);

dataValue = dataValues?.at(0)?.value;
} else {
dataValue = event
? event.dataValues.find(dv => dv.dataElement === curDataElement.id)
?.value
: undefined;
}

const currentQuestion = this.getQuestion(
dataElement.valueType,
dataElement.id,
dataElement.code,
dataElement.formName,
curDataElement.valueType,
curDataElement.id,
curDataElement.code,
curDataElement.formName,
options,
dataElement.optionSet,
eventDataValue
? eventDataValue.value
: trackedEntityDataValue
? trackedEntityDataValue[0]?.value
: ""
curDataElement.optionSet,
dataValue ?? ""
);

///Disable Id fields which are auto generated.
Expand Down Expand Up @@ -670,7 +694,7 @@ export class SurveyD2Repository implements SurveyRepository {
questionnaire: Questionnaire,
orgUnitId: string,
programId: Id,
_eventId: string | undefined = undefined
teiId: string | undefined = undefined
): FutureData<{ trackedEntities: TrackedEntity[] }> {
const eventsByStage: D2TrackerEvent[] = questionnaire.stages.map(stage => {
const dataValuesByStage: { dataElement: string; value: string }[] =
Expand All @@ -692,7 +716,7 @@ export class SurveyD2Repository implements SurveyRepository {

return {
program: programId,
event: "",
event: stage.instanceId ?? "",
programStage: stage.code,
orgUnit: orgUnitId,
dataValues: dataValuesByStage,
Expand Down Expand Up @@ -726,7 +750,7 @@ export class SurveyD2Repository implements SurveyRepository {
{
orgUnit: orgUnitId,
program: programId,
enrollment: "",
enrollment: questionnaire.subLevelDetails?.enrollmentId ?? "",
trackedEntityType: this.getTrackedEntityAttributeType(programId),
notes: [],
relationships: [],
Expand All @@ -748,7 +772,7 @@ export class SurveyD2Repository implements SurveyRepository {

const entity: TrackedEntity = {
orgUnit: orgUnitId,
trackedEntity: "",
trackedEntity: teiId ?? "",
trackedEntityType: this.getTrackedEntityAttributeType(programId),
enrollments: enrollments,
};
Expand Down Expand Up @@ -805,7 +829,7 @@ export class SurveyD2Repository implements SurveyRepository {
: undefined;
return apiToFuture(
this.api.tracker.trackedEntities.get({
fields: { $all: true },
fields: { attributes: true, enrollments: true, trackedEntity: true, orgUnit: true },
program: programId,
orgUnit: orgUnitId,
ouMode: ouMode,
Expand Down Expand Up @@ -998,8 +1022,8 @@ export class SurveyD2Repository implements SurveyRepository {
): FutureData<TrackedEntity | void> {
return apiToFuture(
this.api.tracker.trackedEntities.get({
orgUnit: orgUnitId,
fields: { attributes: true, enrollments: true, trackedEntity: true, orgUnit: true },
orgUnit: orgUnitId,
program: programId,
trackedEntity: trackedEntityId,
ouMode: "DESCENDANTS",
Expand Down
4 changes: 4 additions & 0 deletions src/domain/entities/Questionnaire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export interface QuestionnaireSelector {
export interface Questionnaire extends QuestionnaireBase {
stages: QuestionnaireStage[];
entity?: QuestionnaireEntity; //Equivalant to tracked entity instance of tracker program
subLevelDetails?: {
enrollmentId: Id;
};
}

export interface QuestionnaireEntity {
Expand All @@ -38,6 +41,7 @@ export interface QuestionnaireStage {
sections: QuestionnaireSection[];
isVisible: boolean;
showNextStage?: boolean;
instanceId?: Id; //Corresponds to DHIS eventId
}
export interface QuestionnaireSection {
title: string;
Expand Down

0 comments on commit 7756c56

Please sign in to comment.