diff --git a/i18n/en.pot b/i18n/en.pot index 5102f45ca..1fcaf189b 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -5,8 +5,8 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2024-05-08T09:54:22.766Z\n" -"PO-Revision-Date: 2024-05-08T09:54:22.767Z\n" +"POT-Creation-Date: 2024-06-10T12:10:57.544Z\n" +"PO-Revision-Date: 2024-06-10T12:10:57.544Z\n" msgid "" "THIS NEW RELEASE INCLUDES SHARING SETTINGS PER INSTANCES. FOR THIS VERSION " diff --git a/src/data/metadata/D2MetadataUtils.ts b/src/data/metadata/D2MetadataUtils.ts new file mode 100644 index 000000000..59213c380 --- /dev/null +++ b/src/data/metadata/D2MetadataUtils.ts @@ -0,0 +1,49 @@ +import _ from "lodash"; +import { Id } from "../../domain/common/entities/Schemas"; +import { DataElement, DataSet, Program } from "../../domain/metadata/entities/MetadataEntities"; +import { D2Api } from "../../types/d2-api"; +import { cache } from "../../utils/cache"; + +export class D2MetadataUtils { + static async excludeDefaults(metadata: GenericMetadata, defaultIds: Id[]) { + // In DHIS 2.38.5 and above the defaults parameter is not working + // /api/metadata.json?fields=id,categoryCombo&filter=id:eq:data_element_id&defaults=INCLUDE/EXCLUDE; this is not working + // so manually removing the default categoryCombo + const result = _(metadata) + .mapValues(elements => { + return elements.map(element => this.excludeDefaultsFromModels(element, defaultIds)); + }) + .value(); + return result; + } + + @cache() + static async getDefaultIds(api: D2Api, filter?: string): Promise { + const response = (await api + .get("/metadata", { + filter: "identifiable:eq:default", + fields: "id", + }) + .getData()) as { + [key: string]: { id: string }[]; + }; + + const metadata = _.pickBy(response, (_value, type) => !filter || type === filter); + + return _(metadata) + .omit(["system"]) + .values() + .flatten() + .map(({ id }) => id) + .value(); + } + + private static excludeDefaultsFromModels(fixedElement: MetadataDefaultCategoryCombo, defaultIds: Id[]) { + return fixedElement.categoryCombo && defaultIds.includes(fixedElement.categoryCombo.id) + ? _(fixedElement).omit("categoryCombo").value() + : fixedElement; + } +} + +type GenericMetadata = { dataElements: DataElement[]; dataSets: DataSet[]; programs: Program[] }; +type MetadataDefaultCategoryCombo = DataElement | DataSet | Program; diff --git a/src/data/metadata/MetadataD2ApiRepository.ts b/src/data/metadata/MetadataD2ApiRepository.ts index 2614560ee..801e449f7 100644 --- a/src/data/metadata/MetadataD2ApiRepository.ts +++ b/src/data/metadata/MetadataD2ApiRepository.ts @@ -37,6 +37,7 @@ import { getD2APiFromInstance } from "../../utils/d2-utils"; import { debug } from "../../utils/debug"; import { paginate } from "../../utils/pagination"; import { metadataTransformations } from "../transformations/PackageTransformations"; +import { D2MetadataUtils } from "./D2MetadataUtils"; export class MetadataD2ApiRepository implements MetadataRepository { private api: D2Api; @@ -158,23 +159,8 @@ export class MetadataD2ApiRepository implements MetadataRepository { @cache() public async getDefaultIds(filter?: string): Promise { - const response = (await this.api - .get("/metadata", { - filter: "identifiable:eq:default", - fields: "id", - }) - .getData()) as { - [key: string]: { id: string }[]; - }; - - const metadata = _.pickBy(response, (_value, type) => !filter || type === filter); - - return _(metadata) - .omit(["system"]) - .values() - .flatten() - .map(({ id }) => id) - .value(); + const metadata = D2MetadataUtils.getDefaultIds(this.api, filter); + return metadata; } @cache() @@ -557,7 +543,11 @@ export class MetadataD2ApiRepository implements MetadataRepository { if (results.system) delete results.system; const metadata = await this.validateEventVisualizationsByIds(results); - return metadata; + const defaultIds = await this.getDefaultIds(); + const metadataExcludeDefaults = includeDefaults + ? metadata + : await D2MetadataUtils.excludeDefaults(metadata, defaultIds); + return metadataExcludeDefaults; } private async validateEventVisualizationsByIds(metadata: any) { diff --git a/src/utils/synchronization.ts b/src/utils/synchronization.ts index fe9d4122a..34effe51a 100644 --- a/src/utils/synchronization.ts +++ b/src/utils/synchronization.ts @@ -1,6 +1,7 @@ import FileSaver from "file-saver"; import _ from "lodash"; import moment, { unitOfTime } from "moment"; +import { D2MetadataUtils } from "../data/metadata/D2MetadataUtils"; import { MetadataMapping, MetadataMappingDictionary } from "../domain/mapping/entities/MetadataMapping"; import { CategoryOptionCombo } from "../domain/metadata/entities/MetadataEntities"; import { SynchronizationRule } from "../domain/rules/entities/SynchronizationRule"; @@ -29,7 +30,9 @@ export async function getMetadata(api: D2Api, elements: string[], fields = ":all const response = await Promise.all(promises); const results = _.deepMerge({}, ...response); if (results.system) delete results.system; - return results; + const defaultIds = await D2MetadataUtils.getDefaultIds(api); + const metadataExcludeDefaults = await D2MetadataUtils.excludeDefaults(results, defaultIds); + return metadataExcludeDefaults; } export const availablePeriods = buildObject<{