@openmrs/esm-stock-management-app 3.3.2-pre.21 → 3.3.2-pre.210

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.
Files changed (240) hide show
  1. package/README.md +20 -0
  2. package/dist/1164.js +1 -1
  3. package/dist/1164.js.map +1 -1
  4. package/dist/1207.js +1 -0
  5. package/dist/1207.js.map +1 -0
  6. package/dist/1209.js +1 -0
  7. package/dist/1209.js.map +1 -0
  8. package/dist/1480.js +1 -1
  9. package/dist/1480.js.map +1 -1
  10. package/dist/1600.js +1 -1
  11. package/dist/1600.js.map +1 -1
  12. package/dist/1646.js +1 -1
  13. package/dist/1646.js.map +1 -1
  14. package/dist/1869.js +1 -1
  15. package/dist/1869.js.map +1 -1
  16. package/dist/1877.js +1 -1
  17. package/dist/1877.js.map +1 -1
  18. package/dist/1878.js +1 -1
  19. package/dist/1878.js.map +1 -1
  20. package/dist/2317.js +1 -1
  21. package/dist/2317.js.map +1 -1
  22. package/dist/2392.js +11 -11
  23. package/dist/2392.js.map +1 -1
  24. package/dist/2416.js +1 -1
  25. package/dist/2416.js.map +1 -1
  26. package/dist/2713.js +1 -1
  27. package/dist/2713.js.map +1 -1
  28. package/dist/282.js +1 -1
  29. package/dist/282.js.map +1 -1
  30. package/dist/2881.js +1 -1
  31. package/dist/2881.js.map +1 -1
  32. package/dist/3378.js +1 -1
  33. package/dist/3378.js.map +1 -1
  34. package/dist/3576.js +2 -2
  35. package/dist/3576.js.map +1 -1
  36. package/dist/3588.js +1 -0
  37. package/dist/3588.js.map +1 -0
  38. package/dist/3963.js +1 -1
  39. package/dist/3963.js.map +1 -1
  40. package/dist/4106.js +1 -1
  41. package/dist/4106.js.map +1 -1
  42. package/dist/4111.js +1 -1
  43. package/dist/4111.js.map +1 -1
  44. package/dist/4214.js +1 -1
  45. package/dist/4214.js.map +1 -1
  46. package/dist/434.js +1 -1
  47. package/dist/434.js.map +1 -1
  48. package/dist/4348.js +1 -1
  49. package/dist/4348.js.map +1 -1
  50. package/dist/4383.js +1 -1
  51. package/dist/4383.js.map +1 -1
  52. package/dist/4658.js +1 -1
  53. package/dist/4658.js.map +1 -1
  54. package/dist/4869.css +1 -0
  55. package/dist/4869.css.map +1 -0
  56. package/dist/4869.js +1048 -0
  57. package/dist/4869.js.map +1 -0
  58. package/dist/4928.js +1 -1
  59. package/dist/4928.js.map +1 -1
  60. package/dist/5117.js +1 -1
  61. package/dist/5117.js.map +1 -1
  62. package/dist/5132.js +1 -1
  63. package/dist/5132.js.map +1 -1
  64. package/dist/5145.js +1 -1
  65. package/dist/5145.js.map +1 -1
  66. package/dist/5503.js +1 -1
  67. package/dist/5503.js.map +1 -1
  68. package/dist/556.js +1 -1
  69. package/dist/556.js.map +1 -1
  70. package/dist/5644.js +1 -1
  71. package/dist/5644.js.map +1 -1
  72. package/dist/5838.js +1 -1
  73. package/dist/5838.js.map +1 -1
  74. package/dist/5940.js +1 -1
  75. package/dist/5940.js.map +1 -1
  76. package/dist/5996.js +1 -1
  77. package/dist/5996.js.map +1 -1
  78. package/dist/6047.js +1 -1
  79. package/dist/6047.js.map +1 -1
  80. package/dist/6105.js +1 -1
  81. package/dist/6105.js.map +1 -1
  82. package/dist/6371.js +1 -1
  83. package/dist/6371.js.map +1 -1
  84. package/dist/6377.js +1 -1
  85. package/dist/6377.js.map +1 -1
  86. package/dist/6444.js +1 -1
  87. package/dist/6444.js.map +1 -1
  88. package/dist/6508.js +1 -1
  89. package/dist/6508.js.map +1 -1
  90. package/dist/6724.js +1 -1
  91. package/dist/6724.js.map +1 -1
  92. package/dist/6904.js +1 -1
  93. package/dist/6904.js.map +1 -1
  94. package/dist/6981.js +1 -1
  95. package/dist/6981.js.map +1 -1
  96. package/dist/7037.js +1 -1
  97. package/dist/7045.js +1 -1
  98. package/dist/7045.js.map +1 -1
  99. package/dist/7175.js +1 -1
  100. package/dist/7175.js.map +1 -1
  101. package/dist/7182.js +1 -1
  102. package/dist/7182.js.map +1 -1
  103. package/dist/7742.js +1 -1
  104. package/dist/7742.js.map +1 -1
  105. package/dist/7912.js +1 -1
  106. package/dist/7912.js.map +1 -1
  107. package/dist/8156.js +1 -1
  108. package/dist/8156.js.map +1 -1
  109. package/dist/822.js +1 -1
  110. package/dist/822.js.map +1 -1
  111. package/dist/8358.js +1 -1
  112. package/dist/8358.js.map +1 -1
  113. package/dist/8359.js +1 -1
  114. package/dist/8359.js.map +1 -1
  115. package/dist/8473.js +1 -0
  116. package/dist/8473.js.map +1 -0
  117. package/dist/854.js +1 -0
  118. package/dist/854.js.map +1 -0
  119. package/dist/8661.js +1 -1
  120. package/dist/8661.js.map +1 -1
  121. package/dist/8695.js +1 -1
  122. package/dist/8695.js.map +1 -1
  123. package/dist/8990.js +1 -1
  124. package/dist/8990.js.map +1 -1
  125. package/dist/903.js +1 -1
  126. package/dist/903.js.map +1 -1
  127. package/dist/9072.js +1 -1
  128. package/dist/9072.js.map +1 -1
  129. package/dist/9154.js +1 -1
  130. package/dist/9154.js.map +1 -1
  131. package/dist/9191.js +1 -1
  132. package/dist/9191.js.map +1 -1
  133. package/dist/9806.js +1 -1
  134. package/dist/9806.js.map +1 -1
  135. package/dist/openmrs-esm-stock-management-app.js +2 -2
  136. package/dist/openmrs-esm-stock-management-app.js.map +1 -1
  137. package/dist/routes.json +1 -1
  138. package/package.json +1 -1
  139. package/src/config-schema.ts +38 -0
  140. package/src/constants.ts +0 -56
  141. package/src/routes.json +6 -2
  142. package/src/stock-app-menu-item/item.component.tsx +9 -11
  143. package/src/stock-app-menu-item/item.test.tsx +18 -0
  144. package/src/stock-home/expired-stock.modal.tsx +2 -6
  145. package/src/stock-home/expired-stock.test.tsx +23 -0
  146. package/src/stock-home/issuing-stock.modal.tsx +18 -16
  147. package/src/stock-home/issuing-stock.test.tsx +41 -0
  148. package/src/stock-items/add-stock-item/add-stock-item.component.tsx +10 -1
  149. package/src/stock-items/add-stock-item/add-stock-item.integration.test.tsx +45 -1
  150. package/src/stock-items/add-stock-item/add-stock-item.test.tsx +20 -1
  151. package/src/stock-items/add-stock-item/stock-item-details/stock-item-details.component.tsx +13 -4
  152. package/src/stock-items/add-stock-item/stock-item-units-edit/stock-item-units-edit.component.test.tsx +69 -0
  153. package/src/stock-items/add-stock-item/stock-item-units-edit/stock-item-units-edit.component.tsx +1 -1
  154. package/src/stock-items/stock-items.resource.test.ts +79 -0
  155. package/src/stock-items/stock-items.resource.ts +25 -3
  156. package/src/stock-items/validationSchema.test.ts +51 -0
  157. package/src/stock-items/validationSchema.ts +10 -1
  158. package/src/stock-locations/stock-locations-table.resource.test.ts +24 -2
  159. package/src/stock-lookups/stock-lookups.resource.ts +12 -22
  160. package/src/stock-operations/stock-operation-types-selector/stock-operation-types-selector.component.tsx +4 -1
  161. package/src/stock-operations/stock-operation-types-selector/stock-operation-types-selector.test.tsx +46 -0
  162. package/src/stock-operations/stock-operations-forms/hooks/useFilteredOperationTypesByRoles.test.tsx +77 -0
  163. package/src/stock-operations/stock-operations-forms/hooks/useFilteredOperationTypesByRoles.ts +20 -9
  164. package/src/stock-operations/stock-operations-forms/steps/base-operation-details-form-step.test.tsx +64 -0
  165. package/src/stock-operations/stock-operations-forms/steps/base-operation-details-form-step.tsx +16 -5
  166. package/src/stock-operations/stock-operations-forms/steps/stock-operation-item-cell.component.tsx +2 -7
  167. package/src/stock-operations/stock-operations-forms/steps/stock-operation-submission-form-step.component.tsx +11 -3
  168. package/src/stock-operations/stock-operations-forms/steps/stock-operation-submission-form-step.test.tsx +146 -0
  169. package/src/stock-operations/stock-operations-forms/stock-operation-form.component.tsx +4 -2
  170. package/src/stock-operations/stock-operations-forms/stock-operation-form.test.tsx +135 -0
  171. package/src/stock-operations/stock-operations-modal/stock-operations.modal.test.tsx +69 -0
  172. package/src/stock-operations/stock-operations-modal/stock-operations.modal.tsx +12 -2
  173. package/src/stock-operations/stock-operations-table.component.tsx +8 -2
  174. package/src/stock-operations/stock-operations-table.test.tsx +48 -0
  175. package/src/stock-operations/stock-print-reports/StockOperationReport.test.ts +15 -0
  176. package/src/stock-operations/stock-print-reports/StockOperationReport.tsx +5 -2
  177. package/src/stock-reports/ReportType.ts +0 -14
  178. package/src/stock-reports/generate-report/create-stock-report.test.tsx +191 -0
  179. package/src/stock-reports/generate-report/create-stock-report.workspace.tsx +63 -37
  180. package/src/stock-reports/report-list/stock-reports.component.tsx +3 -4
  181. package/src/stock-reports/report-validation-schema.ts +13 -2
  182. package/src/stock-reports/stock-reports.resource.test.ts +50 -0
  183. package/src/stock-reports/stock-reports.resource.ts +23 -13
  184. package/src/stock-sources/add-stock-sources/add-stock-sources.test.tsx +30 -0
  185. package/src/stock-sources/add-stock-sources/add-stock-sources.workspace.tsx +2 -8
  186. package/src/stock-sources/stock-sources-items-table.resource.test.ts +40 -0
  187. package/src/stock-sources/stock-sources-items-table.resource.ts +2 -1
  188. package/src/stock-user-role-scopes/add-stock-user-scope/add-stock-user-role-scope.test.tsx +49 -0
  189. package/src/stock-user-role-scopes/add-stock-user-scope/add-stock-user-role-scope.workspace.tsx +6 -21
  190. package/src/stock-user-role-scopes/stock-user-role-scopes-items-table.component.tsx +1 -10
  191. package/src/stock-user-role-scopes/stock-user-role-scopes-items-table.test.tsx +65 -0
  192. package/translations/am.json +6 -1
  193. package/translations/ar.json +6 -1
  194. package/translations/ar_SY.json +6 -1
  195. package/translations/bn.json +6 -1
  196. package/translations/cs.json +6 -1
  197. package/translations/de.json +6 -1
  198. package/translations/en.json +6 -1
  199. package/translations/en_US.json +6 -1
  200. package/translations/es.json +6 -1
  201. package/translations/es_MX.json +6 -1
  202. package/translations/fr.json +6 -1
  203. package/translations/he.json +6 -1
  204. package/translations/hi.json +6 -1
  205. package/translations/hi_IN.json +6 -1
  206. package/translations/id.json +6 -1
  207. package/translations/it.json +6 -1
  208. package/translations/ka.json +6 -1
  209. package/translations/km.json +6 -1
  210. package/translations/ku.json +6 -1
  211. package/translations/ky.json +6 -1
  212. package/translations/lg.json +6 -1
  213. package/translations/ne.json +6 -1
  214. package/translations/pl.json +6 -1
  215. package/translations/pt.json +6 -1
  216. package/translations/pt_BR.json +6 -1
  217. package/translations/qu.json +6 -1
  218. package/translations/ro_RO.json +6 -1
  219. package/translations/ru_RU.json +6 -1
  220. package/translations/si.json +6 -1
  221. package/translations/sq.json +6 -1
  222. package/translations/sw.json +6 -1
  223. package/translations/sw_KE.json +6 -1
  224. package/translations/tr.json +6 -1
  225. package/translations/tr_TR.json +6 -1
  226. package/translations/uk.json +6 -1
  227. package/translations/uz.json +6 -1
  228. package/translations/uz@Latn.json +6 -1
  229. package/translations/uz_UZ.json +6 -1
  230. package/translations/vi.json +6 -1
  231. package/translations/zh.json +6 -1
  232. package/translations/zh_CN.json +6 -1
  233. package/translations/zh_TW.json +6 -1
  234. package/dist/138.js +0 -1
  235. package/dist/138.js.map +0 -1
  236. package/dist/3936.css +0 -1
  237. package/dist/3936.css.map +0 -1
  238. package/dist/3936.js +0 -1048
  239. package/dist/3936.js.map +0 -1
  240. package/src/stock-app-menu-item/item.scss +0 -14
