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

UIOR-1281 Applied 'Strategies' approach for error handling of order updates #1653

Merged
merged 3 commits into from
Oct 7, 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 @@ -34,6 +34,7 @@
* ECS - handle error message appears when user without appropriate affiliation is trying to edit/open POL with location in such affiliation. Refs UIOR-1307.
* UX: Removal of hyphen in Reexport and add refresh icon. UIOR-1246.
* Improve the error message when attempting to open an order when no budget for the fiscal year exists for the fund specified in "Fund distribution". Refs UIOR-1313.
* Applied "Strategies" approach for error handling of order updates. Refs UIOR-1281.

## [6.0.4](https://github.com/folio-org/ui-orders/tree/v6.0.4) (2024-04-25)
[Full Changelog](https://github.com/folio-org/ui-orders/compare/v6.0.3...v6.0.4)
Expand Down
3 changes: 3 additions & 0 deletions src/components/Utils/order/handleErrorsStrategies/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { noBudgetForFiscalYearStrategy } from './noBudgetForFiscalYearStrategy';
export { noExpenseClassesStrategy } from './noExpenseClassesStrategy';
export { restrictedLocationViolationStrategy } from './restrictedLocationViolationStrategy';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ERROR_CODES } from '../../../../common/constants';

export const noBudgetForFiscalYearStrategy = ({ callout }) => {
const handle = (errorsContainer) => {
let fundCodes;
const error = errorsContainer.getError();
const fiscalYearCode = error.getParameter('fiscalYearCode');

try {
fundCodes = JSON.parse(error.getParameter('fundCodes')).join(', ');
} catch {
fundCodes = error.getParameter('fundCodes');
}

callout.sendCallout({
messageId: `ui-orders.errors.${ERROR_CODES[error.code]}`,
type: 'error',
values: {
fiscalYearCode,
fundCodes,
},
});
};

return { handle };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const noExpenseClassesStrategy = ({ callout }) => {
const handle = (errorsContainer) => {
const fundCode = errorsContainer.getError().getParameter('fundCode');
const expenseClassName = errorsContainer.getError().getParameter('expenseClassName');

callout.sendCallout({
messageId: `ui-orders.errors.${errorsContainer.code}`,
type: 'error',
values: { fundCode, expenseClassName },
});
};

return { handle };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const restrictedLocationViolationStrategy = ({ callout }) => {
const handle = (errorsContainer) => {
const polNumber = errorsContainer.getError().getParameter('poLineNumber');

callout.sendCallout({
messageId: 'ui-orders.errors.openOrder.fundLocationRestrictionViolation',
type: 'error',
values: { polNumber },
});
};

return { handle };
};
58 changes: 20 additions & 38 deletions src/components/Utils/order/showUpdateOrderError.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import get from 'lodash/get';
import React from 'react';
import { FormattedMessage } from 'react-intl';
import { get } from 'lodash';

import { ResponseErrorsContainer } from '@folio/stripes-acq-components';

import { ERROR_CODES } from '../../../common/constants';
import {
noBudgetForFiscalYearStrategy,
noExpenseClassesStrategy,
restrictedLocationViolationStrategy,
} from './handleErrorsStrategies';

const POL_NUMBER_KEY = 'poLineNumber';

const showMessage = (callout, code, error, path) => {
const title = get(error, 'errors.0.parameters.0.value', '');
const showMessage = (callout, code, errors, path) => {
const title = get(errors.getError().parameters, '0.value', '');

callout.sendCallout({
type: 'error',
Expand All @@ -28,15 +35,9 @@ const showUpdateOrderError = async (
genericCode = ERROR_CODES.orderGenericError1,
toggleDeletePieces = null,
) => {
let error;

try {
error = await response.clone().json();
} catch (parsingException) {
error = response;
}
const { handler } = await ResponseErrorsContainer.create(response);

const errorCode = get(error, 'errors.0.code');
const errorCode = handler.getError().code;
const code = get(ERROR_CODES, errorCode, genericCode);

switch (code) {
Expand All @@ -49,7 +50,7 @@ const showUpdateOrderError = async (
case ERROR_CODES.accessProviderIsInactive:
case ERROR_CODES.accessProviderNotFound: {
let errors =
get(error, 'errors.0.parameters', [])
handler.getError().parameters
.filter(({ key }) => key === POL_NUMBER_KEY)
.map(({ value }) => ({ code, poLineNumber: value }));

Expand All @@ -62,52 +63,33 @@ const showUpdateOrderError = async (
break;
}
case ERROR_CODES.missingInstanceStatus: {
showMessage(callout, code, error, 'instanceStatusTypes');
showMessage(callout, code, handler, 'instanceStatusTypes');
break;
}
case ERROR_CODES.missingInstanceType: {
showMessage(callout, code, error, 'resourcetypes');
showMessage(callout, code, handler, 'resourcetypes');
break;
}
case ERROR_CODES.missingLoanType: {
showMessage(callout, code, error, 'loantypes');
showMessage(callout, code, handler, 'loantypes');
break;
}
case ERROR_CODES.budgetExpenseClassNotFound: {
const fundCode = error?.errors?.[0]?.parameters?.find(({ key }) => key === 'fundCode')?.value;
const expenseClassName = error?.errors?.[0]?.parameters?.find(({ key }) => key === 'expenseClassName')?.value;

callout.sendCallout({
messageId: `ui-orders.errors.${code}`,
type: 'error',
values: { fundCode, expenseClassName },
});
handler.handle(noExpenseClassesStrategy({ callout }));
break;
}
case ERROR_CODES.fundCannotBePaid: {
const fundCodes = error?.errors?.[0]?.parameters?.find(({ key }) => key === 'finance.funds')?.value;
const fundCodes = handler.getError().getParameter('finance.funds');

callout.sendCallout({ messageId: `ui-orders.errors.${ERROR_CODES[code]}`, type: 'error', values: { fundCodes } });
break;
}
case ERROR_CODES.fundLocationRestrictionViolation: {
const polNumber = error?.errors?.[0]?.parameters?.find(({ key }) => key === 'poLineNumber')?.value;

callout.sendCallout({
messageId: 'ui-orders.errors.openOrder.fundLocationRestrictionViolation',
type: 'error',
values: { polNumber },
});
handler.handle(restrictedLocationViolationStrategy({ callout }));
break;
}
case ERROR_CODES.budgetNotFoundForFiscalYear: {
const fundCodes = error?.errors?.[0]?.parameters?.find(({ key }) => key === 'fundCodes')?.value;

callout.sendCallout({
messageId: `ui-orders.errors.${ERROR_CODES[code]}`,
type: 'error',
values: { fundCodes: JSON.parse(fundCodes)?.join(', ') },
});
handler.handle(noBudgetForFiscalYearStrategy({ callout }));
break;
}
default: {
Expand Down
104 changes: 50 additions & 54 deletions src/components/Utils/order/showUpdateOrderError.test.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,91 @@
import showUpdateOrderError from './showUpdateOrderError';

const getResponseMock = ({ code, message, parameters }) => {
return {
clone: jest.fn().mockReturnThis(),
json: jest.fn().mockResolvedValue({
errors: [{
code,
parameters,
message,
}],
}),
status: 400,
};
};

const params = {
response: {
errors: [{
code: 'errorCode',
}],
},
response: getResponseMock({ code: 'genericError' }),
callout: {
sendCallout: jest.fn(),
},
openModal: jest.fn(),
};

describe('showUpdateOrderError', () => {
it('should handle error and open modal', () => {
const response = {
errors: [{
code: 'vendorIsInactive',
}],
};
it('should handle error and open modal', async () => {
const response = getResponseMock({ code: 'vendorIsInactive' });

showUpdateOrderError(response, params.callout, params.openModal);
await showUpdateOrderError(response, params.callout, params.openModal);

expect(params.openModal).toHaveBeenCalled();
});

it('should handle error and show message', () => {
const response = {
errors: [{
code: 'missingInstanceStatus',
}],
};
it('should handle error and show message', async () => {
const response = getResponseMock({ code: 'missingInstanceStatus' });

showUpdateOrderError(response, params.callout, params.openModal);
await showUpdateOrderError(response, params.callout, params.openModal);

expect(params.callout.sendCallout).toHaveBeenCalled();
});

it('should handle `fundLocationRestrictionViolation` error and show message', () => {
const response = {
errors: [{
code: 'fundLocationRestrictionViolation',
parameters: [{
key: 'poLineNumber',
value: 'value',
}],
it('should handle `fundLocationRestrictionViolation` error and show message', async () => {
const response = getResponseMock({
code: 'fundLocationRestrictionViolation',
parameters: [{
key: 'poLineNumber',
value: 'value',
}],
};
});

showUpdateOrderError(response, params.callout, params.openModal);
await showUpdateOrderError(response, params.callout, params.openModal);

expect(params.callout.sendCallout).toHaveBeenCalled();
});

it('should handle `budgetExpenseClassNotFound` error and show message', () => {
const response = {
errors: [{
code: 'budgetExpenseClassNotFound',
parameters: [{
key: 'fundCode',
value: 'value',
}, {
key: 'expenseClassName',
value: 'value',
}],
it('should handle `budgetExpenseClassNotFound` error and show message', async () => {
const response = getResponseMock({
code: 'budgetExpenseClassNotFound',
parameters: [{
key: 'fundCode',
value: 'value',
}, {
key: 'expenseClassName',
value: 'value',
}],
};
});

showUpdateOrderError(response, params.callout, params.openModal);
await showUpdateOrderError(response, params.callout, params.openModal);

expect(params.callout.sendCallout).toHaveBeenCalled();
});

it('should handle `budgetNotFoundForFiscalYear` error and show message', () => {
const response = {
errors: [{
code: 'budgetNotFoundForFiscalYear',
parameters: [{
key: 'fundCodes',
value: '[1,2]',
}],
it('should handle `budgetNotFoundForFiscalYear` error and show message', async () => {
const response = getResponseMock({
code: 'budgetNotFoundForFiscalYear',
parameters: [{
key: 'fundCodes',
value: '[1,2]',
}],
};
});

showUpdateOrderError(response, params.callout, params.openModal);
await showUpdateOrderError(response, params.callout, params.openModal);

expect(params.callout.sendCallout).toHaveBeenCalled();
});

it('should handle default error case', () => {
showUpdateOrderError(...Object.values(params));
it('should handle default error case', async () => {
await showUpdateOrderError(...Object.values(params));

expect(params.callout.sendCallout).toHaveBeenCalled();
});
Expand Down
2 changes: 1 addition & 1 deletion translations/ui-orders/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"errors.accessProviderNotFound": "POL {poLineNumber} is using an Access Provider that is inactive or has been removed, please select an active Access Provider to continue.",
"errors.budgetExpenseClassNotFound": "<b>{expenseClassName}</b> expense class not found on <b>{fundCode}</b> Fund",
"errors.budgetIsInactive": "Cannot create encumbrance from the not active budget",
"errors.budgetNotFoundForFiscalYear": "Order cannot be opened because there is no current budget for fund <b>{fundCodes}</b>.",
"errors.budgetNotFoundForFiscalYear": "Order cannot be opened because there is no current budget for fund(s) <b>{fundCodes}</b> for fiscal year <b>{fiscalYearCode}</b>.",
"errors.budgetNotFoundForTransaction": "One or more fund distribution(s) on this order can not be encumbered, because there is no current budget",
"errors.clone.error": "The purchase order was not cloned",
"errors.costAdditionalCostInvalid": "Cost's additional cost must be positive number",
Expand Down
Loading