Skip to content

Commit

Permalink
UIREC-427 Chunk HTTP requests to get pieces 'requests' by piece IDs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Jan 6, 2025
1 parent deacf55 commit 39d6f01
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change history for ui-receiving

## 6.1.0 (IN PROGRESS)
## 7.0.0 (IN PROGRESS)

* Chunk HTTP requests to get pieces "requests" by piece IDs. Refs UIREC-427.

## [6.0.5](https://github.com/folio-org/ui-receiving/tree/v6.0.5) (2024-12-13)
[Full Changelog](https://github.com/folio-org/ui-receiving/compare/v6.0.4...v6.0.5)
Expand Down
3 changes: 3 additions & 0 deletions src/common/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ export const BATCH_IDENTIFIER_TYPE = {
instanceHrid: 'instanceHrid',
itemBarcode: 'itemBarcode',
};

export const CHUNK_SIZE = 25;
export const MAX_PARALLEL_REQUESTS = 5;
37 changes: 28 additions & 9 deletions src/common/hooks/usePaginatedPieces/util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import chunk from 'lodash/chunk';

import {
batchRequest,
ITEMS_API,
} from '@folio/stripes-acq-components';

import {
CHUNK_SIZE,
MAX_PARALLEL_REQUESTS,
PIECE_REQUESTS_API,
TENANT_ITEMS_API,
} from '../../constants';
Expand Down Expand Up @@ -38,15 +42,30 @@ export const fetchConsortiumPieceItems = (ky, { pieces }) => {
.then(({ tenantItems }) => tenantItems.map(({ item, tenantId }) => ({ tenantId, ...item })));
};

export const fetchLocalPieceRequests = (ky, { pieces }) => {
if (!pieces.length) {
return Promise.resolve([]);
export const fetchLocalPieceRequests = async (ky, { pieces }) => {
const results = [];

if (!pieces?.length) {
return Promise.resolve(results);
}

return ky
.get(PIECE_REQUESTS_API, {
searchParams: buildPieceRequestsSearchParams(pieces),
})
.json()
.then(({ circulationRequests }) => circulationRequests);
const pieceChunks = chunk(pieces, CHUNK_SIZE);
const pieceChunksGroups = chunk(pieceChunks, MAX_PARALLEL_REQUESTS);

for (const group of pieceChunksGroups) {
const responses = await Promise.all(
group.map(async (_chunk) => {
return ky.get(
PIECE_REQUESTS_API,
{ searchParams: buildPieceRequestsSearchParams(_chunk) },
)
.json()
.then(({ circulationRequests }) => circulationRequests);
}),
);

results.push(...responses);
}

return results.flat();
};
31 changes: 27 additions & 4 deletions src/common/hooks/usePiecesRequests/usePiecesRequests.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import chunk from 'lodash/chunk';
import { useQuery } from 'react-query';

import {
useNamespace,
useOkapiKy,
} from '@folio/stripes/core';

import { PIECE_REQUESTS_API } from '../../constants';
import {
CHUNK_SIZE,
MAX_PARALLEL_REQUESTS,
PIECE_REQUESTS_API,
} from '../../constants';
import { buildPieceRequestsSearchParams } from '../../utils';

const DEFAULT_DATA = [];
Expand All @@ -26,10 +31,28 @@ export const usePiecesRequests = (pieces = [], options = {}) => {
isLoading,
} = useQuery({
queryKey: [namespace, tenantId, pieces],
queryFn: ({ signal }) => {
const searchParams = buildPieceRequestsSearchParams(pieces);
queryFn: async ({ signal }) => {
const chunks = chunk(pieces, CHUNK_SIZE);
const chunkGroups = chunk(chunks, MAX_PARALLEL_REQUESTS);

const initialAccumulator = { circulationRequests: [], totalRecords: 0 };

for (const group of chunkGroups) {
const responses = await Promise.all(
group.map(async (_chunk) => {
const searchParams = buildPieceRequestsSearchParams(_chunk);

return ky.get(PIECE_REQUESTS_API, { searchParams, signal }).json();
}),
);

responses.forEach((response) => {
initialAccumulator.circulationRequests.push(...response.circulationRequests);
initialAccumulator.totalRecords += response.totalRecords;
});
}

return ky.get(PIECE_REQUESTS_API, { searchParams, signal }).json();
return initialAccumulator;
},
enabled: enabled && Boolean(pieces.length),
...queryOptions,
Expand Down

0 comments on commit 39d6f01

Please sign in to comment.