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
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
49 changes: 49 additions & 0 deletions src/data/metadata/D2MetadataUtils.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> {
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;
26 changes: 8 additions & 18 deletions src/data/metadata/MetadataD2ApiRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -158,23 +159,8 @@ export class MetadataD2ApiRepository implements MetadataRepository {

@cache()
public async getDefaultIds(filter?: string): Promise<string[]> {
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()
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/synchronization.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<{
Expand Down
Loading