Skip to content

Commit

Permalink
move logic into data layer
Browse files Browse the repository at this point in the history
  • Loading branch information
eperedo committed Jun 10, 2024
1 parent 4767357 commit 962fad0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 47 deletions.
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
27 changes: 8 additions & 19 deletions src/data/metadata/MetadataD2ApiRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { D2Api, D2Model, Id, MetadataResponse, Model, Stats } from "../../types/
import { Dictionary, isNotEmpty, Maybe } from "../../types/utils";
import { cache } from "../../utils/cache";
import { promiseMap } from "../../utils/common";
import { getD2APiFromInstance } from "../../utils/d2-utils";
import { D2MetadataUtils, getD2APiFromInstance } from "../../utils/d2-utils";
import { debug } from "../../utils/debug";
import { paginate } from "../../utils/pagination";
import { metadataTransformations } from "../transformations/PackageTransformations";
Expand Down Expand Up @@ -158,23 +158,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 +542,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
27 changes: 2 additions & 25 deletions src/domain/metadata/usecases/MetadataSyncUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ 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 {
DataElement,
DataSet,
Document,
MetadataEntities,
MetadataPackage,
Program,
} from "../entities/MetadataEntities";
import { Document, MetadataEntities, MetadataPackage, Program } from "../entities/MetadataEntities";
import { NestedRules } from "../entities/MetadataExcludeIncludeRules";
import { buildNestedRules, cleanObject, cleanReferences, getAllReferences } from "../utils";

Expand Down Expand Up @@ -49,27 +41,18 @@ 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 =
type === "dataElements" || type === "dataSets"
? this.excludeDefaultsFromElement(fixedElement as DataElement, defaultIds)
: fixedElement;

// Store metadata object in result
const object = cleanObject(
this.api,
schema.name,
elementWithoutDefaults,
fixedElement,
excludeRules,
includeSharingSettings,
removeOrgUnitReferences,
Expand Down Expand Up @@ -104,12 +87,6 @@ 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
47 changes: 47 additions & 0 deletions src/utils/d2-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import _ from "lodash";
import { D2Api } from "../types/d2-api";
import { Instance } from "../domain/instance/entities/Instance";
import { Id } from "../domain/common/entities/Schemas";
import { DataElement, DataSet, Program } from "../domain/metadata/entities/MetadataEntities";
import { cache } from "./cache";

export function getMajorVersion(version: string): number {
const apiVersion = _.get(version.split("."), 1);
Expand All @@ -22,3 +25,47 @@ export function getD2APiFromInstance(instance: Instance) {
*/
return new D2Api({ baseUrl: instance.url, auth: instance.auth, backend: "fetch" });
}

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;
5 changes: 4 additions & 1 deletion src/utils/synchronization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MAPPED_BY_VALUE_KEY } from "../presentation/react/core/components/mappi
import { D2Api } from "../types/d2-api";
import { buildObject } from "../types/utils";
import "../utils/lodash-mixins";
import { D2MetadataUtils } from "./d2-utils";

//TODO: when all request to metadata using metadataRepository.getMetadataByIds
// this function should be removed
Expand All @@ -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

0 comments on commit 962fad0

Please sign in to comment.