Skip to content

Commit

Permalink
refactor and delete unused files
Browse files Browse the repository at this point in the history
  • Loading branch information
eperedo committed Dec 10, 2024
1 parent 8530908 commit b297bbb
Show file tree
Hide file tree
Showing 22 changed files with 262 additions and 261 deletions.
4 changes: 0 additions & 4 deletions src/CompositionRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { LogRepository } from "$/domain/repositories/LogRepository";
import { OrgUnitRepository } from "$/domain/repositories/OrgUnitRepository";
import { ProjectRepository } from "$/domain/repositories/ProjectRepository";
import { GetAllProjectsUseCase } from "$/domain/usecases/GetAllProjectsUseCase";
import { GetCoreCompetencyUseCase } from "$/domain/usecases/GetCoreCompetencyUseCase";
import { GetDataSetSettingsUseCase } from "$/domain/usecases/GetDataSetSettingsUseCase";
import { GetDataSetsByIdsUseCase } from "$/domain/usecases/GetDataSetsByIdsUseCase";
import { GetDataSetsUseCase } from "$/domain/usecases/GetDataSetsUseCase";
Expand Down Expand Up @@ -95,9 +94,6 @@ function getCompositionRoot(repositories: Repositories) {
orgUnits: {
getByIds: new GetOrgUnitsByIdsUseCase(repositories.orgUnitRepository),
},
coreCompetencies: {
getAll: new GetCoreCompetencyUseCase(repositories.coreCompetencyRepository),
},
indicators: {
get: new GetIndicatorsUseCase(repositories.indicatorRepository),
},
Expand Down
64 changes: 39 additions & 25 deletions src/data/repositories/CoreCompetencyD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { CoreCompetency } from "$/domain/entities/DataSet";
import { CoreCompetencyRepository } from "$/domain/repositories/CoreCompetencyRepository";
import { Future, FutureData } from "$/domain/entities/generic/Future";
import { apiToFuture } from "$/data/api-futures";
import { D2ApiConfig, D2Config } from "$/data/repositories/D2ApiConfig";
import { D2ApiConfig, D2Config } from "$/data/repositories/D2ApiMetadata";
import i18n from "$/utils/i18n";
import { Id } from "$/domain/entities/Ref";

export class CoreCompetencyD2Repository implements CoreCompetencyRepository {
private d2ApiConfig: D2ApiConfig;
Expand All @@ -16,35 +17,48 @@ export class CoreCompetencyD2Repository implements CoreCompetencyRepository {
getAll(): FutureData<CoreCompetency[]> {
return this.getConfig().flatMap(config => {
const coreCompetencyId = config.dataElementGroupSets.coreCompetency.id;
return apiToFuture(
this.api.models.dataElementGroupSets.get({
fields: {
id: true,
displayName: true,
dataElementGroups: { id: true, code: true, displayName: true },
},
filter: { id: { eq: coreCompetencyId } },
})
).flatMap(d2Response => {
const coreCompetencyGroup = d2Response.objects[0];
return coreCompetencyGroup
? Future.success(
coreCompetencyGroup.dataElementGroups.map(
(d2DataElementGroup): CoreCompetency => ({
id: d2DataElementGroup.id,
code: d2DataElementGroup.code,
name: d2DataElementGroup.displayName,
})
)
)
: Future.error(
new Error(i18n.t(`Core competency group not found: ${coreCompetencyId}`))
);
return this.getDataElementGroupSetById(coreCompetencyId).flatMap(d2GroupSet => {
return this.mapToCoreCompetencies(d2GroupSet.dataElementGroups);
});
});
}

private getDataElementGroupSetById(id: Id): FutureData<D2DataElementGroupSet> {
return apiToFuture(
this.api.models.dataElementGroupSets.get({
fields: {
id: true,
displayName: true,
dataElementGroups: { id: true, code: true, displayName: true },
},
filter: { id: { eq: id } },
})
).flatMap(d2Response => {
const d2DataElementGroupSet = d2Response.objects[0];
return d2DataElementGroupSet
? Future.success(d2DataElementGroupSet)
: Future.error(new Error(i18n.t(`dataElementGroupSet not found: ${id}`)));
});
}

private mapToCoreCompetencies(
dataElementGroups: D2DataElementGroup[]
): FutureData<CoreCompetency[]> {
return Future.success(
dataElementGroups.map(
(d2DataElementGroup): CoreCompetency => ({
id: d2DataElementGroup.id,
code: d2DataElementGroup.code,
name: d2DataElementGroup.displayName,
})
)
);
}

private getConfig(): FutureData<D2Config> {
return this.d2ApiConfig.get();
}
}

type D2DataElementGroupSet = { id: Id; dataElementGroups: D2DataElementGroup[] };
type D2DataElementGroup = { id: Id; displayName: string; code: string };
Original file line number Diff line number Diff line change
Expand Up @@ -65,60 +65,63 @@ export class D2ApiConfig {

private getMetadata(): FutureData<D2Config> {
return apiToFuture(this.api.metadata.get(metadataFields)).map(d2Response => {
const getOrThrowMetadata = (metadataKey: keyof typeof metadataFields, code: string) =>
getOrThrow(d2Response[metadataKey], code);

return {
attributes: this.buildAttributes(d2Response.attributes),
categories: {
project: getOrThrow(d2Response.categories, metadataCodes.categories.project),
project: getOrThrowMetadata("categories", metadataCodes.categories.project),
},
dataElementGroupSets: {
coreCompetency: getOrThrow(
d2Response.dataElementGroupSets,
coreCompetency: getOrThrowMetadata(
"dataElementGroupSets",
metadataCodes.dataElementGroupSets.coreCompetency
),
theme: getOrThrow(
d2Response.dataElementGroupSets,
theme: getOrThrowMetadata(
"dataElementGroupSets",
metadataCodes.dataElementGroupSets.theme
),
status: getOrThrow(
d2Response.dataElementGroupSets,
status: getOrThrowMetadata(
"dataElementGroupSets",
metadataCodes.dataElementGroupSets.status
),
},
dataElementGroups: {
localIndicator: getOrThrow(
d2Response.dataElementGroups,
localIndicator: getOrThrowMetadata(
"dataElementGroups",
metadataCodes.dataElementGroups.localIndicator
),
donorIndicator: getOrThrow(
d2Response.dataElementGroups,
donorIndicator: getOrThrowMetadata(
"dataElementGroups",
metadataCodes.dataElementGroups.donorIndicator
),
coreIndicator: getOrThrow(
d2Response.dataElementGroups,
coreIndicator: getOrThrowMetadata(
"dataElementGroups",
metadataCodes.dataElementGroups.coreIndicator
),
},
indicatorGroups: {
coreIndicator: getOrThrow(
d2Response.indicatorGroups,
coreIndicator: getOrThrowMetadata(
"indicatorGroups",
metadataCodes.indicatorGroup.coreIndicator
),
donorIndicator: getOrThrow(
d2Response.indicatorGroups,
donorIndicator: getOrThrowMetadata(
"indicatorGroups",
metadataCodes.indicatorGroup.donorIndicator
),
localIndicator: getOrThrow(
d2Response.indicatorGroups,
localIndicator: getOrThrowMetadata(
"indicatorGroups",
metadataCodes.indicatorGroup.localIndicator
),
},
indicatorGroupSets: {
theme: getOrThrow(
d2Response.indicatorGroupSets,
theme: getOrThrowMetadata(
"indicatorGroupSets",
metadataCodes.indicatorGroupSets.theme
),
status: getOrThrow(
d2Response.indicatorGroupSets,
status: getOrThrowMetadata(
"indicatorGroupSets",
metadataCodes.indicatorGroupSets.status
),
},
Expand Down
65 changes: 40 additions & 25 deletions src/data/repositories/DataElementD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,49 @@ export class DataElementD2Repository implements DataElementRepository {
if (identifiables.length === 0) return Future.success([]);

return chunkRequest(identifiables, values => {
return apiToFuture(
this.api.models.dataElements.get({
fields: {
id: true,
displayName: true,
code: true,
categoryCombo: { id: true, displayName: true },
},
filter: { identifiable: { in: values } },
paging: false,
})
).map(response => {
return response.objects;
});
return this.getByIdentifiables(values);
}).map(response => {
return response.map((d2DataElement): DataElement => {
return {
code: d2DataElement.code,
id: d2DataElement.id,
name: d2DataElement.displayName,
disaggregation: d2DataElement.categoryCombo
? {
id: d2DataElement.categoryCombo.id,
name: d2DataElement.categoryCombo.displayName,
}
: undefined,
};
return this.buildDataElement(d2DataElement);
});
});
}

private buildDataElement(d2DataElement: D2ApiDataElement): DataElement {
return {
code: d2DataElement.code,
id: d2DataElement.id,
name: d2DataElement.displayName,
disaggregation: d2DataElement.categoryCombo
? {
id: d2DataElement.categoryCombo.id,
name: d2DataElement.categoryCombo.displayName,
}
: undefined,
};
}

private getByIdentifiables(identifiables: string[]) {
return apiToFuture(
this.api.models.dataElements.get({
fields: {
id: true,
displayName: true,
code: true,
categoryCombo: { id: true, displayName: true },
},
filter: { identifiable: { in: identifiables } },
paging: false,
})
).map(response => {
return response.objects;
});
}
}

type D2ApiDataElement = {
code: string;
id: string;
displayName: string;
categoryCombo: { id: string; displayName: string };
};
2 changes: 1 addition & 1 deletion src/data/repositories/DataSetD2Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Permission } from "$/domain/entities/Permission";
import _ from "$/domain/entities/generic/Collection";
import { Project } from "$/domain/entities/Project";
import { D2ApiCategoryOption } from "$/data/repositories/D2ApiCategoryOption";
import { D2ApiConfig, D2Config } from "$/data/repositories/D2ApiConfig";
import { D2ApiConfig, D2Config } from "$/data/repositories/D2ApiMetadata";
import { Pager } from "@eyeseetea/d2-api/api";

export class DataSetD2Api {
Expand Down
79 changes: 48 additions & 31 deletions src/data/repositories/DataSetD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { D2AttributeValue } from "@eyeseetea/d2-api/2.36";
import { D2AttributeValue, MetadataPick } from "@eyeseetea/d2-api/2.36";
import { D2Api, MetadataResponse } from "$/types/d2-api";

import { apiToFuture } from "$/data/api-futures";
Expand All @@ -14,9 +14,9 @@ import { getUid } from "$/utils/uid";
import _ from "$/domain/entities/generic/Collection";
import { DataSetD2Api, dataSetFieldsWithOrgUnits } from "$/data/repositories/DataSetD2Api";
import { Maybe } from "$/utils/ts-utils";
import { chunkRequest } from "$/data/utils";
import { D2Config } from "$/data/repositories/D2ApiConfig";
import { Indicator } from "$/domain/entities/Indicator";
import { chunkRequest, runMetadata } from "$/data/utils";
import { D2Config } from "$/data/repositories/D2ApiMetadata";
import { Indicator, IndicatorAttrs } from "$/domain/entities/Indicator";
import { Id, Ref } from "$/domain/entities/Ref";
import { DataSetToSave } from "$/domain/entities/DataSetToSave";

Expand Down Expand Up @@ -106,37 +106,21 @@ export class DataSetD2Repository implements DataSetRepository {
const $requests = chunkRequest<string[]>(ids, dataSetIds => {
return apiToFuture(
this.api.models.dataSets.get({
fields: { $owner: true },
fields: ownerFields,
filter: { id: { in: dataSetIds } },
paging: false,
})
).flatMap(d2Response => {
const dataSetsToSave = dataSetIds.map(dataSetId => {
const existingDataSet = d2Response.objects.find(ds => ds.id === dataSetId);
const dataSet = dataSets.find(dataSet => dataSet.id === dataSetId);
if (!dataSet) {
throw Error(`Cannot find dataSet: ${dataSetId}`);
}

const existingAttributes = existingDataSet?.attributeValues;

const result = {
...(existingDataSet || {}),
...this.buildD2DataSet(dataSet, existingAttributes, config.attributes),
};

const { sharing: _, ...rest } = result;
return rest;
});

return apiToFuture(
const dataSetsToSave = this.getD2DataSetsToSave(
dataSetIds,
d2Response.objects,
dataSets,
config
);

return runMetadata(
this.api.metadata.post({ dataSets: dataSetsToSave })
).flatMap(response => {
const allErrors = this.extractErrorsFromResponse(response);

if (allErrors.length > 0)
return Future.error(new Error(allErrors.join("\n")));

).flatMap(() => {
return this.saveAllSections(dataSetsToSave, dataSets).map(() => []);
});
});
Expand All @@ -146,6 +130,31 @@ export class DataSetD2Repository implements DataSetRepository {
});
}

private getD2DataSetsToSave(
dataSetIds: string[],
d2DataSets: D2DataSetOwner[],
dataSets: DataSetToSave[],
config: D2Config
) {
return dataSetIds.map(dataSetId => {
const existingDataSet = d2DataSets.find(ds => ds.id === dataSetId);
const dataSet = dataSets.find(dataSet => dataSet.id === dataSetId);
if (!dataSet) {
throw Error(`Cannot find dataSet: ${dataSetId}`);
}

const existingAttributes = existingDataSet?.attributeValues;

const result = {
...(existingDataSet || {}),
...this.buildD2DataSet(dataSet, existingAttributes, config.attributes),
};

const { sharing: _, ...rest } = result;
return rest;
});
}

delete(ids: string[]): FutureData<void> {
if (ids.length === 0) return Future.success(undefined);

Expand Down Expand Up @@ -389,4 +398,12 @@ type D2DataSetSection = {
indicators: Ref[];
};

const indicatorTypeLabel = { outcomes: "Outcomes", outputs: "Outputs" };
const indicatorTypeLabel: Record<IndicatorAttrs["type"], string> = {
outcomes: "Outcomes",
outputs: "Outputs",
};

const ownerFields = { $owner: true };
type D2DataSetOwner = MetadataPick<{
dataSets: { fields: typeof ownerFields };
}>["dataSets"][number];
2 changes: 1 addition & 1 deletion src/data/repositories/IndicatorD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { apiToFuture } from "$/data/api-futures";
import { D2ApiConfig, D2Config } from "$/data/repositories/D2ApiConfig";
import { D2ApiConfig, D2Config } from "$/data/repositories/D2ApiMetadata";
import { Future, FutureData } from "$/domain/entities/generic/Future";
import { Indicator } from "$/domain/entities/Indicator";
import { IndicatorRepository } from "$/domain/repositories/IndicatorRepository";
Expand Down
Loading

0 comments on commit b297bbb

Please sign in to comment.