@@ -1,17 +1,15 @@
1
- import { ClickableTile } from '@carbon/react';
2
1
  import React from 'react';
3
- import styles from './item.scss';
4
- import { Report } from '@carbon/react/icons';
2
+ import { useTranslation } from 'react-i18next';
3
+ import { ConfigurableLink } from '@openmrs/esm-framework';
5
4
 
6
- const Item = () => {
7
- // items
8
- const openmrsSpaBase = window['getOpenmrsSpaBase']();
5
+ const StockManagementAppMenuLink = () => {
6
+ const { t } = useTranslation();
9
7
 
10
8
  return (
11
- <ClickableTile className={styles.customTile} id="menu-item" href={`${openmrsSpaBase}stock-management`}>
12
- <div className="customTileTitle">{<Report size={24} />}</div>
13
- <div>Stock Management</div>
14
- </ClickableTile>
9
+ <ConfigurableLink to={'${openmrsSpaBase}/stock-management'}>
10
+ {t('stockManagement', 'Stock management')}
11
+ </ConfigurableLink>
15
12
  );
16
13
  };
17
- export default Item;
14
+
15
+ export default StockManagementAppMenuLink;
@@ -0,0 +1,18 @@
1
+ import React from 'react';
2
+ import { beforeEach, describe, expect, it } from 'vitest';
3
+ import { render, screen } from '@testing-library/react';
4
+ import StockManagementAppMenuLink from './item.component';
5
+
6
+ describe('StockManagementAppMenuLink', () => {
7
+ beforeEach(() => {
8
+ window.spaBase = '/openmrs/spa';
9
+ window.getOpenmrsSpaBase = () => '/openmrs/spa/';
10
+ });
11
+
12
+ it('renders a translated link to the stock management app', () => {
13
+ render(<StockManagementAppMenuLink />);
14
+
15
+ const link = screen.getByRole('link', { name: 'Stock management' });
16
+ expect(link).toHaveAttribute('href', '/openmrs/spa/stock-management');
17
+ });
18
+ });
@@ -11,6 +11,7 @@ import {
11
11
  TableRow,
12
12
  } from '@carbon/react';
13
13
  import { useTranslation } from 'react-i18next';
14
+ import { formatDate as formatDisplayDate } from '@openmrs/esm-framework';
14
15
 
15
16
  interface ExpiredStockModalProps {
16
17
  closeModal: () => void;
@@ -30,12 +31,7 @@ const ExpiredStockModal = ({ closeModal, expiredStock }: ExpiredStockModalProps)
30
31
 
31
32
  const formatDate = (dateString) => {
32
33
  if (!dateString) return 'N/A';
33
- const date = new Date(dateString);
34
- return new Intl.DateTimeFormat('en-GB', {
35
- year: 'numeric',
36
- month: 'short',
37
- day: '2-digit',
38
- }).format(date);
34
+ return formatDisplayDate(new Date(dateString), { time: false, noToday: true });
39
35
  };
40
36
 
41
37
  return (
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+ import { afterEach, describe, expect, it, vi } from 'vitest';
3
+ import { render, screen } from '@testing-library/react';
4
+ import ExpiredStockModal from './expired-stock.modal';
5
+
6
+ const expiredStock = [{ drugName: 'Paracetamol', batchNo: 'B1', quantity: 5, expiration: '2026-01-15T12:00:00.000Z' }];
7
+
8
+ describe('ExpiredStockModal', () => {
9
+ afterEach(() => {
10
+ vi.unstubAllGlobals();
11
+ });
12
+
13
+ it.each([
14
+ ['en', /Jan/],
15
+ ['fr', /janv/],
16
+ ])("formats expiration dates in the user's locale (%s)", (language, expected) => {
17
+ vi.stubGlobal('i18next', { language });
18
+
19
+ render(<ExpiredStockModal closeModal={vi.fn()} expiredStock={expiredStock} />);
20
+
21
+ expect(screen.getByRole('cell', { name: expected })).toBeInTheDocument();
22
+ });
23
+ });
@@ -21,12 +21,12 @@ const IssuingStockModal = ({ issuingStock, closeModal }: IssuingStockModalProps)
21
21
  const { t } = useTranslation();
22
22
 
23
23
  const headers = [
24
- { key: 'status', header: 'Status' },
25
- { key: 'sourceName', header: 'Source' },
26
- { key: 'destinationName', header: 'Destination' },
27
- { key: 'stockItemName', header: 'Stock Item' },
28
- { key: 'stockItemPackagingUOMName', header: 'Unit' },
29
- { key: 'quantity', header: 'Quantity' },
24
+ { key: 'status', header: t('status', 'Status') },
25
+ { key: 'sourceName', header: t('source', 'Source') },
26
+ { key: 'destinationName', header: t('destination', 'Destination') },
27
+ { key: 'stockItemName', header: t('stockItem', 'Stock Item') },
28
+ { key: 'stockItemPackagingUOMName', header: t('unit', 'Unit') },
29
+ { key: 'quantity', header: t('quantity', 'Quantity') },
30
30
  ];
31
31
 
32
32
  return (
@@ -44,16 +44,18 @@ const IssuingStockModal = ({ issuingStock, closeModal }: IssuingStockModalProps)
44
44
  </TableRow>
45
45
  </TableHead>
46
46
  <TableBody>
47
- {issuingStock.map((item, index) => (
48
- <TableRow key={index}>
49
- <TableCell>{item?.status || 'N/A'}</TableCell>
50
- <TableCell>{item?.sourceName || 'N/A'}</TableCell>
51
- <TableCell>{item?.destinationName || 'N/A'}</TableCell>
52
- <TableCell>{item?.stockItemName || 'N/A'}</TableCell>
53
- <TableCell>{item?.stockItemPackagingUOMName || 'N/A'}</TableCell>
54
- <TableCell>{item?.quantity || 'N/A'}</TableCell>
55
- </TableRow>
56
- ))}
47
+ {issuingStock.map((item, index) =>
48
+ item?.stockOperationItems?.map((stock, stockIndex) => (
49
+ <TableRow key={`${index}-${stockIndex}`}>
50
+ <TableCell>{item?.status || 'N/A'}</TableCell>
51
+ <TableCell>{item?.sourceName || 'N/A'}</TableCell>
52
+ <TableCell>{item?.destinationName || 'N/A'}</TableCell>
53
+ <TableCell>{stock?.stockItemName || 'N/A'}</TableCell>
54
+ <TableCell>{stock?.stockItemPackagingUOMName || 'N/A'}</TableCell>
55
+ <TableCell>{stock?.quantity || 'N/A'}</TableCell>
56
+ </TableRow>
57
+ )),
58
+ )}
57
59
  </TableBody>
58
60
  </Table>
59
61
  </TableContainer>
@@ -0,0 +1,41 @@
1
+ import React from 'react';
2
+ import { describe, expect, it, vi } from 'vitest';
3
+ import { render, screen } from '@testing-library/react';
4
+ import routes from '../routes.json';
5
+ import IssuingStockModal from './issuing-stock.modal';
6
+
7
+ const issuingStock = [
8
+ {
9
+ status: 'DISPATCHED',
10
+ sourceName: 'Main Store',
11
+ destinationName: 'Main Pharmacy',
12
+ stockOperationItems: [
13
+ { stockItemName: 'Paracetamol 500mg', stockItemPackagingUOMName: 'Tablet', quantity: 200 },
14
+ { stockItemName: 'Amoxicillin 250mg', stockItemPackagingUOMName: 'Capsule', quantity: 100 },
15
+ ],
16
+ },
17
+ ];
18
+
19
+ describe('IssuingStockModal', () => {
20
+ it('is registered as a modal so showModal can launch it', () => {
21
+ expect(routes.modals).toContainEqual({ name: 'issuing-stock-modal', component: 'issuingStockModal' });
22
+ });
23
+
24
+ it('lists each item of the issued stock operations', () => {
25
+ render(<IssuingStockModal closeModal={vi.fn()} issuingStock={issuingStock} />);
26
+
27
+ expect(
28
+ screen.getByRole('row', { name: /DISPATCHED Main Store Main Pharmacy Paracetamol 500mg Tablet 200/ }),
29
+ ).toBeInTheDocument();
30
+ expect(
31
+ screen.getByRole('row', { name: /DISPATCHED Main Store Main Pharmacy Amoxicillin 250mg Capsule 100/ }),
32
+ ).toBeInTheDocument();
33
+ expect(screen.queryByRole('cell', { name: 'N/A' })).not.toBeInTheDocument();
34
+ });
35
+
36
+ it('says so when there is no issued stock', () => {
37
+ render(<IssuingStockModal closeModal={vi.fn()} issuingStock={[]} />);
38
+
39
+ expect(screen.getByText('No issued stock data available.')).toBeInTheDocument();
40
+ });
41
+ });
@@ -20,8 +20,16 @@ const AddEditStockItem: React.FC<Workspace2DefinitionProps<AddStockItemProps>> =
20
20
  workspaceProps,
21
21
  closeWorkspace,
22
22
  }) => {
23
- const { stockItem } = workspaceProps ?? {};
23
+ const { stockItem: initialStockItem } = workspaceProps ?? {};
24
24
  const { t } = useTranslation();
25
+ // Holds the item as last saved, so the Details tab does not fall back to the values it opened with
26
+ const [stockItem, setStockItem] = useState(initialStockItem);
27
+ const [previousInitialStockItem, setPreviousInitialStockItem] = useState(initialStockItem);
28
+ if (initialStockItem !== previousInitialStockItem) {
29
+ // The workspace was given a different item: start from it
30
+ setPreviousInitialStockItem(initialStockItem);
31
+ setStockItem(initialStockItem);
32
+ }
25
33
  const [selectedTab, setSelectedTab] = useState(0);
26
34
  const isEditing = Boolean(stockItem);
27
35
 
@@ -37,6 +45,7 @@ const AddEditStockItem: React.FC<Workspace2DefinitionProps<AddStockItemProps>> =
37
45
  key={stockItem?.uuid}
38
46
  handleTabChange={handleTabChange}
39
47
  stockItem={stockItem}
48
+ onStockItemSaved={setStockItem}
40
49
  onCloseWorkspace={closeWorkspace}
41
50
  />
42
51
  ),
@@ -1,10 +1,20 @@
1
1
  import React from 'react';
2
2
  import { vi, describe, it, expect } from 'vitest';
3
- import { render, screen } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+ import { render, screen, waitFor } from '@testing-library/react';
5
+ import { type FetchResponse } from '@openmrs/esm-framework';
4
6
  import { type StockItemDTO } from '../../core/api/types/stockItem/StockItem';
5
7
  import { mockWorkspace2Props } from '@mocks';
8
+ import { updateStockItem } from '../stock-items.resource';
6
9
  import AddEditStockItem from './add-stock-item.component';
7
10
 
11
+ vi.mock('../stock-items.resource', () => ({
12
+ createStockItem: vi.fn(),
13
+ updateStockItem: vi.fn(),
14
+ }));
15
+
16
+ const mockUpdateStockItem = vi.mocked(updateStockItem);
17
+
8
18
  // Mock the API-hitting child components so the integration test can focus on
9
19
  // the form-staleness behaviour without bringing in their dependencies.
10
20
  vi.mock('./drug-selector/drug-selector.component', () => ({ default: () => <div data-testid="drug-selector" /> }));
@@ -79,4 +89,38 @@ describe('AddEditStockItem (integration)', () => {
79
89
  expect(screen.queryByDisplayValue('Aspirin')).not.toBeInTheDocument();
80
90
  expect(screen.queryByDisplayValue('ASP')).not.toBeInTheDocument();
81
91
  });
92
+
93
+ it('keeps an edit when the Details tab is left and saved again', async () => {
94
+ const user = userEvent.setup();
95
+ mockUpdateStockItem.mockImplementation((uuid, item) =>
96
+ Promise.resolve({ data: { ...item, uuid } } as unknown as FetchResponse<StockItemDTO>),
97
+ );
98
+ const item: StockItemDTO = {
99
+ ...(baseItem as StockItemDTO),
100
+ uuid: 'item-a',
101
+ conceptUuid: 'concept-a',
102
+ commonName: 'Aspirin',
103
+ acronym: 'ASP',
104
+ };
105
+
106
+ render(<AddEditStockItem {...mockWorkspace2Props({ stockItem: item })} />);
107
+
108
+ const commonName = screen.getByDisplayValue('Aspirin');
109
+ await user.clear(commonName);
110
+ await user.type(commonName, 'Aspirin v2');
111
+ await user.click(screen.getByRole('button', { name: /save/i }));
112
+ await waitFor(() => expect(mockUpdateStockItem).toHaveBeenCalledTimes(1));
113
+
114
+ await user.click(screen.getByText(/packaging units/i));
115
+ await user.click(screen.getByText(/stock item details/i));
116
+
117
+ expect(screen.getByDisplayValue('Aspirin v2')).toBeInTheDocument();
118
+
119
+ await user.click(screen.getByRole('button', { name: /save/i }));
120
+ await waitFor(() => expect(mockUpdateStockItem).toHaveBeenCalledTimes(2));
121
+ expect(mockUpdateStockItem).toHaveBeenLastCalledWith(
122
+ 'item-a',
123
+ expect.objectContaining({ commonName: 'Aspirin v2', acronym: 'ASP' }),
124
+ );
125
+ });
82
126
  });
