Skip to content

Commit

Permalink
Merge branch 'development' into feat/restructure-survey-form
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelAdell authored Feb 4, 2025
2 parents 3b51bda + b6ba458 commit 47a0526
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 43 deletions.
4 changes: 2 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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: 2025-01-21T09:58:39.001Z\n"
"PO-Revision-Date: 2025-01-21T09:58:39.001Z\n"
"POT-Creation-Date: 2025-01-17T13:50:55.771Z\n"
"PO-Revision-Date: 2025-01-17T13:50:55.771Z\n"

msgid "There was a problem with \"{{name}}\" - {{prop}} is not set"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion i18n/es.po
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: i18next-conv\n"
"POT-Creation-Date: 2025-01-21T09:58:39.001Z\n"
"POT-Creation-Date: 2025-01-17T13:50:55.771Z\n"
"PO-Revision-Date: 2018-10-25T09:02:35.143Z\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
Expand Down
62 changes: 48 additions & 14 deletions src/data/repositories/SurveyFormD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
TrackedEntitiesGetResponse,
} from "@eyeseetea/d2-api/api/trackerTrackedEntities";
import {
getParentDataElementForProgram,
getSurveyType,
getTrackedEntityAttributeType,
isTrackerProgram,
Expand All @@ -38,8 +39,15 @@ 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";
import { Maybe } from "../../utils/ts-utils";

const OU_CHUNK_SIZE = 500;

type Filter = {
id: string;
value: string;
};

export class SurveyD2Repository implements SurveyRepository {
constructor(private api: D2Api) {}

Expand Down Expand Up @@ -221,33 +229,53 @@ export class SurveyD2Repository implements SurveyRepository {
});
}

