Skip to content

Commit

Permalink
Fix: HPC-9451 total amount discrepancy
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Mar 8, 2024
1 parent 6262e47 commit e7bb802
Showing 1 changed file with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,48 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy {
);

// Then we are going to slice the flows using the limit and offset
const reducedFlows: UniqueFlowEntity[] = sortedFlows.slice(
offset,
offset! + limit!
);

// Once the list of elements is reduced, we need to build the conditions
const searchConditions = this.buildConditions(reducedFlows, flowFilters);
let reducedFlows: UniqueFlowEntity[];
if (offset && limit) {
reducedFlows = sortedFlows.slice(offset, offset + limit);
} else {
reducedFlows = sortedFlows;
}

const orderByForFlow = { column: 'updatedAt', order: 'DESC' };

const flows = await this.flowService.getFlows({
models,
conditions: searchConditions,
orderBy: orderByForFlow,
});
let flows: FlowEntity[] = [];
const chunkSize = 5000;
if (reducedFlows.length > chunkSize) {
// We need to paginate over the searchConditions
// Then collect the flows

// 1. Generate an array of arrays with the chunkSize
const chunkedFlows = [];
for (let i = 0; i < reducedFlows.length; i += chunkSize) {
chunkedFlows.push(reducedFlows.slice(i, i + chunkSize));
}

// 2. Iterate over the array of arrays to generate the searchConditions
// And collect the flows
for (const chunk of chunkedFlows) {
const chunkSearchConditions = this.buildConditions(chunk, flowFilters);
const flowsChunk = await this.flowService.getFlows({
models,
conditions: chunkSearchConditions,
orderBy: orderByForFlow,
});
flows = [...flows, ...flowsChunk];
}
} else {
// Once the list of elements is reduced, we need to build the conditions
const searchConditions = this.buildConditions(reducedFlows, flowFilters);

flows = await this.flowService.getFlows({
models,
conditions: searchConditions,
orderBy: orderByForFlow,
});
}

return { flows, count };
}
Expand Down

0 comments on commit e7bb802

Please sign in to comment.