Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIREC-307 Expect "Unreceivable" pieces on the full screen form #485

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Delay claim action for piece record. Refs UIREC-303.
* Send claim action for piece record. Refs UIREC-304.
* Align the display of fields in full screen receiving view and piece edit form. Refs UIREC-296.
* Expect "Unreceivable" pieces on the full screen form. Refs UIREC-307.

## [4.0.0](https://github.com/folio-org/ui-receiving/tree/v4.0.0) (2023-10-12)
[Full Changelog](https://github.com/folio-org/ui-receiving/compare/v3.0.0...v4.0.0)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"subPermissions": [
"acquisition.piece.events.get",
"orders.check-in.collection.post",
"orders.expect.collection.post",
"orders.receiving.collection.post",
"settings.receiving.enabled",
"ui-receiving.basic.view",
Expand Down
1 change: 1 addition & 0 deletions src/common/constants/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const ACQ_AUDIT_API = 'audit-data/acquisition';
export const CHECKIN_API = 'orders/check-in';
export const EXPECT_API = 'orders/expect';
export const HOLDINGS_API = 'holdings-storage/holdings';
export const PIECE_AUDIT_API = `${ACQ_AUDIT_API}/piece`;
export const TITLES_API = 'orders/titles';
Expand Down
34 changes: 32 additions & 2 deletions src/common/hooks/usePiecesExpect/usePiecesExpect.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import { useMutation } from 'react-query';

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

import { EXPECT_API } from '../../constants';

export const usePiecesExpect = () => {
const ky = useOkapiKy();

const { isLoading, mutateAsync } = useMutation({
mutationFn: (_pieces) => {
return Promise.reject(new Error('TODO: UIREC-307'));
mutationFn: (pieces) => {
const selectedPieces = pieces
.filter(({ checked }) => checked === true)
.map(piece => ({
id: piece.id,
comment: piece.comment,
}));

const json = {
toBeExpected: [{
poLineId: pieces[0]?.poLineId,
expected: selectedPieces.length,
expectPieces: selectedPieces,
}],
totalRecords: selectedPieces.length,
};

return ky.post(EXPECT_API, { json })
.json()
.then(({ receivingResults }) => {
if (receivingResults?.some(({ processedWithError }) => processedWithError > 0)) {
return Promise.reject(receivingResults);
}

return receivingResults;
});
},
});

Expand Down
63 changes: 63 additions & 0 deletions src/common/hooks/usePiecesExpect/usePiecesExpect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { renderHook } from '@folio/jest-config-stripes/testing-library/react';
import {
QueryClient,
QueryClientProvider,
} from 'react-query';

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

import { EXPECT_API } from '../../constants';
import { usePiecesExpect } from './usePiecesExpect';

const pieceValues = {
id: 'pieceId',
holdingId: 'holdingId',
caption: 'v1',
comment: '123',
};

const kyMock = {
post: jest.fn(() => ({
json: () => Promise.resolve({ receivingResults: [{ processedWithError: 0 }] }),
})),
};

const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

describe('usePiecesExpect', () => {
beforeEach(() => {
kyMock.post.mockClear();
useOkapiKy
.mockClear()
.mockReturnValue(kyMock);
});

it('should call expect pieces API', async () => {
const { result } = renderHook(() => usePiecesExpect(), { wrapper });

await result.current.expectPieces([pieceValues]);

expect(kyMock.post).toHaveBeenCalledWith(EXPECT_API, expect.anything());
});

it('should reject if there are results with errors', async () => {
const receivingResults = [{ processedWithError: 1 }];

kyMock.post.mockImplementationOnce(() => ({
json: () => Promise.resolve({ receivingResults }),
}));

const { result } = renderHook(() => usePiecesExpect(), { wrapper });

try {
await result.current.expectPieces([pieceValues]);
} catch (error) {
expect(error).toEqual(receivingResults);
}
});
});
Loading