Skip to content

Commit

Permalink
fix: avoid invalid dates in getQuestion. Catch errors from mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatiasArriola committed Dec 20, 2024
1 parent d309583 commit 04e4510
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
7 changes: 5 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-05-23T16:02:03.207Z\n"
"PO-Revision-Date: 2024-05-23T16:02:03.207Z\n"
"POT-Creation-Date: 2024-12-20T16:08:06.715Z\n"
"PO-Revision-Date: 2024-12-20T16:08:06.715Z\n"

msgid "There was an error processing the form"
msgstr ""

msgid "Facilities"
msgstr ""
Expand Down
5 changes: 4 additions & 1 deletion i18n/es.po
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
msgid ""
msgstr ""
"Project-Id-Version: i18next-conv\n"
"POT-Creation-Date: 2024-05-22T13:24:01.336Z\n"
"POT-Creation-Date: 2024-12-20T16:08:06.715Z\n"
"PO-Revision-Date: 2018-10-25T09:02:35.143Z\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"

msgid "There was an error processing the form"
msgstr ""

msgid "Facilities"
msgstr ""

Expand Down
17 changes: 15 additions & 2 deletions src/data/utils/questionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
import _ from "../../domain/entities/generic/Collection";
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";
import { D2TrackerTrackedEntity as TrackedEntity } from "@eyeseetea/d2-api/api/trackerTrackedEntities";
import { isValidDate } from "../../utils/dates";

const SPECIES_QUESTION_FORNAME = "Specify the specie";
const ANTIBIOTIC_QUESTION_FORNAME = "Specify the antibiotic";
Expand Down Expand Up @@ -173,7 +174,7 @@ export const getQuestion = (
const dateQ: DateQuestion = {
...base,
type: "date",
value: dataValue ? new Date(dataValue) : undefined,
value: parseQuestionDate(dataValue, base),
};
return dateQ;
}
Expand All @@ -182,13 +183,25 @@ export const getQuestion = (
const dateQ: DateTimeQuestion = {
...base,
type: "datetime",
value: dataValue ? new Date(dataValue) : undefined,
value: parseQuestionDate(dataValue, base),
};
return dateQ;
}
}
};

const parseQuestionDate = (
dateStr: string | undefined,
question: QuestionBase
): Date | undefined => {
if (!dateStr) return undefined;
const result = isValidDate(new Date(dateStr)) ? new Date(dateStr) : undefined;
if (dateStr && !result) {
console.debug(`Invalid date value: ${dateStr}`, question);
}
return result;
};

export const mapQuestionsToDataValues = (questions: Question[]): DataValue[] => {
const dataValues = _(
questions.map(question => {
Expand Down
33 changes: 26 additions & 7 deletions src/data/utils/surveyFormMappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
} from "@eyeseetea/d2-api/api/trackerEnrollments";
import { DataValue } from "@eyeseetea/d2-api";
import { generateUid } from "../../utils/uid";
import i18n from "../../utils/i18n";

const AntibioticTreatmentHospitalEpisodeSectionName =
`Antibiotic treatments during hospital episode`.toLowerCase();
Expand Down Expand Up @@ -294,7 +295,12 @@ export const mapQuestionnaireToEvent = (
stages.sections.flatMap(section => section.questions)
);

const dataValues = mapQuestionsToDataValues(questions);
let dataValues: DataValue[] = [];
try {
dataValues = mapQuestionsToDataValues(questions);
} catch (error) {
return Future.error(new Error(i18n.t("There was an error processing the form")));
}

if (eventId) {
return getEventProgramById(eventId, api).flatMap(event => {
Expand Down Expand Up @@ -323,13 +329,12 @@ export const mapQuestionnaireToEvent = (
}
};

export const mapQuestionnaireToTrackedEntities = (
const mapQuestionnaireToEventsByStage = (
questionnaire: Questionnaire,
orgUnitId: string,
programId: Id,
teiId: string | undefined = undefined
): FutureData<{ trackedEntities: TrackedEntity[] }> => {
const eventsByStage: D2TrackerEvent[] = questionnaire.stages.map(stage => {
programId: Id
): D2TrackerEvent[] => {
return questionnaire.stages.map(stage => {
const dataValuesByStage = stage.sections.flatMap(section => {
return mapQuestionsToDataValues(section.questions);
});
Expand All @@ -339,11 +344,25 @@ export const mapQuestionnaireToTrackedEntities = (
event: stage.instanceId ?? "",
programStage: stage.code,
orgUnit: orgUnitId,
dataValues: dataValuesByStage as DataValue[],
dataValues: dataValuesByStage,
occurredAt: new Date().getTime().toString(),
status: "ACTIVE",
};
});
};

export const mapQuestionnaireToTrackedEntities = (
questionnaire: Questionnaire,
orgUnitId: string,
programId: Id,
teiId: string | undefined = undefined
): FutureData<{ trackedEntities: TrackedEntity[] }> => {
let eventsByStage: D2TrackerEvent[] = [];
try {
eventsByStage = mapQuestionnaireToEventsByStage(questionnaire, orgUnitId, programId);
} catch (error) {
return Future.error(new Error(i18n.t("There was an error processing the form")));
}

const attributes: D2TrackerEnrollmentAttribute[] = questionnaire.entity
? questionnaire.entity.questions.map(question => {
Expand Down
1 change: 1 addition & 0 deletions src/utils/dates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isValidDate = (date: Date): boolean => !Number.isNaN(date.getTime());

0 comments on commit 04e4510

Please sign in to comment.