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 a534e0f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 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,24 @@ 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;

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 +94,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 a534e0f

Please sign in to comment.