Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error msgs #72

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 53 additions & 13 deletions src/data/repositories/SurveyFormD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { mapEventToSurvey, mapTrackedEntityToSurvey } from "../utils/surveyListM
import { Questionnaire } from "../../domain/entities/Questionnaire/Questionnaire";
import { ASTGUIDELINE_TYPES } from "../../domain/entities/ASTGuidelines";
import { getSurveyChildCount, SurveyChildCountType } from "../utils/surveyChildCountHelper";
import { TrackerPostResponse } from "@eyeseetea/d2-api/api/tracker";

const OU_CHUNK_SIZE = 500;
export class SurveyD2Repository implements SurveyRepository {
Expand Down Expand Up @@ -215,9 +216,7 @@ export class SurveyD2Repository implements SurveyRepository {
: result.bundleReport?.typeReportMap?.EVENT?.objectReports[0]?.uid;
return Future.success(surveyId);
} else {
return this.getErrorMessageWithNames(
result?.validationReport?.errorReports?.at(0)?.message
).flatMap(errorMessage => {
return this.getErrorMessageWithNames(result).flatMap(errorMessage => {
return Future.error(new Error(`Error: ${errorMessage} `));
});
}
Expand Down Expand Up @@ -449,9 +448,7 @@ export class SurveyD2Repository implements SurveyRepository {
if (result && result.status !== "ERROR") {
return Future.success(undefined);
} else {
return this.getErrorMessageWithNames(
result?.validationReport?.errorReports?.at(0)?.message
).flatMap(errorMessage => {
return this.getErrorMessageWithNames(result).flatMap(errorMessage => {
return Future.error(new Error(`Error: ${errorMessage} `));
});
}
Expand Down Expand Up @@ -479,18 +476,24 @@ export class SurveyD2Repository implements SurveyRepository {
if (result && result.status !== "ERROR") {
return Future.success(undefined);
} else {
return this.getErrorMessageWithNames(
result?.validationReport?.errorReports?.at(0)?.message
).flatMap(errorMessage => {
return this.getErrorMessageWithNames(result).flatMap(errorMessage => {
return Future.error(new Error(`Error: ${errorMessage} `));
});
}
});
});
}

private getErrorMessageWithNames(errMsg?: string): FutureData<string> {
if (!errMsg) return Future.success("");
private getErrorMessageWithNames(result: TrackerPostResponse | null): FutureData<string> {
if (!result) return Future.success("");

//All possible values where DHIS would populate error messages.
//So far, DHIS only populates one of them.

const errMsg =
result.validationReport?.warningReports[0]?.message ||
result.validationReport?.errorReports[0]?.message ||
result.message;

let errorMessageWithNames = errMsg;

Expand All @@ -506,11 +509,20 @@ export class SurveyD2Repository implements SurveyRepository {
const orgUnitPattern = /(?<=OrganisationUnit(:*) )`([A-Za-z0-9]{11})`/g;
const orgUnitIds = orgUnitPattern.exec(errMsg);

//Attribute: `{attributeId}`
const teaPattern = /(?<=attribute(:*) )`([A-Za-z0-9]{11})`/g;
const teaIds = teaPattern.exec(errMsg);

//User: `{userId}`
const userPattern = /(?<=User(:*) )`([A-Za-z0-9]{11})`/g;
const userIds = userPattern.exec(errMsg);

return this.fetchNames({
dataElementId: dataelementIds?.[2],
programId: programIds?.[2],
orgUnitId: orgUnitIds?.[2],
}).flatMap(({ dataElementName, programName, orgUnitName }) => {
teaId: teaIds?.[2],
}).flatMap(({ dataElementName, programName, orgUnitName, teaName }) => {
if (dataelementIds && dataelementIds[2] && dataElementName) {
errorMessageWithNames = this.parseErrorMessage(
errorMessageWithNames,
Expand All @@ -534,6 +546,21 @@ export class SurveyD2Repository implements SurveyRepository {
orgUnitName
);
}
if (teaIds && teaIds[2] && teaName) {
errorMessageWithNames = this.parseErrorMessage(
errorMessageWithNames,
teaIds[2],
teaName
);
}

if (userIds && userIds[2]) {
errorMessageWithNames = this.parseErrorMessage(
errorMessageWithNames,
userIds[2],
"This User"
);
}

return Future.success(errorMessageWithNames);
});
Expand All @@ -551,16 +578,19 @@ export class SurveyD2Repository implements SurveyRepository {
dataElementId,
programId,
orgUnitId,
teaId,
}: {
dataElementId?: Id;
programId?: Id;
orgUnitId?: Id;
teaId?: Id;
}): FutureData<{
dataElementName?: string;
programName?: string;
orgUnitName?: string;
teaName?: string;
}> => {
if (!dataElementId && !programId && !orgUnitId) return Future.success({});
if (!dataElementId && !programId && !orgUnitId && !teaId) return Future.success({});

return apiToFuture(
this.api.metadata
Expand All @@ -583,12 +613,19 @@ export class SurveyD2Repository implements SurveyRepository {
filter: { id: { eq: orgUnitId } },
},
}),
...(teaId && {
trackedEntityAttributes: {
fields: { shortName: true },
filter: { id: { eq: teaId } },
},
}),
})
.map(response => {
const fetchedNames: {
dataElementName?: string;
programName?: string;
orgUnitName?: string;
teaName?: string;
} = {};
if (response?.data?.dataElements) {
fetchedNames.dataElementName = `${response.data.dataElements?.[0]?.shortName}`;
Expand All @@ -599,6 +636,9 @@ export class SurveyD2Repository implements SurveyRepository {
if (response?.data?.organisationUnits) {
fetchedNames.orgUnitName = `${response.data.organisationUnits?.[0]?.shortName}`;
}
if (response?.data?.trackedEntityAttributes) {
fetchedNames.teaName = `${response.data.trackedEntityAttributes?.[0]?.shortName}`;
}
return fetchedNames;
})
);
Expand Down
6 changes: 4 additions & 2 deletions src/webapp/hooks/useSurveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export function useSurveys(surveyFormType: SURVEY_FORM_TYPES) {
setLoadingSurveys(false);
},
err => {
setSurveysError(err.message);
//@ts-ignore
setSurveysError(err?.response.data.message || err.message);
setLoadingSurveys(false);
}
);
Expand All @@ -137,7 +138,8 @@ export function useSurveys(surveyFormType: SURVEY_FORM_TYPES) {
setLoadingSurveys(false);
},
err => {
setSurveysError(err.message);
//@ts-ignore
setSurveysError(err?.response.data.message || err.message);
setLoadingSurveys(false);
}
);
Expand Down
Loading