Skip to content

Commit

Permalink
UIREQ-960: TLRs check circulation rules before request is placed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy-Litvinenko committed Jul 6, 2023
1 parent 1012387 commit bde1f31
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* Cover InstanceInformation by jest/RTL tests. Refs UIREQ-950.
* Fix inconsistency in RTL/Jest tests. Refs UIREQ-979.
* Remove redundant ariaLabel prop. Refs UIREQ-972.
* TLRs check circulation rules before request is placed. Refs UIREQ-960.

## [8.0.2](https://github.com/folio-org/ui-requests/tree/v8.0.2) (2023-03-29)
[Full Changelog](https://github.com/folio-org/ui-requests/compare/v8.0.1...v8.0.2)
Expand Down
28 changes: 27 additions & 1 deletion src/routes/RequestsRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ export const buildHoldRecords = (records) => {
});
};

export const REQUEST_ERROR_MESSAGE_CODE = {
REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION: 'REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION',
};

export const REQUEST_ERROR_MESSAGE_TRANSLATION_KEYS = {
[REQUEST_ERROR_MESSAGE_CODE.REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION]: 'ui-requests.errors.requestNotAllowedForPatronTitleCombination',
};

export const getRequestErrorMessage = (error, intl) => {
const {
code = '',
message = '',
} = error;

return code && REQUEST_ERROR_MESSAGE_TRANSLATION_KEYS[code]
? intl.formatMessage({ id: REQUEST_ERROR_MESSAGE_TRANSLATION_KEYS[code] })
: message;
};

class RequestsRoute extends React.Component {
static contextType = CalloutContext;

Expand Down Expand Up @@ -813,8 +832,15 @@ class RequestsRoute extends React.Component {
}

handleJsonError({ errors }) {
const {
intl,
} = this.props;
const errorMessages = [];
errors.forEach(({ message }) => errorMessages.push(message));

errors.forEach((error) => (
errorMessages.push(getRequestErrorMessage(error, intl))
));

this.setState({ errorMessage: errorMessages.join(';') });
}

Expand Down
49 changes: 49 additions & 0 deletions src/routes/RequestsRoute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { CommandList, defaultKeyboardShortcuts } from '@folio/stripes/components

import RequestsRoute, {
buildHoldRecords,
REQUEST_ERROR_MESSAGE_CODE,
REQUEST_ERROR_MESSAGE_TRANSLATION_KEYS,
getRequestErrorMessage,
} from './RequestsRoute';

import {
Expand Down Expand Up @@ -300,4 +303,50 @@ describe('RequestsRoute', () => {
expect(buildHoldRecords(records)).toEqual(expectedResult);
});
});

describe('getRequestErrorMessage', () => {
const formatMessage = jest.fn(({ id }) => id);
const intl = {
formatMessage,
};
const message = 'test message';

it('should have same count of code and translation keys', () => {
expect(Object.keys(REQUEST_ERROR_MESSAGE_CODE).length).toEqual(Object.keys(REQUEST_ERROR_MESSAGE_TRANSLATION_KEYS).length);
});

describe('should have translation key for each code', () => {
Object.keys(REQUEST_ERROR_MESSAGE_CODE).forEach((key) => {
it(`should have translation key for code: ${key}`, () => {
expect(!!REQUEST_ERROR_MESSAGE_TRANSLATION_KEYS[key]).toBeTruthy();
});
});
});

it('should return translation key for code', () => {
expect(getRequestErrorMessage({
code: REQUEST_ERROR_MESSAGE_CODE.REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION,
}, intl)).toEqual(REQUEST_ERROR_MESSAGE_TRANSLATION_KEYS.REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION);
});

it('should trigger formatMessage with correct props', () => {
getRequestErrorMessage({
code: REQUEST_ERROR_MESSAGE_CODE.REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION,
}, intl);

expect(formatMessage).toHaveBeenCalledWith({
id: REQUEST_ERROR_MESSAGE_TRANSLATION_KEYS.REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION,
});
});

it('should return message when code empty', () => {
expect(getRequestErrorMessage({
message,
}, intl)).toEqual(message);
});

it('should return default message when code and message empty', () => {
expect(getRequestErrorMessage({}, intl)).toEqual('');
});
});
});
1 change: 1 addition & 0 deletions translations/ui-requests/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"errors.unknown.requestQueueLabel": "Request queue error",
"errors.unknown.requestQueueBody": "Request queue reorder failed",
"errors.closingAlreadyClosedRequest": "Error: The Request has already been closed.",
"errors.requestNotAllowedForPatronTitleCombination": "Hold requests are not allowed for this patron and title combination",

"actions.label": "Actions",
"actions.edit": "Edit",
Expand Down

0 comments on commit bde1f31

Please sign in to comment.