getSurveys(
surveyFormType: SURVEY_FORM_TYPES,
programId: Id,
orgUnitId: Id,
chunked = false
): FutureData<Survey[]> {
getSurveys(options: {
surveyFormType: SURVEY_FORM_TYPES;
programId: Id;
parentId?: Id;
orgUnitId: Id;
chunked: boolean;
}): FutureData<Survey[]> {
const { surveyFormType, programId, parentId, orgUnitId, chunked = false } = options;
const filter = this.getFilterByParentId(programId, parentId);

return isTrackerProgram(programId)
? this.getTrackerProgramSurveys(surveyFormType, programId, orgUnitId, chunked)
: this.getEventProgramSurveys(surveyFormType, programId, orgUnitId);
? this.getTrackerProgramSurveys(surveyFormType, programId, orgUnitId, chunked, filter)
: this.getEventProgramSurveys(surveyFormType, programId, orgUnitId, filter);
}

private getFilterByParentId(programId: string, parentId: Maybe<string>): Maybe<Filter> {
const filterParentDEId = getParentDataElementForProgram(programId);

const filter =
parentId && filterParentDEId
? {
id: filterParentDEId,
value: parentId,
}
: undefined;

return filter;
}

//Currently tracker programs are only in Prevalence module
private getTrackerProgramSurveys(
surveyFormType: SURVEY_FORM_TYPES,
programId: Id,
orgUnitId: Id,
chunked = false
chunked = false,
filter: Maybe<Filter>
): FutureData<Survey[]> {
return chunked
? this.getTrackerProgramSurveysChunked(surveyFormType, programId, orgUnitId)
: this.getTrackerProgramSurveysUnchunked(surveyFormType, programId, orgUnitId);
? this.getTrackerProgramSurveysChunked(surveyFormType, programId, orgUnitId, filter)
: this.getTrackerProgramSurveysUnchunked(surveyFormType, programId, orgUnitId, filter);
}

private getTrackerProgramSurveysUnchunked(
surveyFormType: SURVEY_FORM_TYPES,
programId: Id,
orgUnitId: Id
orgUnitId: Id,
filter: Maybe<Filter>
): FutureData<Survey[]> {
const ouMode =
(orgUnitId !== "" && programId === PREVALENCE_FACILITY_LEVEL_FORM_ID) ||
Expand All @@ -261,6 +289,7 @@ export class SurveyD2Repository implements SurveyRepository {
program: programId,
orgUnit: orgUnitId,
ouMode: ouMode,
filter: filter ? `${filter.id}:eq:${filter.value}` : undefined,
})
).flatMap((trackedEntities: TrackedEntitiesGetResponse) => {
const surveys = mapTrackedEntityToSurvey(trackedEntities, surveyFormType);
Expand All @@ -271,7 +300,8 @@ export class SurveyD2Repository implements SurveyRepository {
private getTrackerProgramSurveysChunked(
surveyFormType: SURVEY_FORM_TYPES,
programId: Id,
orgUnits: string
orgUnits: string,
filter: Maybe<Filter>
): FutureData<Survey[]> {
const orgUnitIds = orgUnits.split(";");
const chunkedOUs = _(orgUnitIds).chunk(OU_CHUNK_SIZE).value();
Expand All @@ -288,6 +318,7 @@ export class SurveyD2Repository implements SurveyRepository {
},
program: programId,
orgUnit: ouChunk.join(";"),
filter: filter ? `${filter.id}:eq:${filter.value}` : undefined,
})
).flatMap((trackedEntities: TrackedEntitiesGetResponse) => {
const surveys = mapTrackedEntityToSurvey(trackedEntities, surveyFormType);
Expand All @@ -300,19 +331,22 @@ export class SurveyD2Repository implements SurveyRepository {
private getEventProgramSurveys(
surveyFormType: SURVEY_FORM_TYPES,
programId: Id,
orgUnitId: Id
orgUnitId: Id,
filter: Maybe<Filter>
): FutureData<Survey[]> {
const ouMode =
orgUnitId !== "" &&
(programId === PPS_WARD_REGISTER_ID || programId === PPS_HOSPITAL_FORM_ID)
? "DESCENDANTS"
: undefined;

return apiToFuture(
this.api.tracker.events.get({
fields: { $all: true },
program: programId,
orgUnit: orgUnitId,
ouMode: ouMode,
filter: filter ? `${filter.id}:eq:${filter.value}` : undefined,
})
).flatMap(response => {
const events = response.instances;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export class SurveyTestRepository implements SurveyRepository {
else return Future.error(new Error("An error occured while saving the survey"));
}

getSurveys(programId: string, orgUnitId: string): FutureData<Survey[]> {
getSurveys(options: { programId: string; orgUnitId: string }): FutureData<Survey[]> {
const { programId, orgUnitId } = options;

if (programId === PPS_SURVEY_FORM_ID)
return Future.success([
{
Expand Down
15 changes: 9 additions & 6 deletions src/domain/repositories/SurveyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ export interface SurveyRepository {
eventId: string | undefined,
programId: Id
): FutureData<Id | undefined>;
getSurveys(
surveyFormType: SURVEY_FORM_TYPES,
programId: Id,
orgUnitId: Id,
chunked: boolean
): FutureData<Survey[]>;
getSurveys(options: GetSurveyOptions): FutureData<Survey[]>;
getPopulatedSurveyById(
eventId: Id,
programId: Id,
Expand All @@ -46,3 +41,11 @@ export interface SurveyRepository {
secondaryparentId: Id | undefined
): SurveyChildCountType;
}

export type GetSurveyOptions = {
surveyFormType: SURVEY_FORM_TYPES;
programId: Id;
parentId?: Id;
orgUnitId: Id;
chunked: boolean;
};
25 changes: 8 additions & 17 deletions src/domain/usecases/GetAllSurveysUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Survey, SurveyBase, SURVEY_FORM_TYPES } from "../entities/Survey";
import { SurveyRepository } from "../repositories/SurveyRepository";
import { getProgramId } from "../utils/PPSProgramsHelper";
import { GLOBAL_OU_ID } from "./SaveFormDataUseCase";
import _ from "../entities/generic/Collection";
import { getChildCount } from "../utils/getChildCountHelper";

export class GetAllSurveysUseCase {
Expand All @@ -23,23 +22,15 @@ export class GetAllSurveysUseCase {
const ouId = surveyFormType === "PPSSurveyForm" ? GLOBAL_OU_ID : orgUnitId;

return this.surveyReporsitory
.getSurveys(surveyFormType, programId, ouId, chunked)
.getSurveys({
surveyFormType: surveyFormType,
programId: programId,
parentId: parentSurveyId,
orgUnitId: ouId,
chunked: chunked,
})
.flatMap(surveys => {
const filteredSurveys =
surveyFormType === "PPSSurveyForm" ||
(surveyFormType === "PPSHospitalForm" && !parentSurveyId) ||
surveyFormType === "PrevalenceSurveyForm" ||
(surveyFormType === "PrevalenceFacilityLevelForm" && !parentSurveyId)
? surveys
: _(
surveys.map(survey => {
if (survey.rootSurvey.id === parentSurveyId) return survey;
})
)
.compact()
.value();

const surveysWithName = filteredSurveys.map(survey => {
const surveysWithName = surveys.map(survey => {
return Future.join2(
this.surveyReporsitory.getSurveyNameAndASTGuidelineFromId(
survey.rootSurvey.id,
Expand Down
7 changes: 6 additions & 1 deletion src/domain/usecases/SaveFormDataUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export class SaveFormDataUseCase {
//Do not allow creation of multiple Prevalence Facility Level Forms for the same facility.
if (!eventId && surveyFormType === "PrevalenceFacilityLevelForm") {
return this.surveyReporsitory
.getSurveys(surveyFormType, programId, ouId, false)
.getSurveys({
surveyFormType: surveyFormType,
programId: programId,
orgUnitId: ouId,
chunked: false,
})
.flatMap(surveys => {
if (surveys.length > 0) {
return Future.error(
Expand Down
2 changes: 1 addition & 1 deletion src/webapp/hooks/useSurveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function useSurveys(surveyFormType: SURVEY_FORM_TYPES) {
},
err => {
//@ts-ignore
setSurveysError(err?.response.data.message || err.message);
setSurveysError(err.message || err?.response.data.message);
setLoadingSurveys(false);
}
);
Expand Down

0 comments on commit 47a0526

Please sign in to comment.