@openmrs/esm-stock-management-app 3.3.2-pre.608 → 3.3.2-pre.612
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/1302.js +1 -0
- package/dist/1302.js.map +1 -0
- package/dist/3629.js +1 -0
- package/dist/3629.js.map +1 -0
- package/dist/4486.js +1 -1
- package/dist/4486.js.map +1 -1
- package/dist/{5838.css.map → 4590.css.map} +1 -1
- package/dist/4590.js +1 -0
- package/dist/4590.js.map +1 -0
- package/dist/5117.js +1 -1
- package/dist/5117.js.map +1 -1
- package/dist/7193.js +2 -2
- package/dist/7193.js.map +1 -1
- package/dist/9396.js +1 -1
- package/dist/9396.js.map +1 -1
- package/dist/openmrs-esm-stock-management-app.js +2 -2
- package/dist/routes.json +1 -1
- package/package.json +1 -1
- package/src/stock-items/add-stock-item/stock-item-rules/add-stock-rules.component.tsx +29 -3
- package/src/stock-items/add-stock-item/stock-item-rules/delete-stock-rule.modal.tsx +12 -6
- package/src/stock-items/add-stock-item/stock-item-rules/stock-item-rules.component.tsx +30 -11
- package/src/stock-items/add-stock-item/stock-item-rules/stock-item-rules.resource.ts +12 -3
- package/src/stock-items/add-stock-item/stock-item-rules/stock-item-rules.test.tsx +320 -0
- package/src/stock-items/add-stock-item/stock-item-rules/stock-rule-actions-menu.component.test.tsx +1 -0
- package/src/stock-items/add-stock-item/stock-item-rules/stock-rule-actions-menu.component.tsx +5 -2
- package/src/stock-items/add-stock-item/stock-item-rules/stock-rule.schema.ts +56 -0
- package/src/stock-items/add-stock-item/stock-item-rules/stock-rules-filter.component.tsx +57 -25
- package/translations/en.json +11 -1
- package/dist/5838.js +0 -1
- package/dist/5838.js.map +0 -1
- package/dist/9457.js +0 -1
- package/dist/9457.js.map +0 -1
- /package/dist/{5838.css → 4590.css} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { type ChangeEvent, useCallback, useEffect, useState } from 'react';
|
|
1
|
+
import React, { type ChangeEvent, useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
2
|
import classNames from 'classnames';
|
|
3
3
|
import { useTranslation } from 'react-i18next';
|
|
4
4
|
import {
|
|
@@ -22,6 +22,8 @@ import {
|
|
|
22
22
|
type Workspace2DefinitionProps,
|
|
23
23
|
} from '@openmrs/esm-framework';
|
|
24
24
|
import { createOrUpdateStockRule } from './stock-rules.resource';
|
|
25
|
+
import { createStockRuleSchema, getStockRuleFieldErrors, type StockRuleFieldErrors } from './stock-rule.schema';
|
|
26
|
+
import { extractErrorMessagesFromResponse } from '../../../constants';
|
|
25
27
|
import { ResourceRepresentation } from '../../../core/api/api';
|
|
26
28
|
import { type StockItemInventoryFilter, useStockItemPackagingUOMs } from '../../stock-items.resource';
|
|
27
29
|
import { type StockRule } from '../../../core/api/types/stockItem/StockRule';
|
|
@@ -69,6 +71,10 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
69
71
|
enabled: true,
|
|
70
72
|
...model,
|
|
71
73
|
});
|
|
74
|
+
const stockRuleSchema = useMemo(() => createStockRuleSchema(t), [t]);
|
|
75
|
+
const [submitted, setSubmitted] = useState(false);
|
|
76
|
+
// Errors show once the user has tried to save, and then update as they correct the form
|
|
77
|
+
const fieldErrors: StockRuleFieldErrors = submitted ? getStockRuleFieldErrors(stockRuleSchema, formModel) : {};
|
|
72
78
|
|
|
73
79
|
useEffect(() => {
|
|
74
80
|
if (model != null && Object.keys(model).length != 0) {
|
|
@@ -147,6 +153,10 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
147
153
|
const onFormSubmit = useCallback(
|
|
148
154
|
(event) => {
|
|
149
155
|
event.preventDefault();
|
|
156
|
+
setSubmitted(true);
|
|
157
|
+
if (Object.keys(getStockRuleFieldErrors(stockRuleSchema, formModel)).length > 0) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
150
160
|
if (model) {
|
|
151
161
|
formModel.uuid = model.uuid;
|
|
152
162
|
}
|
|
@@ -174,13 +184,13 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
174
184
|
: t('errorAddingRule', 'Error adding a rule'),
|
|
175
185
|
kind: 'error',
|
|
176
186
|
isLowContrast: true,
|
|
177
|
-
subtitle: error
|
|
187
|
+
subtitle: extractErrorMessagesFromResponse(error).join(', '),
|
|
178
188
|
});
|
|
179
189
|
},
|
|
180
190
|
)
|
|
181
191
|
.catch();
|
|
182
192
|
},
|
|
183
|
-
[formModel, model, t, stockItemUuid, closeWorkspace, handleMutate],
|
|
193
|
+
[formModel, model, t, stockItemUuid, closeWorkspace, handleMutate, stockRuleSchema],
|
|
184
194
|
);
|
|
185
195
|
|
|
186
196
|
return (
|
|
@@ -195,6 +205,8 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
195
205
|
labelText={t('location', 'Location')}
|
|
196
206
|
id="location"
|
|
197
207
|
disabled={Boolean(model)}
|
|
208
|
+
invalid={!!fieldErrors.locationUuid}
|
|
209
|
+
invalidText={fieldErrors.locationUuid}
|
|
198
210
|
value={formModel?.locationUuid ? formModel.locationUuid : ''}
|
|
199
211
|
onChange={onLocationChange}
|
|
200
212
|
>
|
|
@@ -205,6 +217,8 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
205
217
|
</Select>
|
|
206
218
|
<TextInput
|
|
207
219
|
id="name"
|
|
220
|
+
invalid={!!fieldErrors.name}
|
|
221
|
+
invalidText={fieldErrors.name}
|
|
208
222
|
type="text"
|
|
209
223
|
labelText={t('ruleName', 'Rule name')}
|
|
210
224
|
size="md"
|
|
@@ -217,6 +231,8 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
217
231
|
className="select-field"
|
|
218
232
|
labelText={t('quantityUnit', 'Quantity unit')}
|
|
219
233
|
id="quantityUnit"
|
|
234
|
+
invalid={!!fieldErrors.stockItemPackagingUOMUuid}
|
|
235
|
+
invalidText={fieldErrors.stockItemPackagingUOMUuid}
|
|
220
236
|
value={formModel?.stockItemPackagingUOMUuid ? formModel.stockItemPackagingUOMUuid : ''}
|
|
221
237
|
onChange={onQuantityUnitChange}
|
|
222
238
|
>
|
|
@@ -233,6 +249,8 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
233
249
|
</Select>
|
|
234
250
|
<TextInput
|
|
235
251
|
id="quantity"
|
|
252
|
+
invalid={!!fieldErrors.quantity}
|
|
253
|
+
invalidText={fieldErrors.quantity}
|
|
236
254
|
type="number"
|
|
237
255
|
labelText={t('quantityThreshold', 'Quantity threshold')}
|
|
238
256
|
size="md"
|
|
@@ -250,6 +268,8 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
250
268
|
className="select-field"
|
|
251
269
|
labelText={t('alertRole', 'Alert role')}
|
|
252
270
|
id="alertRole"
|
|
271
|
+
invalid={!!fieldErrors.alertRole}
|
|
272
|
+
invalidText={fieldErrors.alertRole}
|
|
253
273
|
value={formModel?.alertRole ? formModel.alertRole : ''}
|
|
254
274
|
onChange={onAlertRoleChange}
|
|
255
275
|
>
|
|
@@ -263,6 +283,8 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
263
283
|
className="select-field"
|
|
264
284
|
labelText={t('mailRole', 'Mail role')}
|
|
265
285
|
id="mailRole"
|
|
286
|
+
invalid={!!fieldErrors.mailRole}
|
|
287
|
+
invalidText={fieldErrors.mailRole}
|
|
266
288
|
value={formModel?.mailRole ? formModel.mailRole : ''}
|
|
267
289
|
onChange={onMailRoleChange}
|
|
268
290
|
>
|
|
@@ -273,6 +295,8 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
273
295
|
</Select>
|
|
274
296
|
<TextInput
|
|
275
297
|
id="evaluationFrequency"
|
|
298
|
+
invalid={!!fieldErrors.evaluationFrequency}
|
|
299
|
+
invalidText={fieldErrors.evaluationFrequency}
|
|
276
300
|
type="number"
|
|
277
301
|
labelText={t('evaluationFrequency', 'Frequency Check (Minutes)')}
|
|
278
302
|
size="md"
|
|
@@ -282,6 +306,8 @@ const StockRulesAddOrUpdate: React.FC<Workspace2DefinitionProps<AddStockRuleProp
|
|
|
282
306
|
/>
|
|
283
307
|
<TextInput
|
|
284
308
|
id="actionFrequency"
|
|
309
|
+
invalid={!!fieldErrors.actionFrequency}
|
|
310
|
+
invalidText={fieldErrors.actionFrequency}
|
|
285
311
|
type="number"
|
|
286
312
|
labelText={t('actionFrequency', 'Notification Frequency (Minutes)')}
|
|
287
313
|
size="md"
|
|
@@ -6,11 +6,12 @@ import styles from './delete-stock-rule.scss';
|
|
|
6
6
|
|
|
7
7
|
interface DeleteStockRuleModalProps {
|
|
8
8
|
uuid?: string;
|
|
9
|
+
ruleName?: string;
|
|
9
10
|
close: () => void;
|
|
10
11
|
onConfirmation: () => void;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
const DeleteStockRuleModal: React.FC<DeleteStockRuleModalProps> = ({ close, onConfirmation,
|
|
14
|
+
const DeleteStockRuleModal: React.FC<DeleteStockRuleModalProps> = ({ close, onConfirmation, ruleName }) => {
|
|
14
15
|
const { t } = useTranslation();
|
|
15
16
|
const handleCancel = () => close();
|
|
16
17
|
const handleDelete = () => onConfirmation?.();
|
|
@@ -22,11 +23,16 @@ const DeleteStockRuleModal: React.FC<DeleteStockRuleModalProps> = ({ close, onCo
|
|
|
22
23
|
</ModalHeader>
|
|
23
24
|
<ModalBody>
|
|
24
25
|
<p className={styles.bodyText}>
|
|
25
|
-
{
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
{ruleName
|
|
27
|
+
? t(
|
|
28
|
+
'deleteNamedRuleConfirmationText',
|
|
29
|
+
"Are you sure you want to delete the stock rule {{name}}? This action can't be undone.",
|
|
30
|
+
{ name: ruleName },
|
|
31
|
+
)
|
|
32
|
+
: t(
|
|
33
|
+
'deleteRuleConfirmationText',
|
|
34
|
+
`Are you sure you want to delete this stock rule? This action can't be undone.`,
|
|
35
|
+
)}
|
|
30
36
|
</p>
|
|
31
37
|
</ModalBody>
|
|
32
38
|
<ModalFooter>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useMemo } from 'react';
|
|
1
|
+
import React, { useMemo, useState } from 'react';
|
|
2
2
|
import { useTranslation } from 'react-i18next';
|
|
3
3
|
import {
|
|
4
4
|
DataTable,
|
|
@@ -25,7 +25,7 @@ import { type StockRule } from '../../../core/api/types/stockItem/StockRule';
|
|
|
25
25
|
import { useStockItemRules } from './stock-item-rules.resource';
|
|
26
26
|
import AddStockRuleActionButton from './add-stock-rule-button.component';
|
|
27
27
|
import StockRuleActionsMenu from './stock-rule-actions-menu.component';
|
|
28
|
-
import StockRulesFilter from './stock-rules-filter.component';
|
|
28
|
+
import StockRulesFilter, { type StockRuleStatusFilter } from './stock-rules-filter.component';
|
|
29
29
|
import styles from '../../stock-items-table.scss';
|
|
30
30
|
import rulesStyles from './stock-item-rules.scss';
|
|
31
31
|
|
|
@@ -38,6 +38,8 @@ interface StockItemRulesProps {
|
|
|
38
38
|
|
|
39
39
|
const StockItemRules: React.FC<StockItemRulesProps> = ({ stockItemUuid, model, canEdit = true }) => {
|
|
40
40
|
const { t } = useTranslation();
|
|
41
|
+
const [selectedLocationUuid, setSelectedLocationUuid] = useState('');
|
|
42
|
+
const [selectedStatus, setSelectedStatus] = useState<StockRuleStatusFilter>('all');
|
|
41
43
|
const {
|
|
42
44
|
isLoading,
|
|
43
45
|
items,
|
|
@@ -49,11 +51,15 @@ const StockItemRules: React.FC<StockItemRulesProps> = ({ stockItemUuid, model, c
|
|
|
49
51
|
mutate,
|
|
50
52
|
setPageSize,
|
|
51
53
|
goTo,
|
|
52
|
-
} = useStockItemRules(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
} = useStockItemRules(
|
|
55
|
+
{
|
|
56
|
+
v: ResourceRepresentation.Default,
|
|
57
|
+
totalCount: true,
|
|
58
|
+
stockItemUuid: stockItemUuid,
|
|
59
|
+
locationUuid: selectedLocationUuid || undefined,
|
|
60
|
+
},
|
|
61
|
+
selectedStatus,
|
|
62
|
+
);
|
|
57
63
|
|
|
58
64
|
const handleRefresh = () => {
|
|
59
65
|
void mutate();
|
|
@@ -76,18 +82,19 @@ const StockItemRules: React.FC<StockItemRulesProps> = ({ stockItemUuid, model, c
|
|
|
76
82
|
nextActionDate: formatDisplayDate(stockRule?.nextActionDate),
|
|
77
83
|
enabled: stockRule.enabled ? 'Yes' : 'No',
|
|
78
84
|
actions: (
|
|
79
|
-
<StockRuleActionsMenu data={
|
|
85
|
+
<StockRuleActionsMenu data={stockRule} stockItemUuid={stockRule.stockItemUuid} onDeleteSuccess={mutate} />
|
|
80
86
|
),
|
|
81
87
|
}));
|
|
82
88
|
}, [items, mutate]);
|
|
83
89
|
|
|
84
|
-
|
|
90
|
+
// Keep the table mounted while a location filter loads, so the filters keep their values
|
|
91
|
+
if (isLoading && !selectedLocationUuid) {
|
|
85
92
|
return <DataTableSkeleton role="progressbar" />;
|
|
86
93
|
}
|
|
87
94
|
|
|
88
95
|
return (
|
|
89
96
|
<div className={rulesStyles.tableOverride}>
|
|
90
|
-
<DataTable rows={tableRows} headers={tableHeaders} isSortable useZebraStyles>
|
|
97
|
+
<DataTable rows={tableRows ?? []} headers={tableHeaders} isSortable useZebraStyles>
|
|
91
98
|
{({ rows, headers, getHeaderProps, getTableProps, getRowProps, onInputChange }) => (
|
|
92
99
|
<TableContainer>
|
|
93
100
|
<TableToolbar
|
|
@@ -105,7 +112,19 @@ const StockItemRules: React.FC<StockItemRulesProps> = ({ stockItemUuid, model, c
|
|
|
105
112
|
alignItems: 'center',
|
|
106
113
|
}}
|
|
107
114
|
>
|
|
108
|
-
<StockRulesFilter
|
|
115
|
+
<StockRulesFilter
|
|
116
|
+
stockItemUuid={stockItemUuid}
|
|
117
|
+
selectedLocationUuid={selectedLocationUuid}
|
|
118
|
+
selectedStatus={selectedStatus}
|
|
119
|
+
onLocationChange={(locationUuid) => {
|
|
120
|
+
setSelectedLocationUuid(locationUuid);
|
|
121
|
+
goTo(1);
|
|
122
|
+
}}
|
|
123
|
+
onStatusChange={(status) => {
|
|
124
|
+
setSelectedStatus(status);
|
|
125
|
+
goTo(1);
|
|
126
|
+
}}
|
|
127
|
+
/>
|
|
109
128
|
</div>
|
|
110
129
|
<TableToolbarMenu>
|
|
111
130
|
<TableToolbarAction className={rulesStyles.toolbarMenuAction} onClick={handleRefresh}>
|
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import { useMemo, useState } from 'react';
|
|
2
2
|
import { type StockRuleFilter, useStockRules } from '../../stock-items.resource';
|
|
3
|
+
import { type StockRuleStatusFilter } from './stock-rules-filter.component';
|
|
3
4
|
import { usePagination } from '@openmrs/esm-framework';
|
|
4
5
|
|
|
5
|
-
export function useStockItemRules(filter: StockRuleFilter) {
|
|
6
|
+
export function useStockItemRules(filter: StockRuleFilter, status: StockRuleStatusFilter = 'all') {
|
|
6
7
|
const { items, isLoading, error, mutate } = useStockRules(filter);
|
|
7
8
|
|
|
9
|
+
const filteredItems = useMemo(() => {
|
|
10
|
+
const results = items?.results ?? [];
|
|
11
|
+
if (status === 'all') {
|
|
12
|
+
return results;
|
|
13
|
+
}
|
|
14
|
+
return results.filter((rule) => rule.enabled === (status === 'enabled'));
|
|
15
|
+
}, [items?.results, status]);
|
|
16
|
+
|
|
8
17
|
const pageSizes = [10, 20, 30, 40, 50];
|
|
9
18
|
const [currentPageSize, setPageSize] = useState(10);
|
|
10
|
-
const { goTo, results: paginatedItems, currentPage } = usePagination(
|
|
19
|
+
const { goTo, results: paginatedItems, currentPage } = usePagination(filteredItems, currentPageSize);
|
|
11
20
|
|
|
12
21
|
const tableHeaders = useMemo(
|
|
13
22
|
() => [
|
|
@@ -54,7 +63,7 @@ export function useStockItemRules(filter: StockRuleFilter) {
|
|
|
54
63
|
|
|
55
64
|
return {
|
|
56
65
|
items: paginatedItems,
|
|
57
|
-
totalItems:
|
|
66
|
+
totalItems: filteredItems.length,
|
|
58
67
|
currentPage,
|
|
59
68
|
currentPageSize,
|
|
60
69
|
paginatedItems,
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
6
|
+
import userEvent from '@testing-library/user-event';
|
|
7
|
+
import { showModal, showSnackbar, useWorkspace2Context } from '@openmrs/esm-framework';
|
|
8
|
+
import { mockWorkspace2Props } from '@mocks';
|
|
9
|
+
import routes from '../../../routes.json';
|
|
10
|
+
import { type StockRule } from '../../../core/api/types/stockItem/StockRule';
|
|
11
|
+
import { deleteStockRule, useStockItemPackagingUOMs } from '../../stock-items.resource';
|
|
12
|
+
import { useRoles, useStockTagLocations } from '../../../stock-lookups/stock-lookups.resource';
|
|
13
|
+
import { createOrUpdateStockRule, useStockRules } from './stock-rules.resource';
|
|
14
|
+
import { useStockItemRules } from './stock-item-rules.resource';
|
|
15
|
+
import DeleteStockRuleModal from './delete-stock-rule.modal';
|
|
16
|
+
import StockItemRules from './stock-item-rules.component';
|
|
17
|
+
import StockRulesAddOrUpdate from './add-stock-rules.component';
|
|
18
|
+
import StockRuleActionsMenu from './stock-rule-actions-menu.component';
|
|
19
|
+
|
|
20
|
+
const { mockHandleMutate } = vi.hoisted(() => ({ mockHandleMutate: vi.fn() }));
|
|
21
|
+
|
|
22
|
+
vi.mock('../../stock-items.resource', () => ({
|
|
23
|
+
deleteStockRule: vi.fn(),
|
|
24
|
+
useStockItemPackagingUOMs: vi.fn(),
|
|
25
|
+
}));
|
|
26
|
+
vi.mock('./stock-rules.resource', () => ({ createOrUpdateStockRule: vi.fn(), useStockRules: vi.fn() }));
|
|
27
|
+
vi.mock('./stock-item-rules.resource', () => ({ useStockItemRules: vi.fn() }));
|
|
28
|
+
vi.mock('../../../stock-lookups/stock-lookups.resource', () => ({ useRoles: vi.fn(), useStockTagLocations: vi.fn() }));
|
|
29
|
+
vi.mock('../../../utils', () => ({ useHandleMutate: () => mockHandleMutate }));
|
|
30
|
+
vi.mock('./add-stock-rule-button.component', () => ({ default: () => null }));
|
|
31
|
+
|
|
32
|
+
const rule = (overrides: Partial<StockRule>) =>
|
|
33
|
+
({
|
|
34
|
+
uuid: 'rule-uuid',
|
|
35
|
+
stockItemUuid: 'item-uuid',
|
|
36
|
+
name: 'Advil alert',
|
|
37
|
+
locationUuid: 'main-store',
|
|
38
|
+
locationName: 'Main Store',
|
|
39
|
+
quantity: 10,
|
|
40
|
+
packagingUomName: 'Box',
|
|
41
|
+
enabled: true,
|
|
42
|
+
evaluationFrequency: 30,
|
|
43
|
+
actionFrequency: 3600,
|
|
44
|
+
...overrides,
|
|
45
|
+
} as StockRule);
|
|
46
|
+
|
|
47
|
+
const mainStoreRule = rule({});
|
|
48
|
+
const pharmacyRule = rule({
|
|
49
|
+
uuid: 'rule-2',
|
|
50
|
+
name: 'Pharmacy alert',
|
|
51
|
+
locationUuid: 'main-pharmacy',
|
|
52
|
+
locationName: 'Main Pharmacy',
|
|
53
|
+
enabled: false,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('stock item rules', () => {
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
vi.mocked(useWorkspace2Context).mockReturnValue({ launchChildWorkspace: vi.fn() } as never);
|
|
59
|
+
vi.mocked(deleteStockRule).mockResolvedValue({} as never);
|
|
60
|
+
vi.mocked(useStockRules).mockReturnValue({ items: { results: [mainStoreRule, pharmacyRule] } } as never);
|
|
61
|
+
vi.mocked(useStockItemRules).mockImplementation(({ locationUuid }, status = 'all') => {
|
|
62
|
+
const filtered = [mainStoreRule, pharmacyRule]
|
|
63
|
+
.filter((r) => !locationUuid || r.locationUuid === locationUuid)
|
|
64
|
+
.filter((r) => status === 'all' || r.enabled === (status === 'enabled'));
|
|
65
|
+
return {
|
|
66
|
+
items: filtered,
|
|
67
|
+
isLoading: false,
|
|
68
|
+
tableHeaders: [
|
|
69
|
+
{ key: 'name', header: 'Name' },
|
|
70
|
+
{ key: 'enabled', header: 'Enabled?' },
|
|
71
|
+
],
|
|
72
|
+
currentPage: 1,
|
|
73
|
+
currentPageSize: 10,
|
|
74
|
+
pageSizes: [10],
|
|
75
|
+
totalItems: filtered.length,
|
|
76
|
+
mutate: mockHandleMutate,
|
|
77
|
+
setPageSize: vi.fn(),
|
|
78
|
+
goTo: vi.fn(),
|
|
79
|
+
} as never;
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('deleting a rule', () => {
|
|
84
|
+
it('registers the delete modal under the name the button opens', () => {
|
|
85
|
+
// index.ts can't be imported in a test, so check its source for the export routes.json points at
|
|
86
|
+
const indexSource = readFileSync(resolve(__dirname, '../../../index.ts'), 'utf8');
|
|
87
|
+
const registration = routes.modals.find((modal) => modal.name === 'delete-stock-rule-modal');
|
|
88
|
+
|
|
89
|
+
expect(registration?.component).toBe('deleteStockRuleModal');
|
|
90
|
+
expect(indexSource).toMatch(
|
|
91
|
+
/export const deleteStockRuleModal = getAsyncLifecycle\(\s*\(\) => import\('\.\/stock-items\/add-stock-item\/stock-item-rules\/delete-stock-rule\.modal'\)/,
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('asks for confirmation naming the rule, deletes it and refreshes the list', async () => {
|
|
96
|
+
const user = userEvent.setup();
|
|
97
|
+
const closeModal = vi.fn();
|
|
98
|
+
vi.mocked(showModal).mockReturnValue(closeModal);
|
|
99
|
+
render(
|
|
100
|
+
<StockRuleActionsMenu data={mainStoreRule} stockItemUuid="item-uuid" onDeleteSuccess={mockHandleMutate} />,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
await user.click(screen.getByRole('button', { name: /stock rule actions/i }));
|
|
104
|
+
const deleteItem = screen
|
|
105
|
+
.getAllByRole('menuitem', { hidden: true })
|
|
106
|
+
.find((el) => el.textContent?.includes('Delete Rule'));
|
|
107
|
+
await user.click(deleteItem);
|
|
108
|
+
|
|
109
|
+
const props = vi.mocked(showModal).mock.lastCall[1] as { ruleName: string; onConfirmation: () => void };
|
|
110
|
+
expect(vi.mocked(showModal).mock.lastCall[0]).toBe('delete-stock-rule-modal');
|
|
111
|
+
expect(props.ruleName).toBe('Advil alert');
|
|
112
|
+
|
|
113
|
+
props.onConfirmation();
|
|
114
|
+
|
|
115
|
+
await waitFor(() => expect(deleteStockRule).toHaveBeenCalledWith('rule-uuid'));
|
|
116
|
+
await waitFor(() => expect(mockHandleMutate).toHaveBeenCalledTimes(1));
|
|
117
|
+
expect(showSnackbar).toHaveBeenCalledWith(expect.objectContaining({ kind: 'success' }));
|
|
118
|
+
expect(closeModal).toHaveBeenCalled();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('names the rule in the confirmation', () => {
|
|
122
|
+
render(<DeleteStockRuleModal ruleName="Advil alert" close={vi.fn()} onConfirmation={vi.fn()} />);
|
|
123
|
+
|
|
124
|
+
expect(
|
|
125
|
+
screen.getByText("Are you sure you want to delete the stock rule Advil alert? This action can't be undone."),
|
|
126
|
+
).toBeInTheDocument();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('filters', () => {
|
|
131
|
+
const openAndPick = async (user: ReturnType<typeof userEvent.setup>, filter: string, option: string) => {
|
|
132
|
+
await user.click(screen.getByRole('combobox', { name: filter }));
|
|
133
|
+
await user.click(screen.getByRole('option', { name: option }));
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
it('filters the rules by location on the server', async () => {
|
|
137
|
+
const user = userEvent.setup();
|
|
138
|
+
render(<StockItemRules stockItemUuid="item-uuid" />);
|
|
139
|
+
expect(screen.getByText('Advil alert')).toBeInTheDocument();
|
|
140
|
+
expect(screen.getByText('Pharmacy alert')).toBeInTheDocument();
|
|
141
|
+
|
|
142
|
+
await openAndPick(user, 'Location', 'Main Pharmacy');
|
|
143
|
+
|
|
144
|
+
expect(useStockItemRules).toHaveBeenLastCalledWith(
|
|
145
|
+
expect.objectContaining({ locationUuid: 'main-pharmacy' }),
|
|
146
|
+
'all',
|
|
147
|
+
);
|
|
148
|
+
await waitFor(() => expect(screen.queryByText('Advil alert')).not.toBeInTheDocument());
|
|
149
|
+
expect(screen.getByText('Pharmacy alert')).toBeInTheDocument();
|
|
150
|
+
|
|
151
|
+
await openAndPick(user, 'Location', 'All locations');
|
|
152
|
+
expect(useStockItemRules).toHaveBeenLastCalledWith(expect.objectContaining({ locationUuid: undefined }), 'all');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('filters the rules by enabled state', async () => {
|
|
156
|
+
const user = userEvent.setup();
|
|
157
|
+
render(<StockItemRules stockItemUuid="item-uuid" />);
|
|
158
|
+
|
|
159
|
+
await openAndPick(user, 'Status', 'Disabled');
|
|
160
|
+
|
|
161
|
+
await waitFor(() => expect(screen.queryByText('Advil alert')).not.toBeInTheDocument());
|
|
162
|
+
expect(screen.getByText('Pharmacy alert')).toBeInTheDocument();
|
|
163
|
+
|
|
164
|
+
await openAndPick(user, 'Status', 'Enabled');
|
|
165
|
+
|
|
166
|
+
expect(await screen.findByText('Advil alert')).toBeInTheDocument();
|
|
167
|
+
await waitFor(() => expect(screen.queryByText('Pharmacy alert')).not.toBeInTheDocument());
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe('the rule form', () => {
|
|
172
|
+
beforeEach(() => {
|
|
173
|
+
vi.mocked(useStockTagLocations).mockReturnValue({
|
|
174
|
+
stockLocations: [{ id: 'main-store', name: 'Main Store' }],
|
|
175
|
+
} as never);
|
|
176
|
+
vi.mocked(useStockItemPackagingUOMs).mockReturnValue({
|
|
177
|
+
items: { results: [{ uuid: 'box-uuid', packagingUomName: 'Box' }] },
|
|
178
|
+
} as never);
|
|
179
|
+
vi.mocked(useRoles).mockReturnValue({ items: { results: [{ display: 'Inventory Manager' }] } } as never);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
const renderForm = () => render(<StockRulesAddOrUpdate {...mockWorkspace2Props({ stockItemUuid: 'item-uuid' })} />);
|
|
183
|
+
const save = (user: ReturnType<typeof userEvent.setup>) =>
|
|
184
|
+
user.click(screen.getByRole('button', { name: /save/i }));
|
|
185
|
+
|
|
186
|
+
const fillForm = async (user: ReturnType<typeof userEvent.setup>) => {
|
|
187
|
+
await user.selectOptions(screen.getByRole('combobox', { name: 'Location' }), 'Main Store');
|
|
188
|
+
await user.type(screen.getByRole('textbox', { name: 'Rule name' }), 'Advil alert');
|
|
189
|
+
await user.selectOptions(screen.getByRole('combobox', { name: 'Quantity unit' }), 'Box');
|
|
190
|
+
await user.type(screen.getByRole('spinbutton', { name: 'Quantity threshold' }), '10');
|
|
191
|
+
await user.selectOptions(screen.getByRole('combobox', { name: 'Alert role' }), 'Inventory Manager');
|
|
192
|
+
await user.selectOptions(screen.getByRole('combobox', { name: 'Mail role' }), 'Inventory Manager');
|
|
193
|
+
await user.type(screen.getByRole('spinbutton', { name: 'Frequency Check (Minutes)' }), '30');
|
|
194
|
+
await user.type(screen.getByRole('spinbutton', { name: 'Notification Frequency (Minutes)' }), '3600');
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
it('blocks an empty save and marks every required field', async () => {
|
|
198
|
+
const user = userEvent.setup();
|
|
199
|
+
renderForm();
|
|
200
|
+
|
|
201
|
+
await save(user);
|
|
202
|
+
|
|
203
|
+
expect(createOrUpdateStockRule).not.toHaveBeenCalled();
|
|
204
|
+
for (const message of [
|
|
205
|
+
'Location is required',
|
|
206
|
+
'Rule name is required',
|
|
207
|
+
'Quantity unit is required',
|
|
208
|
+
'Enter a quantity threshold greater than 0',
|
|
209
|
+
'Choose an alert role, a mail role or both',
|
|
210
|
+
'Enter a frequency check greater than 0',
|
|
211
|
+
'Enter a notification frequency greater than 0',
|
|
212
|
+
]) {
|
|
213
|
+
expect(screen.getByText(message)).toBeInTheDocument();
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('saves an existing alert-only rule when mailRole is null', async () => {
|
|
218
|
+
const user = userEvent.setup();
|
|
219
|
+
vi.mocked(createOrUpdateStockRule).mockResolvedValue({} as never);
|
|
220
|
+
render(
|
|
221
|
+
<StockRulesAddOrUpdate
|
|
222
|
+
{...mockWorkspace2Props({
|
|
223
|
+
stockItemUuid: 'item-uuid',
|
|
224
|
+
model: rule({
|
|
225
|
+
stockItemPackagingUOMUuid: 'box-uuid',
|
|
226
|
+
alertRole: 'Inventory Manager',
|
|
227
|
+
mailRole: null,
|
|
228
|
+
}),
|
|
229
|
+
})}
|
|
230
|
+
/>,
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
await save(user);
|
|
234
|
+
|
|
235
|
+
await waitFor(() => expect(createOrUpdateStockRule).toHaveBeenCalledTimes(1));
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('saves an existing mail-only rule when alertRole is null', async () => {
|
|
239
|
+
const user = userEvent.setup();
|
|
240
|
+
vi.mocked(createOrUpdateStockRule).mockResolvedValue({} as never);
|
|
241
|
+
render(
|
|
242
|
+
<StockRulesAddOrUpdate
|
|
243
|
+
{...mockWorkspace2Props({
|
|
244
|
+
stockItemUuid: 'item-uuid',
|
|
245
|
+
model: rule({
|
|
246
|
+
stockItemPackagingUOMUuid: 'box-uuid',
|
|
247
|
+
alertRole: null,
|
|
248
|
+
mailRole: 'Inventory Manager',
|
|
249
|
+
}),
|
|
250
|
+
})}
|
|
251
|
+
/>,
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
await save(user);
|
|
255
|
+
|
|
256
|
+
await waitFor(() => expect(createOrUpdateStockRule).toHaveBeenCalledTimes(1));
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('blocks saving and shows the translated message when both notification roles are null', async () => {
|
|
260
|
+
const user = userEvent.setup();
|
|
261
|
+
render(
|
|
262
|
+
<StockRulesAddOrUpdate
|
|
263
|
+
{...mockWorkspace2Props({
|
|
264
|
+
stockItemUuid: 'item-uuid',
|
|
265
|
+
model: rule({
|
|
266
|
+
stockItemPackagingUOMUuid: 'box-uuid',
|
|
267
|
+
alertRole: null,
|
|
268
|
+
mailRole: null,
|
|
269
|
+
}),
|
|
270
|
+
})}
|
|
271
|
+
/>,
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
await save(user);
|
|
275
|
+
|
|
276
|
+
expect(screen.getByText('Choose an alert role, a mail role or both')).toBeInTheDocument();
|
|
277
|
+
expect(createOrUpdateStockRule).not.toHaveBeenCalled();
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('rejects a threshold that is not greater than 0', async () => {
|
|
281
|
+
const user = userEvent.setup();
|
|
282
|
+
renderForm();
|
|
283
|
+
await fillForm(user);
|
|
284
|
+
await user.clear(screen.getByRole('spinbutton', { name: 'Quantity threshold' }));
|
|
285
|
+
await user.type(screen.getByRole('spinbutton', { name: 'Quantity threshold' }), '0');
|
|
286
|
+
|
|
287
|
+
await save(user);
|
|
288
|
+
|
|
289
|
+
expect(screen.getByText('Enter a quantity threshold greater than 0')).toBeInTheDocument();
|
|
290
|
+
expect(createOrUpdateStockRule).not.toHaveBeenCalled();
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("shows the server's reason when a save is refused", async () => {
|
|
294
|
+
const user = userEvent.setup();
|
|
295
|
+
vi.mocked(createOrUpdateStockRule).mockRejectedValue({
|
|
296
|
+
responseBody: { error: { message: 'User has no rights to update stock rule' } },
|
|
297
|
+
});
|
|
298
|
+
renderForm();
|
|
299
|
+
await fillForm(user);
|
|
300
|
+
|
|
301
|
+
await save(user);
|
|
302
|
+
|
|
303
|
+
await waitFor(() =>
|
|
304
|
+
expect(showSnackbar).toHaveBeenCalledWith(
|
|
305
|
+
expect.objectContaining({ kind: 'error', subtitle: 'User has no rights to update stock rule' }),
|
|
306
|
+
),
|
|
307
|
+
);
|
|
308
|
+
expect(createOrUpdateStockRule).toHaveBeenCalledWith(
|
|
309
|
+
expect.objectContaining({
|
|
310
|
+
locationUuid: 'main-store',
|
|
311
|
+
name: 'Advil alert',
|
|
312
|
+
stockItemPackagingUOMUuid: 'box-uuid',
|
|
313
|
+
quantity: 10,
|
|
314
|
+
evaluationFrequency: 30,
|
|
315
|
+
actionFrequency: 3600,
|
|
316
|
+
}),
|
|
317
|
+
);
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
});
|
package/src/stock-items/add-stock-item/stock-item-rules/stock-rule-actions-menu.component.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import React, { useCallback, useState } from 'react';
|
|
|
2
2
|
import { useTranslation } from 'react-i18next';
|
|
3
3
|
import { InlineLoading, OverflowMenu, OverflowMenuItem } from '@carbon/react';
|
|
4
4
|
import { showModal, showSnackbar, useWorkspace2Context } from '@openmrs/esm-framework';
|
|
5
|
+
import { extractErrorMessagesFromResponse } from '../../../constants';
|
|
5
6
|
import { type StockRule } from '../../../core/api/types/stockItem/StockRule';
|
|
6
7
|
import { deleteStockRule } from '../../stock-items.resource';
|
|
7
8
|
|
|
@@ -24,6 +25,8 @@ const StockRuleActionsMenu: React.FC<StockRuleActionsMenuProps> = ({ data, stock
|
|
|
24
25
|
const close = showModal('delete-stock-rule-modal', {
|
|
25
26
|
close: () => close(),
|
|
26
27
|
uuid: data.uuid,
|
|
28
|
+
// `name` is taken: the framework passes the modal's own name to every modal
|
|
29
|
+
ruleName: data.name,
|
|
27
30
|
onConfirmation: () => {
|
|
28
31
|
setDeletingRule(true);
|
|
29
32
|
deleteStockRule(data.uuid)
|
|
@@ -44,7 +47,7 @@ const StockRuleActionsMenu: React.FC<StockRuleActionsMenuProps> = ({ data, stock
|
|
|
44
47
|
title: t('errorDeletingRule', 'Error deleting a rule'),
|
|
45
48
|
kind: 'error',
|
|
46
49
|
isLowContrast: true,
|
|
47
|
-
subtitle: error
|
|
50
|
+
subtitle: extractErrorMessagesFromResponse(error).join(', '),
|
|
48
51
|
});
|
|
49
52
|
},
|
|
50
53
|
)
|
|
@@ -52,7 +55,7 @@ const StockRuleActionsMenu: React.FC<StockRuleActionsMenuProps> = ({ data, stock
|
|
|
52
55
|
close();
|
|
53
56
|
},
|
|
54
57
|
});
|
|
55
|
-
}, [data.uuid, onDeleteSuccess, t]);
|
|
58
|
+
}, [data.name, data.uuid, onDeleteSuccess, t]);
|
|
56
59
|
|
|
57
60
|
if (deletingRule) {
|
|
58
61
|
return <InlineLoading />;
|