@@ -11,7 +11,15 @@ vi.mock('@carbon/react/icons', () => ({
11
11
  }));
12
12
 
13
13
  vi.mock('./stock-item-details/stock-item-details.component', () => ({
14
- default: ({ stockItem }) => <div data-testid="stock-item-details">Stock Item Details: {stockItem?.uuid}</div>,
14
+ default: ({ stockItem, onStockItemSaved }) => (
15
+ <>
16
+ <div data-testid="stock-item-details">Stock Item Details: {stockItem?.uuid}</div>
17
+ <div data-testid="stock-item-common-name">{stockItem?.commonName}</div>
18
+ <button onClick={() => onStockItemSaved?.({ ...stockItem, commonName: 'Saved Common Name' })}>
19
+ Mock save details
20
+ </button>
21
+ </>
22
+ ),
15
23
  }));
16
24
 
17
25
  vi.mock('./packaging-units/packaging-units.component', () => ({
@@ -115,6 +123,17 @@ describe('AddEditStockItem', () => {
115
123
  expect(screen.getByText('Stock Item Details: test-uuid-123')).toBeInTheDocument();
116
124
  });
117
125
 
126
+ it('shows the saved details when returning to the Details tab', async () => {
127
+ const user = userEvent.setup();
128
+ render(<AddEditStockItem {...mockWorkspace2Props({ stockItem: mockModel })} />);
129
+
130
+ await user.click(screen.getByText('Mock save details'));
131
+ await user.click(screen.getByText(/packaging units/i));
132
+ await user.click(screen.getByText(/stock item details/i));
133
+
134
+ expect(screen.getByTestId('stock-item-common-name')).toHaveTextContent('Saved Common Name');
135
+ });
136
+
118
137
  it('changes selected tab when clicking on different tabs', async () => {
119
138
  const user = userEvent.setup();
120
139
  render(<AddEditStockItem {...mockWorkspace2Props({ stockItem: mockModel })} />);
@@ -26,11 +26,17 @@ import styles from '../../add-stock-item/add-stock-item.scss';
26
26
  interface StockItemDetailsProps {
27
27
  stockItem?: StockItemDTO;
28
28
  handleTabChange: (index) => void;
29
+ onStockItemSaved?: (stockItem: StockItemDTO) => void;
29
30
  onCloseWorkspace?: () => void;
30
31
  }
31
32
 
32
33
  // t('purchasePriceUoMRequired', 'Purchase price packaging unit is required when purchase price is set')
33
- const StockItemDetails = ({ stockItem, handleTabChange, onCloseWorkspace }: StockItemDetailsProps) => {
34
+ const StockItemDetails = ({
35
+ stockItem,
36
+ handleTabChange,
37
+ onStockItemSaved,
38
+ onCloseWorkspace,
39
+ }: StockItemDetailsProps) => {
34
40
  const { t } = useTranslation();
35
41
  const handleMutate = useHandleMutate();
36
42
  const isTablet = useLayoutType() === 'tablet';
@@ -43,10 +49,10 @@ const StockItemDetails = ({ stockItem, handleTabChange, onCloseWorkspace }: Stoc
43
49
 
44
50
  const { errors } = formState;
45
51
  const handleSave: SubmitHandler<StockItemFormData> = async (formValues) => {
52
+ // The backend rejects a reorder level unit without a reorder level, so clear both together
53
+ const payload = formValues.reorderLevel == null ? { ...formValues, reorderLevelUoMUuid: null } : formValues;
46
54
  try {
47
- const response = stockItem
48
- ? await updateStockItem(stockItem?.uuid, formValues)
49
- : await createStockItem(formValues);
55
+ const response = stockItem ? await updateStockItem(stockItem?.uuid, payload) : await createStockItem(payload);
50
56
  if (response?.data) {
51
57
  showSnackbar({
52
58
  isLowContrast: true,
@@ -61,6 +67,9 @@ const StockItemDetails = ({ stockItem, handleTabChange, onCloseWorkspace }: Stoc
61
67
  const item = response.data;
62
68
  item.isDrug = !!item.drugUuid;
63
69
  launchAddOrEditStockItemWorkspace(item);
70
+ } else {
71
+ // Keep what the server now holds, so the next visit to this tab does not show (and re-save) stale values
72
+ onStockItemSaved?.({ ...stockItem, ...response.data, isDrug: stockItem.isDrug });
64
73
  }
65
74
  }
66
75
 
@@ -0,0 +1,69 @@
1
+ import React from 'react';
2
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
3
+ import { render, screen } from '@testing-library/react';
4
+ import { useForm } from 'react-hook-form';
5
+ import { type StockItemDTO } from '../../../core/api/types/stockItem/StockItem';
6
+ import { useStockItem } from '../../stock-items.resource';
7
+ import { type StockItemFormData } from '../../validationSchema';
8
+ import StockItemUnitsEdit from './stock-item-units-edit.component';
9
+
10
+ vi.mock('../../stock-items.resource', () => ({
11
+ useStockItem: vi.fn(),
12
+ }));
13
+
14
+ const mockUseStockItem = vi.mocked(useStockItem);
15
+
16
+ const packagingUnits = [
17
+ {
18
+ uuid: 'reorder-unit',
19
+ stockItemUuid: 'item-uuid',
20
+ packagingUomName: 'Box',
21
+ factor: 10,
22
+ isDefaultStockOperationsUoM: false,
23
+ isDispensingUnit: false,
24
+ },
25
+ {
26
+ uuid: 'purchase-unit',
27
+ stockItemUuid: 'item-uuid',
28
+ packagingUomName: 'Vial',
29
+ factor: 1,
30
+ isDefaultStockOperationsUoM: false,
31
+ isDispensingUnit: false,
32
+ },
33
+ ];
34
+
35
+ const stockItem = {
36
+ uuid: 'item-uuid',
37
+ reorderLevel: 5,
38
+ reorderLevelUoMUuid: 'reorder-unit',
39
+ purchasePrice: 20,
40
+ purchasePriceUoMUuid: 'purchase-unit',
41
+ packagingUnits,
42
+ } as StockItemDTO;
43
+
44
+ const Harness = ({ item }: { item: StockItemDTO }) => {
45
+ const { control, formState } = useForm<StockItemFormData>({ defaultValues: item as StockItemFormData });
46
+ return <StockItemUnitsEdit stockItemUuid={item.uuid} control={control} formState={formState} />;
47
+ };
48
+
49
+ describe('StockItemUnitsEdit', () => {
50
+ beforeEach(() => {
51
+ mockUseStockItem.mockReturnValue({ isLoading: false, item: stockItem, error: undefined });
52
+ });
53
+
54
+ it('shows the saved reorder level unit rather than the purchase price unit', () => {
55
+ render(<Harness item={stockItem} />);
56
+
57
+ expect(screen.getByRole('combobox', { name: /reorder level packaging unit/i })).toHaveValue('Box - 10 ');
58
+ expect(screen.getByRole('combobox', { name: /purchase price packaging unit/i })).toHaveValue('Vial - 1 ');
59
+ });
60
+
61
+ it('leaves the reorder level unit blank when no reorder unit is saved', () => {
62
+ const itemWithoutReorderUnit = { ...stockItem, reorderLevelUoMUuid: null };
63
+ mockUseStockItem.mockReturnValue({ isLoading: false, item: itemWithoutReorderUnit, error: undefined });
64
+
65
+ render(<Harness item={itemWithoutReorderUnit} />);
66
+
67
+ expect(screen.getByRole('combobox', { name: /reorder level packaging unit/i })).toHaveValue('');
68
+ });
69
+ });
@@ -64,7 +64,7 @@ const StockItemUnitsEdit: React.FC<StockItemUnitsEditProps> = ({ stockItemUuid,
64
64
  />
65
65
  )}
66
66
  <DispensingPackageMeasurement
67
- dispensingUnitPackagingUoMUuid={stockItem?.purchasePriceUoMUuid}
67
+ dispensingUnitPackagingUoMUuid={stockItem?.reorderLevelUoMUuid}
68
68
  name="reorderLevelUoMUuid"
69
69
  controllerName="reorderLevelUoMUuid"
70
70
  control={control}
@@ -0,0 +1,79 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { openmrsFetch } from '@openmrs/esm-framework';
3
+ import { type StockItemFormData } from './validationSchema';
4
+ import { updateStockItem } from './stock-items.resource';
5
+
6
+ describe('updateStockItem', () => {
7
+ // An item as it is loaded for editing, with every packaging unit assigned
8
+ const loadedItem = {
9
+ uuid: 'item-uuid',
10
+ isDrug: true,
11
+ drugUuid: 'drug-uuid',
12
+ drugName: 'Advil 200mg',
13
+ conceptUuid: 'concept-uuid',
14
+ conceptName: 'Ibuprofen',
15
+ commonName: 'IBUPROFEN',
16
+ acronym: 'IBU',
17
+ hasExpiration: true,
18
+ expiryNotice: 180,
19
+ preferredVendorUuid: 'vendor-uuid',
20
+ preferredVendorName: 'National Medical Stores',
21
+ purchasePrice: 1000,
22
+ purchasePriceUoMUuid: 'box-uuid',
23
+ purchasePriceUoMName: 'Box',
24
+ categoryUuid: null,
25
+ categoryName: null,
26
+ dispensingUnitUuid: 'tablet-concept-uuid',
27
+ dispensingUnitName: 'Tablet',
28
+ dispensingUnitPackagingUoMUuid: 'box-uuid',
29
+ dispensingUnitPackagingUoMName: 'Box',
30
+ defaultStockOperationsUoMUuid: 'box-uuid',
31
+ defaultStockOperationsUoMName: 'Box',
32
+ reorderLevel: 10,
33
+ reorderLevelUoMUuid: 'box-uuid',
34
+ reorderLevelUoMName: 'Box',
35
+ dateCreated: new Date('2026-10-01'),
36
+ creatorGivenName: 'Super',
37
+ creatorFamilyName: 'User',
38
+ voided: false,
39
+ } as StockItemFormData;
40
+
41
+ it('sends only the properties the backend accepts on update', () => {
42
+ updateStockItem('item-uuid', loadedItem);
43
+
44
+ const [url, init] = vi.mocked(openmrsFetch).mock.lastCall;
45
+ expect(url).toContain('/stockmanagement/stockitem/item-uuid');
46
+ expect(init.body).toEqual({
47
+ hasExpiration: true,
48
+ preferredVendorUuid: 'vendor-uuid',
49
+ purchasePrice: 1000,
50
+ purchasePriceUoMUuid: 'box-uuid',
51
+ dispensingUnitUuid: 'tablet-concept-uuid',
52
+ dispensingUnitPackagingUoMUuid: 'box-uuid',
53
+ defaultStockOperationsUoMUuid: 'box-uuid',
54
+ categoryUuid: null,
55
+ commonName: 'IBUPROFEN',
56
+ acronym: 'IBU',
57
+ reorderLevel: 10,
58
+ reorderLevelUoMUuid: 'box-uuid',
59
+ expiryNotice: 180,
60
+ });
61
+ });
62
+
63
+ it('keeps null values so a cleared field is saved as cleared', () => {
64
+ updateStockItem('item-uuid', { ...loadedItem, reorderLevel: null, reorderLevelUoMUuid: null });
65
+
66
+ expect(vi.mocked(openmrsFetch).mock.lastCall[1].body).toMatchObject({
67
+ reorderLevel: null,
68
+ reorderLevelUoMUuid: null,
69
+ });
70
+ });
71
+
72
+ it('does not change the form values it is given', () => {
73
+ const item = { ...loadedItem };
74
+
75
+ updateStockItem('item-uuid', item);
76
+
77
+ expect(item).toEqual(loadedItem);
78
+ });
79
+ });
@@ -242,19 +242,41 @@ export function createStockItem(item: StockItemFormData) {
242
242
  });
243
243
  }
244
244
 
245
+ // The properties StockItemResource accepts on update. The REST layer rejects any other property that has a value,
246
+ // such as the display names the item is loaded with.
247
+ const UPDATABLE_STOCK_ITEM_PROPERTIES = [
248
+ 'hasExpiration',
249
+ 'preferredVendorUuid',
250
+ 'purchasePrice',
251
+ 'purchasePriceUoMUuid',
252
+ 'dispensingUnitUuid',
253
+ 'dispensingUnitPackagingUoMUuid',
254
+ 'defaultStockOperationsUoMUuid',
255
+ 'categoryUuid',
256
+ 'commonName',
257
+ 'acronym',
258
+ 'reorderLevel',
259
+ 'reorderLevelUoMUuid',
260
+ 'expiryNotice',
261
+ ] as const;
262
+
245
263
  // updateStockItem
246
264
  export function updateStockItem(stockItemUuid: string, item: StockItemFormData) {
247
265
  const apiUrl = `${restBaseUrl}/stockmanagement/stockitem/${stockItemUuid}`;
248
266
  const abortController = new AbortController();
249
- delete item.isDrug;
250
- delete item.dateCreated;
267
+ const body = Object.fromEntries(
268
+ UPDATABLE_STOCK_ITEM_PROPERTIES.filter((property) => property in item).map((property) => [
269
+ property,
270
+ item[property],
271
+ ]),
272
+ );
251
273
  return openmrsFetch<StockItemDTO>(apiUrl, {
252
274
  method: 'POST',
253
275
  headers: {
254
276
  'Content-Type': 'application/json',
255
277
  },
256
278
  signal: abortController.signal,
257
- body: item,
279
+ body,
258
280
  });
259
281
  }
260
282
 
@@ -0,0 +1,51 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { createStockItemDetailsSchema } from './validationSchema';
3
+
4
+ const mockT = ((key: string, defaultValue?: string) => defaultValue || key) as any;
5
+
6
+ describe('createStockItemDetailsSchema', () => {
7
+ const base = {
8
+ isDrug: false,
9
+ hasExpiration: false,
10
+ };
11
+
12
+ it('allows reorder level without unit when reorder level is null or undefined', () => {
13
+ const schema = createStockItemDetailsSchema(mockT);
14
+ expect(schema.safeParse({ ...base, reorderLevel: null, reorderLevelUoMUuid: null }).success).toBe(true);
15
+ expect(schema.safeParse({ ...base }).success).toBe(true);
16
+ });
17
+
18
+ it('allows reorder level with unit when set', () => {
19
+ const schema = createStockItemDetailsSchema(mockT);
20
+ const result = schema.safeParse({ ...base, reorderLevel: 10, reorderLevelUoMUuid: 'abc-123' });
21
+ expect(result.success).toBe(true);
22
+ });
23
+
24
+ it('requires reorder level unit when reorder level is set (> 0)', () => {
25
+ const schema = createStockItemDetailsSchema(mockT);
26
+ const result = schema.safeParse({ ...base, reorderLevel: 10, reorderLevelUoMUuid: null });
27
+ expect(result.success).toBe(false);
28
+ if (result.success === false) {
29
+ expect(result.error.issues.some((issue) => issue.path.includes('reorderLevelUoMUuid'))).toBe(true);
30
+ }
31
+ });
32
+
33
+ it('requires reorder level unit when reorder level is greater than 0', () => {
34
+ const schema = createStockItemDetailsSchema(mockT);
35
+ const result = schema.safeParse({ ...base, reorderLevel: 5, reorderLevelUoMUuid: undefined });
36
+ expect(result.success).toBe(false);
37
+ });
38
+
39
+ it('requires reorder level unit when reorder level is 0', () => {
40
+ const schema = createStockItemDetailsSchema(mockT);
41
+ const result = schema.safeParse({ ...base, reorderLevel: 0, reorderLevelUoMUuid: null });
42
+ expect(result.success).toBe(false);
43
+ });
44
+
45
+ it('treats a cleared reorder level as unset rather than 0', () => {
46
+ const schema = createStockItemDetailsSchema(mockT);
47
+ const result = schema.safeParse({ ...base, reorderLevel: '', reorderLevelUoMUuid: null });
48
+ expect(result.success).toBe(true);
49
+ expect(result.success && result.data.reorderLevel).toBeNull();
50
+ });
51
+ });
@@ -32,7 +32,7 @@ export const createStockItemDetailsSchema = (t: TFunction) =>
32
32
  dispensingUnitPackagingUoMName: z.string().nullish(),
33
33
  defaultStockOperationsUoMUuid: z.string().nullish(),
34
34
  defaultStockOperationsUoMName: z.string().nullish(),
35
- reorderLevel: z.coerce.number().nullish(),
35
+ reorderLevel: z.preprocess((value) => (value === '' ? null : value), z.coerce.number().nullish()),
36
36
  reorderLevelUoMUuid: z.string().nullish(),
37
37
  reorderLevelUoMName: z.string().nullish(),
38
38
  dateCreated: z.coerce.date().nullish(),
@@ -75,6 +75,15 @@ export const createStockItemDetailsSchema = (t: TFunction) =>
75
75
  message: t('purchasePriceUoMRequired', 'Purchase price packaging unit is required when purchase price is set'),
76
76
  path: ['purchasePriceUoMUuid'],
77
77
  },
78
+ )
79
+ .refine(
80
+ ({ reorderLevel, reorderLevelUoMUuid }) => {
81
+ return reorderLevel != null ? !!reorderLevelUoMUuid : true;
82
+ },
83
+ {
84
+ message: t('reorderLevelUoMRequired', 'Reorder level packaging unit is required when reorder level is set'),
85
+ path: ['reorderLevelUoMUuid'],
86
+ },
78
87
  );
79
88
 
80
89
  export type StockItemFormData = z.infer<ReturnType<typeof createStockItemDetailsSchema>>;