Skip to content

Commit

Permalink
fix: modularize code in repo
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Jul 23, 2024
1 parent c150e73 commit d61e4dc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 45 deletions.
56 changes: 22 additions & 34 deletions src/data/repositories/DiseaseOutbreakEventD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./utils/DiseaseOutbreakMapper";
import { RTSL_ZEBRA_ORG_UNIT_ID, RTSL_ZEBRA_PROGRAM_ID } from "./consts/DiseaseOutbreakConstants";
import { D2TrackerTrackedEntity } from "@eyeseetea/d2-api/api/trackerTrackedEntities";
import { getProgramTEAsMetadata } from "./utils/MetadataHelper";

export class DiseaseOutbreakEventD2Repository implements DiseaseOutbreakEventRepository {
constructor(private api: D2Api) {}
Expand Down Expand Up @@ -40,41 +41,28 @@ export class DiseaseOutbreakEventD2Repository implements DiseaseOutbreakEventRep
});
}
save(diseaseOutbreak: DiseaseOutbreakEventBaseAttrs): FutureData<void> {
return apiToFuture(
this.api.models.programs.get({
fields: {
id: true,
programTrackedEntityAttributes: {
trackedEntityAttribute: {
id: true,
valueType: true,
code: true,
},
},
},
filter: {
id: { eq: RTSL_ZEBRA_PROGRAM_ID },
},
})
).flatMap(metadataResponse => {
const attributesMetadata = metadataResponse.objects[0]?.programTrackedEntityAttributes;
if (!attributesMetadata) throw new Error("Program not found");
const trackedEntity: D2TrackerTrackedEntity =
mapDiseaseOutbreakEventToTrackedEntityAttributes(
diseaseOutbreak,
attributesMetadata
);
return apiToFuture(getProgramTEAsMetadata(this.api, RTSL_ZEBRA_PROGRAM_ID)).flatMap(
teasMetadataResponse => {
const teasMetadata =
teasMetadataResponse.objects[0]?.programTrackedEntityAttributes;

return apiToFuture(
this.api.tracker.post(
{ importStrategy: "CREATE_AND_UPDATE" },
{ trackedEntities: [trackedEntity] }
)
).map(saveResponse => {
if (saveResponse.status === "ERROR")
throw new Error("Error saving disease ooutbreak event");
});
});
if (!teasMetadata)
throw new Error("Program Tracked Entity Attributes metadata not found");

const trackedEntity: D2TrackerTrackedEntity =
mapDiseaseOutbreakEventToTrackedEntityAttributes(diseaseOutbreak, teasMetadata);

return apiToFuture(
this.api.tracker.post(
{ importStrategy: "CREATE_AND_UPDATE" },
{ trackedEntities: [trackedEntity] }
)
).map(saveResponse => {
if (saveResponse.status === "ERROR")
throw new Error("Error saving disease ooutbreak event");
});
}
);
}
getConfigStrings(): FutureData<ConfigLabel[]> {
throw new Error("Method not implemented.");
Expand Down
22 changes: 12 additions & 10 deletions src/data/repositories/utils/DiseaseOutbreakMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import { SelectedPick } from "@eyeseetea/d2-api/api";
import { D2TrackedEntityAttributeSchema } from "@eyeseetea/d2-api/2.36";
import { D2TrackerEnrollment } from "@eyeseetea/d2-api/api/trackerEnrollments";

type D2TrackedEntityAttribute = {
trackedEntityAttribute: SelectedPick<
D2TrackedEntityAttributeSchema,
{
id: true;
valueType: true;
code: true;
}
>;
};

export function mapTrackedEntityAttributesToDiseaseOutbreak(
trackedEntity: D2TrackerTrackedEntity
): DiseaseOutbreakEventBaseAttrs {
Expand Down Expand Up @@ -94,16 +105,7 @@ export function mapTrackedEntityAttributesToDiseaseOutbreak(

export function mapDiseaseOutbreakEventToTrackedEntityAttributes(
diseaseOutbreak: DiseaseOutbreakEventBaseAttrs,
attributesMetadata: {
trackedEntityAttribute: SelectedPick<
D2TrackedEntityAttributeSchema,
{
id: true;
valueType: true;
code: true;
}
>;
}[]
attributesMetadata: D2TrackedEntityAttribute[]
): D2TrackerTrackedEntity {
const attributes: Attribute[] = attributesMetadata.map(attribute => {
const populatedAttribute = {
Expand Down
21 changes: 21 additions & 0 deletions src/data/repositories/utils/MetadataHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { D2Api } from "@eyeseetea/d2-api/2.36";

export function getProgramTEAsMetadata(api: D2Api, programId: string) {
const teasMetadataResponse = api.models.programs.get({
fields: {
id: true,
programTrackedEntityAttributes: {
trackedEntityAttribute: {
id: true,
valueType: true,
code: true,
},
},
},
filter: {
id: { eq: programId },
},
});

return teasMetadataResponse;
}
2 changes: 1 addition & 1 deletion src/domain/entities/generic/Struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function Struct<Attrs>() {
return Object.fromEntries(entries) as Attrs;
}

_update(partialAttrs: Partial<Attrs>): this {
protected _update(partialAttrs: Partial<Attrs>): this {
const ParentClass = this.constructor as new (values: Attrs) => typeof this;
return new ParentClass({ ...this._getAttributes(), ...partialAttrs });
}
Expand Down

0 comments on commit d61e4dc

Please sign in to comment.