diff --git a/src/domain/metadata/usecases/MetadataSyncUseCase.ts b/src/domain/metadata/usecases/MetadataSyncUseCase.ts index 2c08376ad..184f3605f 100644 --- a/src/domain/metadata/usecases/MetadataSyncUseCase.ts +++ b/src/domain/metadata/usecases/MetadataSyncUseCase.ts @@ -5,11 +5,12 @@ import { ExportBuilder } from "../../../types/synchronization"; import { promiseMap } from "../../../utils/common"; import { debug } from "../../../utils/debug"; import { Ref } from "../../common/entities/Ref"; +import { Id } from "../../common/entities/Schemas"; import { Instance } from "../../instance/entities/Instance"; import { MappingMapper } from "../../mapping/helpers/MappingMapper"; import { SynchronizationResult } from "../../reports/entities/SynchronizationResult"; import { GenericSyncUseCase } from "../../synchronization/usecases/GenericSyncUseCase"; -import { Document, MetadataEntities, MetadataPackage, Program } from "../entities/MetadataEntities"; +import { DataElement, Document, MetadataEntities, MetadataPackage, Program } from "../entities/MetadataEntities"; import { NestedRules } from "../entities/MetadataExcludeIncludeRules"; import { buildNestedRules, cleanObject, cleanReferences, getAllReferences } from "../utils"; @@ -41,6 +42,7 @@ export class MetadataSyncUseCase extends GenericSyncUseCase { const metadataRepository = await this.getMetadataRepository(); const syncMetadata = await metadataRepository.getMetadataByIds(ids); const elements = syncMetadata[collectionName] || []; + const defaultIds = await metadataRepository.getDefaultIds(); for (const element of elements) { //ProgramRules is not included in programs items in the response by the dhis2 API @@ -48,11 +50,16 @@ export class MetadataSyncUseCase extends GenericSyncUseCase { const fixedElement = type === "programs" ? await this.requestAndIncludeProgramRules(element as Program) : element; + const elementWithoutDefaults = + type === "dataElements" + ? this.excludeDefaultsFromDataElement(fixedElement as DataElement, defaultIds) + : fixedElement; + // Store metadata object in result const object = cleanObject( this.api, schema.name, - fixedElement, + elementWithoutDefaults, excludeRules, includeSharingSettings, removeOrgUnitReferences, @@ -87,6 +94,12 @@ export class MetadataSyncUseCase extends GenericSyncUseCase { return recursiveExport(originalBuilder); } + private excludeDefaultsFromDataElement(fixedElement: DataElement, defaultIds: Id[]): Partial { + return fixedElement.categoryCombo && defaultIds.includes(fixedElement.categoryCombo.id) + ? _(fixedElement).omit("categoryCombo").value() + : fixedElement; + } + public buildPayload = memoize(async () => { const { metadataIds, syncParams, filterRules = [] } = this.builder; const {