Skip to content

Commit

Permalink
Merge pull request #137 from EyeSeeTea/fix/glass-submission-paging-bug
Browse files Browse the repository at this point in the history
feat: get TEIs iteratively in data submission report
  • Loading branch information
ifoche authored Feb 22, 2024
2 parents 2c5e4ea + cc977ca commit 3531b05
Showing 1 changed file with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ interface MessageConversations {
}[];
}

type TrackedEntityInstance = {
attributes: {
value: string;
}[];
orgUnit: string;
};

type DataValueType = {
dataElement: string;
period: string;
Expand Down Expand Up @@ -228,13 +235,7 @@ export class GLASSDataSubmissionDefaultRepository implements GLASSDataSubmission
orgUnit: string,
modules: GLASSDataSubmissionModule[]
): Promise<GLASSDataSubmissionItemIdentifier[]> {
const { instances } = await this.api
.get<{ instances: { attributes: { value: string }[]; orgUnit: string }[] }>("/tracker/trackedEntities", {
program: program,
orgUnit: orgUnit,
fields: "attributes[value],orgUnit",
})
.getData();
const instances = await this.getTrackedEntityInstances(program, orgUnit);

const dataSubmissionPeriods = getDataSubmissionPeriods();
const orgUnitModules: { orgUnit: string; module: string }[] = _(instances)
Expand All @@ -258,6 +259,34 @@ export class GLASSDataSubmissionDefaultRepository implements GLASSDataSubmission
});
}

private async getTrackedEntityInstances(program: string, orgUnit: string): Promise<TrackedEntityInstance[]> {
let instances: TrackedEntityInstance[] = [];
let currentPage = 1;
let totalPages = 1;
const pageSize = 200;

while (currentPage <= totalPages) {
const response = await this.api
.get<{ instances: TrackedEntityInstance[] }>("/tracker/trackedEntities", {
program: program,
orgUnit: orgUnit,
fields: "attributes[value],orgUnit",
pageSize: pageSize,
page: currentPage,
})
.getData();

instances = instances.concat(response.instances);

if (response.instances.length === pageSize) {
totalPages++;
}
currentPage++;
}

return instances;
}

private async getCountriesOutsideNARegion(): Promise<string[]> {
const { organisationUnits: kosovoOu } = await this.api.metadata
.get({
Expand Down Expand Up @@ -1034,7 +1063,7 @@ const emptyPage: PaginatedObjects<GLASSDataSubmissionItem> = {
const moduleMapping: Record<string, string> = {
AMC: "AMC",
AMR: "AMR",
AMR_FUNGAL: "AMR - Fungal",
AMR_FUNGHI: "AMR - Fungal",
AMR_INDIVIDUAL: "AMR - Individual",
EAR: "EAR",
EGASP: "EGASP",
Expand Down

0 comments on commit 3531b05

Please sign in to comment.