Skip to content

Commit

Permalink
exclude default categories from dataElements
Browse files Browse the repository at this point in the history
  • Loading branch information
eperedo committed Jun 8, 2024
1 parent 437c8ef commit c0d94af
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/domain/metadata/usecases/MetadataSyncUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -41,18 +42,27 @@ 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
//we request it manually and insert it in the element
const fixedElement =
type === "programs" ? await this.requestAndIncludeProgramRules(element as Program) : element;

// this is because in DHIS 2.38.6 the defaults parameter is not working
// /api/metadata.json?fields=id&filter=id:eq:data_element_id&categoryCombo&defaults=INCLUDE/EXCLUDE; this is not working
// so manually removing the default categoryCombo
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,
Expand Down Expand Up @@ -87,6 +97,12 @@ export class MetadataSyncUseCase extends GenericSyncUseCase {
return recursiveExport(originalBuilder);
}

private excludeDefaultsFromDataElement(fixedElement: DataElement, defaultIds: Id[]): Partial<DataElement> {
return fixedElement.categoryCombo && defaultIds.includes(fixedElement.categoryCombo.id)
? _(fixedElement).omit("categoryCombo").value()
: fixedElement;
}

public buildPayload = memoize(async () => {
const { metadataIds, syncParams, filterRules = [] } = this.builder;
const {
Expand Down

0 comments on commit c0d94af

Please sign in to comment.