Skip to content

Commit

Permalink
fix: review comments - move dhis specific entities to data layer
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Nov 6, 2023
1 parent 0e6bf7e commit 0ec0f6d
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 97 deletions.
93 changes: 81 additions & 12 deletions src/data/repositories/SurveyFormD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import {
EventProgramMetadata,
ImportStrategy,
Option,
TrackerEventsPostRequest,
} from "../../domain/entities/EventProgram";
import { Survey } from "../../domain/entities/Survey";
import { DataValue } from "@eyeseetea/d2-api";

export const PPS_SURVEY_FORM_ID = "OGOw5Kt3ytv";
export const PPS_COUNTRY_QUESTIONNAIRE_ID = "a4aYe2Eoaul";
Expand Down Expand Up @@ -209,23 +209,86 @@ export class SurveyD2Repository implements SurveyRepository {
return questions;
}

saveFormData(events: TrackerEventsPostRequest, action: ImportStrategy): FutureData<void> {
return apiToFuture(this.api.tracker.postAsync({ importStrategy: action }, events)).flatMap(
response => {
saveFormData(
questionnaire: Questionnaire,
action: ImportStrategy,
orgUnitId: Id,
eventId: string | undefined,
programId: Id
): FutureData<void> {
return this.mapQuestionnaireToEvent(questionnaire, orgUnitId, programId, eventId).flatMap(
event => {
return apiToFuture(
// eslint-disable-next-line testing-library/await-async-utils
this.api.system.waitFor("TRACKER_IMPORT_JOB", response.response.id)
).flatMap(result => {
if (result) {
return Future.success(undefined);
} else {
return Future.error(new Error("An error occured while saving the survey"));
}
this.api.tracker.postAsync({ importStrategy: action }, { events: [event] })
).flatMap(response => {
return apiToFuture(
// eslint-disable-next-line testing-library/await-async-utils
this.api.system.waitFor("TRACKER_IMPORT_JOB", response.response.id)
).flatMap(result => {
if (result) {
return Future.success(undefined);
} else {
return Future.error(
new Error("An error occured while saving the survey")
);
}
});
});
}
);
}

private mapQuestionnaireToEvent(
questionnaire: Questionnaire,
orgUnitId: string,
programId: Id,
eventId: string | undefined = undefined
): FutureData<D2TrackerEvent> {
const questions = questionnaire.sections.flatMap(section => section.questions);

const dataValues = _(
questions.map(q => {
if (q) {
if (q.type === "select" && q.value) {
return {
dataElement: q.id,
value: q.value.code,
};
} else {
return {
dataElement: q.id,
value: q.value,
};
}
}
})
)
.compact()
.value();

if (eventId) {
return this.getSurveyById(eventId).flatMap(event => {
const updatedEvent: D2TrackerEvent = {
...event,

dataValues: dataValues as DataValue[],
};
return Future.success(updatedEvent);
});
} else {
const event: D2TrackerEvent = {
event: "",
orgUnit: orgUnitId,
program: programId,
status: "ACTIVE",
occurredAt: new Date().toISOString().split("T")?.at(0) || "",
//@ts-ignore
dataValues: dataValues,
};
return Future.success(event);
}
}

getSurveys(programId: Id, orgUnitId: Id): FutureData<Survey[]> {
return apiToFuture(
this.api.tracker.events.get({
Expand Down Expand Up @@ -277,6 +340,12 @@ export class SurveyD2Repository implements SurveyRepository {
});
}

getPopulatedSurveyById(eventId: Id, programId: Id): FutureData<Questionnaire> {
return this.getSurveyById(eventId).flatMap(event => {
return this.getForm(programId, event);
});
}

getSurveyById(eventId: string): FutureData<D2TrackerEvent> {
return apiToFuture(
this.api.tracker.events.getById(eventId, {
Expand Down
17 changes: 14 additions & 3 deletions src/data/repositories/testRepositories/SurveyFormTestRepository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";
import { ImportStrategy, TrackerEventsPostRequest } from "../../../domain/entities/EventProgram";
import { ImportStrategy } from "../../../domain/entities/EventProgram";
import { Future } from "../../../domain/entities/generic/Future";
import { Questionnaire } from "../../../domain/entities/Questionnaire";
import { Id } from "../../../domain/entities/Ref";
import { Survey } from "../../../domain/entities/Survey";
import { SurveyRepository } from "../../../domain/repositories/SurveyRepository";
import { FutureData } from "../../api-futures";
import { PPS_SURVEY_FORM_ID } from "../SurveyFormD2Repository";

export class SurveyTestRepository implements SurveyRepository {
getPopulatedSurveyById(eventId: string, programId: string): FutureData<Questionnaire> {
console.debug(eventId, programId);
throw new Error("Method not implemented.");
}
getForm(programId: string): FutureData<Questionnaire> {
const questionnaire: Questionnaire = {
id: programId,
Expand All @@ -30,8 +35,14 @@ export class SurveyTestRepository implements SurveyRepository {
return Future.success(questionnaire);
}

saveFormData(events: TrackerEventsPostRequest, action: ImportStrategy): FutureData<void> {
if (events && action) return Future.success(undefined);
saveFormData(
events: Questionnaire,
action: ImportStrategy,
orgUnitId: Id,
eventId: string | undefined,
programId: Id
): FutureData<void> {
if (events && action && orgUnitId && eventId && programId) return Future.success(undefined);
else return Future.error(new Error("An error occured while saving the survey"));
}

Expand Down
5 changes: 0 additions & 5 deletions src/domain/entities/EventProgram.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";

export interface TrackerEventsPostRequest {
events: D2TrackerEvent[];
}
export type ImportStrategy = "CREATE" | "UPDATE" | "CREATE_AND_UPDATE" | "DELETE";

export interface EventProgram {
Expand Down
3 changes: 1 addition & 2 deletions src/domain/entities/Survey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Ref } from "@eyeseetea/d2-api";
import { NamedRef } from "./Ref";
import { NamedRef, Ref } from "./Ref";

export type SURVEY_FORM_TYPES =
| "PPSSurveyForm"
Expand Down
2 changes: 1 addition & 1 deletion src/domain/repositories/ModuleRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Id } from "@eyeseetea/d2-api";
import { FutureData } from "../../data/api-futures";
import { AMRSurveyModule } from "../entities/AMRSurveyModule";
import { Id } from "../entities/Ref";

export interface ModuleRepository {
getAll(): FutureData<AMRSurveyModule[]>;
Expand Down
11 changes: 9 additions & 2 deletions src/domain/repositories/SurveyRepository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";
import { FutureData } from "../../data/api-futures";
import { ImportStrategy, TrackerEventsPostRequest } from "../entities/EventProgram";
import { ImportStrategy } from "../entities/EventProgram";
import { Questionnaire } from "../entities/Questionnaire";
import { Id } from "../entities/Ref";
import { Survey } from "../entities/Survey";

export interface SurveyRepository {
getForm(programId: Id, event: D2TrackerEvent | undefined): FutureData<Questionnaire>;
saveFormData(events: TrackerEventsPostRequest, action: ImportStrategy): FutureData<void>;
saveFormData(
events: Questionnaire,
action: ImportStrategy,
orgUnitId: Id,
eventId: string | undefined,
programId: Id
): FutureData<void>;
getSurveys(programId: Id, orgUnitId: Id): FutureData<Survey[]>;
getSurveyById(eventId: string): FutureData<D2TrackerEvent>;
getPopulatedSurveyById(eventId: Id, programId: Id): FutureData<Questionnaire>;
}
3 changes: 1 addition & 2 deletions src/domain/usecases/GetAllAccesibleModulesUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { AMRSurveyModule } from "../entities/AMRSurveyModule";
import { ModuleRepository } from "../repositories/ModuleRepository";
import _ from "../../domain/entities/generic/Collection";
import { Future } from "../entities/generic/Future";
import { NamedRef } from "../entities/Ref";
import { Id } from "@eyeseetea/d2-api";
import { NamedRef, Id } from "../entities/Ref";

export class GetAllAccesibleModulesUseCase {
constructor(private moduleRepository: ModuleRepository) {}
Expand Down
2 changes: 1 addition & 1 deletion src/domain/usecases/GetAllSurveysUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Id } from "@eyeseetea/d2-api";
import { FutureData } from "../../data/api-futures";
import { PPS_SURVEY_FORM_ID } from "../../data/repositories/SurveyFormD2Repository";
import { Future } from "../entities/generic/Future";
import { Id } from "../entities/Ref";
import { Survey, SURVEY_FORM_TYPES } from "../entities/Survey";
import { SurveyRepository } from "../repositories/SurveyRepository";

Expand Down
5 changes: 1 addition & 4 deletions src/domain/usecases/GetPopulatedSurveyUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export class GetPopulatedSurveyUseCase {
default:
return Future.error(new Error("Unknown survey type"));
}

return this.surveyReporsitory.getSurveyById(eventId).flatMap(event => {
return this.surveyReporsitory.getForm(programId, event);
});
return this.surveyReporsitory.getPopulatedSurveyById(eventId, programId);
}
}
67 changes: 7 additions & 60 deletions src/domain/usecases/SaveFormDataUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { DataValue, Id } from "@eyeseetea/d2-api";
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";
import { FutureData } from "../../data/api-futures";
import { Future } from "../entities/generic/Future";
import { Questionnaire } from "../entities/Questionnaire";
import { SURVEY_FORM_TYPES } from "../entities/Survey";
import { SurveyRepository } from "../repositories/SurveyRepository";
import _ from "../../domain/entities/generic/Collection";
import { PPS_SURVEY_FORM_ID } from "../../data/repositories/SurveyFormD2Repository";
import { Id } from "../entities/Ref";

export class SaveFormDataUseCase {
constructor(private surveyReporsitory: SurveyRepository) {}
Expand All @@ -26,64 +25,12 @@ export class SaveFormDataUseCase {
return Future.error(new Error("Unknown survey type"));
}

return this.mapQuestionnaireToEvent(questionnaire, orgUnitId, programId, eventId).flatMap(
event => {
return this.surveyReporsitory.saveFormData(
{ events: [event] },
"CREATE_AND_UPDATE"
);
}
return this.surveyReporsitory.saveFormData(
questionnaire,
"CREATE_AND_UPDATE",
orgUnitId,
eventId,
programId
);
}

private mapQuestionnaireToEvent(
questionnaire: Questionnaire,
orgUnitId: string,
programId: Id,
eventId: string | undefined = undefined
): FutureData<D2TrackerEvent> {
const questions = questionnaire.sections.flatMap(section => section.questions);

const dataValues = _(
questions.map(q => {
if (q) {
if (q.type === "select" && q.value) {
return {
dataElement: q.id,
value: q.value.code,
};
} else {
return {
dataElement: q.id,
value: q.value,
};
}
}
})
)
.compact()
.value();

if (eventId) {
return this.surveyReporsitory.getSurveyById(eventId).flatMap(event => {
const updatedEvent: D2TrackerEvent = {
...event,

dataValues: dataValues as DataValue[],
};
return Future.success(updatedEvent);
});
} else {
const event: D2TrackerEvent = {
event: "",
orgUnit: orgUnitId,
program: programId,
status: "ACTIVE",
occurredAt: new Date().toISOString().split("T")?.at(0) || "",
//@ts-ignore
dataValues: dataValues,
};
return Future.success(event);
}
}
}
1 change: 0 additions & 1 deletion src/webapp/components/custom-card/CustomCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { PropsWithChildren } from "react";
import { Box, Typography } from "@material-ui/core";
import { Paper } from "material-ui";
import styled from "styled-components";

import i18n from "@eyeseetea/d2-ui-components/locales";
import { palette } from "../../pages/app/themes/dhis2.theme";

Expand Down
1 change: 0 additions & 1 deletion src/webapp/components/left-nav/LeftNavMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import i18n from "@eyeseetea/feedback-component/locales";
import { Button, colors, ListItem, makeStyles, Theme, Typography } from "@material-ui/core";

import { NavLink } from "react-router-dom";
import { MenuLeaf } from "../../hooks/useMenu";
import { palette } from "../../pages/app/themes/dhis2.theme";
Expand Down
3 changes: 1 addition & 2 deletions src/webapp/components/orgunit-selector/OrgUnitSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { MenuItem, Select } from "@material-ui/core";
import React, { useState } from "react";

import SearchInput from "./SearchInput";
import styled from "styled-components";

import { useAppContext } from "../../contexts/app-context";
import { OrgUnitAccess } from "../../../domain/entities/User";
import { useCurrentOrgUnitContext } from "../../contexts/current-org-unit-context/current-orgUnit-context";
import i18n from "@eyeseetea/feedback-component/locales";

interface OrgUnitProps {
fullWidth?: boolean;
}
Expand Down
2 changes: 1 addition & 1 deletion src/webapp/components/survey/hook/useSurveyForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSnackbar } from "@eyeseetea/d2-ui-components";
import { useAppContext } from "../../../contexts/app-context";
import { Questionnaire } from "../../../../domain/entities/Questionnaire";
import { SURVEY_FORM_TYPES } from "../../../../domain/entities/Survey";
import { Id } from "@eyeseetea/d2-api";
import { Id } from "../../../../domain/entities/Ref";

export function useSurveyForm(
formType: SURVEY_FORM_TYPES,
Expand Down

0 comments on commit 0ec0f6d

Please sign in to comment.