Skip to content

Commit

Permalink
Merge pull request #54 from EyeSeeTea/fix/datastore-cache
Browse files Browse the repository at this point in the history
remove datastore cache
  • Loading branch information
Ramon-Jimenez authored Jun 3, 2024
2 parents af5e09a + 14f92ad commit 14b0a92
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/data/common/Dhis2DataElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Dhis2DataStoreDataForm } from "./Dhis2DataStoreDataForm";
export class Dhis2DataElement {
constructor(private api: D2Api) {}

async get(ids: Id[]): Promise<Record<Id, DataElement>> {
const config = await Dhis2DataStoreDataForm.build(this.api);
async get(ids: Id[], dataSetCode: string): Promise<Record<Id, DataElement>> {
const config = await Dhis2DataStoreDataForm.build(this.api, dataSetCode);
const idGroups = _(ids).uniq().chunk(100).value();

const resList = await promiseMap(idGroups, idsGroup =>
Expand Down
2 changes: 1 addition & 1 deletion src/data/common/Dhis2DataFormRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Dhis2DataFormRepository implements DataFormRepository {
.map(dataElement => ({ id: dataElement.id, code: dataElement.code }))
.value();

const dataElements = await new Dhis2DataElement(this.api).get(dataElementIds);
const dataElements = await new Dhis2DataElement(this.api).get(dataElementIds, dataSet.code);

const dataElementsRulesConfig = this.buildRulesFromConfig(dataElements, configDataForm, allDataElements);
return dataSet.sections.map((section): Section => {
Expand Down
17 changes: 10 additions & 7 deletions src/data/common/Dhis2DataStoreDataForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,16 @@ export class Dhis2DataStoreDataForm {
}

static async build(api: D2Api, dataSetCode?: string): Promise<Dhis2DataStoreDataForm> {
if (cachedStore) return cachedStore;

const dataStore = api.dataStore(Namespaces.D2_AUTOGEN_FORMS);
if (!dataSetCode) throw Error(`Unable to load configuration: dataSetCode not defined`);
if (!dataSetCode) {
console.warn(`Unable to load configuration: dataSetCode not defined`);
return new Dhis2DataStoreDataForm({
optionSets: [],
constants: [],
custom: defaultDataStoreConfig,
subNationals: [],
});
}
const storeValue = await dataStore.get<object>(dataSetCode).getData();
if (!storeValue)
return new Dhis2DataStoreDataForm({
Expand Down Expand Up @@ -350,8 +356,7 @@ export class Dhis2DataStoreDataForm {
},
});

cachedStore = new Dhis2DataStoreDataForm(config);
return cachedStore;
return new Dhis2DataStoreDataForm(config);
}

private static async getOptionSets(api: D2Api, storeConfig: DataFormStoreConfig["custom"]): Promise<OptionSet[]> {
Expand Down Expand Up @@ -651,8 +656,6 @@ interface Constant {
displayDescription: string;
}

let cachedStore: Dhis2DataStoreDataForm | undefined;

function sectionConfig<T extends Record<string, Codec<any>>>(properties: T) {
return optional(record(string, Codec.interface(properties)));
}
Expand Down
16 changes: 13 additions & 3 deletions src/data/common/Dhis2DataValueRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ export class Dhis2DataValueRepository implements DataValueRepository {
})
.getData();

const dataElements = await this.getDataElements(dataValues);
const dataSetResponse = await this.api.models.dataSets
.get({
fields: { id: true, code: true },
filter: { id: { eq: options.dataSetId } },
})
.getData();

const dataSetCode = dataSetResponse.objects[0]?.code;
if (!dataSetCode) throw new Error(`Data set not found: ${options.dataSetId}`);

const dataElements = await this.getDataElements(dataValues, dataSetCode);

const dataValuesFiles = await this.getFileResourcesMapping(dataElements, dataValues);

Expand Down Expand Up @@ -161,9 +171,9 @@ export class Dhis2DataValueRepository implements DataValueRepository {
);
}

private async getDataElements(dataValues: DataValueSetsDataValue[]) {
private async getDataElements(dataValues: DataValueSetsDataValue[], dataSetCode: string) {
const dataElementIds = dataValues.map(dv => dv.dataElement);
return new Dhis2DataElement(this.api).get(dataElementIds);
return new Dhis2DataElement(this.api).get(dataElementIds, dataSetCode);
}

async save(dataValue: DataValue): Promise<DataValue> {
Expand Down

0 comments on commit 14b0a92

Please sign in to comment.