From d61e4dc7584c2d24e4d4edc9f0b580b3b6df1fca Mon Sep 17 00:00:00 2001 From: 9sneha-n <9sneha.n@gmail.com> Date: Tue, 23 Jul 2024 22:57:21 +0530 Subject: [PATCH] fix: modularize code in repo --- .../DiseaseOutbreakEventD2Repository.ts | 56 ++++++++----------- .../utils/DiseaseOutbreakMapper.ts | 22 ++++---- src/data/repositories/utils/MetadataHelper.ts | 21 +++++++ src/domain/entities/generic/Struct.ts | 2 +- 4 files changed, 56 insertions(+), 45 deletions(-) create mode 100644 src/data/repositories/utils/MetadataHelper.ts diff --git a/src/data/repositories/DiseaseOutbreakEventD2Repository.ts b/src/data/repositories/DiseaseOutbreakEventD2Repository.ts index b4d40877..9c38fef9 100644 --- a/src/data/repositories/DiseaseOutbreakEventD2Repository.ts +++ b/src/data/repositories/DiseaseOutbreakEventD2Repository.ts @@ -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) {} @@ -40,41 +41,28 @@ export class DiseaseOutbreakEventD2Repository implements DiseaseOutbreakEventRep }); } save(diseaseOutbreak: DiseaseOutbreakEventBaseAttrs): FutureData { - 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 { throw new Error("Method not implemented."); diff --git a/src/data/repositories/utils/DiseaseOutbreakMapper.ts b/src/data/repositories/utils/DiseaseOutbreakMapper.ts index f3f660b7..660ce1fb 100644 --- a/src/data/repositories/utils/DiseaseOutbreakMapper.ts +++ b/src/data/repositories/utils/DiseaseOutbreakMapper.ts @@ -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 { @@ -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 = { diff --git a/src/data/repositories/utils/MetadataHelper.ts b/src/data/repositories/utils/MetadataHelper.ts new file mode 100644 index 00000000..83746b01 --- /dev/null +++ b/src/data/repositories/utils/MetadataHelper.ts @@ -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; +} diff --git a/src/domain/entities/generic/Struct.ts b/src/domain/entities/generic/Struct.ts index e865f656..5a01041e 100644 --- a/src/domain/entities/generic/Struct.ts +++ b/src/domain/entities/generic/Struct.ts @@ -24,7 +24,7 @@ export function Struct() { return Object.fromEntries(entries) as Attrs; } - _update(partialAttrs: Partial): this { + protected _update(partialAttrs: Partial): this { const ParentClass = this.constructor as new (values: Attrs) => typeof this; return new ParentClass({ ...this._getAttributes(), ...partialAttrs }); }