Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude default categories from dataElements #959

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/domain/metadata/usecases/MetadataSyncUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ 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,
DataSet,
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 +49,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;

// In DHIS 2.38.5 and above 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 =
Copy link
Contributor

@tokland tokland Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A use case should not make any reference to DHIS2 (that's an implementation detail). If DHIS2 has a bug (Indeed, I've checked it: 2.36 OK, 2.38 FAIL), any extra processing should be done in the data layer, in the DHIS2 repository implementation (check any file which calls the API with that option defaults: boolean -> apart from the metadata repo, I've found also utils/synchronization.ts; check if more)

Note: checking dhis2-core, DefaultCategoryService.java, the models that have a default object are the 4 expected: category, categoryOption, categoryCombo, categoryOptionCombo.

type === "dataElements" || type === "dataSets"
? this.excludeDefaultsFromElement(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 +104,12 @@ export class MetadataSyncUseCase extends GenericSyncUseCase {
return recursiveExport(originalBuilder);
}

private excludeDefaultsFromElement(fixedElement: DataElement | DataSet, 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
Loading