From 4ba706cb1aeed12ac7e4df4be95fbd44a7dfdc7b Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 20 Aug 2026 16:22:33 +0000 Subject: [PATCH] feature: add Overdue inbound dashboard card backed by the existing overdue shipments API Co-Authored-By: Matthew Guerra --- grails-app/conf/runtime.groovy | 17 ++++++ grails-app/i18n/messages.properties | 6 ++ src/js/actions/index.js | 60 ++++++++++++++----- src/js/components/dashboard/TableCard.jsx | 16 +++++ .../inbound/StockMovementInboundList.jsx | 8 ++- .../utils/dashboard-response-adapters.test.js | 50 ++++++++++++++++ src/js/utils/dashboard-response-adapters.js | 34 +++++++++++ 7 files changed, 175 insertions(+), 16 deletions(-) create mode 100644 src/js/tests/utils/dashboard-response-adapters.test.js create mode 100644 src/js/utils/dashboard-response-adapters.js diff --git a/grails-app/conf/runtime.groovy b/grails-app/conf/runtime.groovy index 29bc35a4b03..950ea7e45fd 100644 --- a/grails-app/conf/runtime.groovy +++ b/grails-app/conf/runtime.groovy @@ -630,6 +630,10 @@ openboxes { [ widgetId: "discrepancy", order : 6 + ], + [ + widgetId: "overdueInbound", + order : 7 ] ] } @@ -676,6 +680,10 @@ openboxes { [ widgetId: "discrepancy", order : 6 + ], + [ + widgetId: "overdueInbound", + order : 7 ] ] } @@ -1005,6 +1013,15 @@ openboxes { endpoint = "/api/dashboard/discrepancy" timeFilter = true } + overdueInbound { + enabled = true + title = "react.dashboard.overdueInbound.title.label" + info = "react.dashboard.overdueInbound.info.label" + graphType = "table" + type = 'graph' + endpoint = "/api/shipments/exceptions/overdue?max=5" + responseAdapter = "overdueInboundShipments" + } delayedShipments { enabled = true title = "react.dashboard.delayedShipments.title.label" diff --git a/grails-app/i18n/messages.properties b/grails-app/i18n/messages.properties index eccb253c8c1..e15ff3da948 100644 --- a/grails-app/i18n/messages.properties +++ b/grails-app/i18n/messages.properties @@ -4548,6 +4548,12 @@ react.dashboard.discrepancy.title.label=Items received with a discrepancy react.dashboard.discrepancy.info.label=List of shipments where there was any discrepancy between shipped and received, filterable by month react.dashboard.delayedShipments.title.label=Delayed Incoming Stock Movements react.dashboard.delayedShipments.info.label=Air shipments not received 30 days after ship date; Sea shipments not received 60 days after ship date; land shipments not received 7 days after ship date +react.dashboard.overdueInbound.title.label=Overdue inbound +react.dashboard.overdueInbound.info.label=Inbound shipments whose expected delivery date has passed and that have not been received yet, most overdue first +react.dashboard.overdueInbound.column.shipment.label=Shipment +react.dashboard.overdueInbound.column.origin.label=Origin +react.dashboard.overdueInbound.column.daysLate.label=Days late +react.dashboard.overdueInbound.empty.label=No overdue inbound shipments react.dashboard.lossCausedByExpiry.title.label=Value of Stock Lost to Expiry react.dashboard.percentageAdHoc.title.label=Stock vs ad-hoc requests last month react.dashboard.percentageAdHoc.info.label=Requests in the previous month broken down by stock vs ad hoc diff --git a/src/js/actions/index.js b/src/js/actions/index.js index 892a8f80284..b1b387de5e0 100644 --- a/src/js/actions/index.js +++ b/src/js/actions/index.js @@ -1,7 +1,7 @@ /* eslint no-param-reassign: ["error", { "props": false }] */ import _ from 'lodash'; import queryString from 'query-string'; -import { addTranslationForLanguage } from 'react-localize-redux'; +import { addTranslationForLanguage, getTranslate } from 'react-localize-redux'; import { ADD_EMPTY_ROW, @@ -85,7 +85,9 @@ import RoleType from 'consts/roleType'; import { UnitOfMeasureType } from 'consts/UnitOfMeasureType'; import apiClient, { parseResponse } from 'utils/apiClient'; import { removeBinLocationData } from 'utils/cycleCountUtils'; +import dashboardResponseAdapters from 'utils/dashboard-response-adapters'; import { fetchBins, getLotNumbersByProductIds, mapShipmentTypes } from 'utils/option-utils'; +import { translateWithDefaultMessage } from 'utils/Translate'; export function showSpinner() { return { @@ -304,16 +306,27 @@ function getParameterList(params = '', locationId = '', userId = '') { return listParams; } +function getTranslateFromState(getState) { + return translateWithDefaultMessage(getTranslate(getState().localize)); +} + +function adaptIndicatorResponse(indicatorConfig, responseData, translate) { + const adapter = dashboardResponseAdapters[indicatorConfig.responseAdapter]; + return adapter ? adapter(responseData, translate) : responseData; +} + function fetchGraphIndicator( dispatch, indicatorConfig, locationId = '', params = '', + translate = (id, defaultMessage) => defaultMessage || id, ) { const id = indicatorConfig.order; const listParams = getParameterList(params, locationId); - const url = `${indicatorConfig.endpoint}?${listParams}`; + const separator = indicatorConfig.endpoint.includes('?') ? '&' : '?'; + const url = `${indicatorConfig.endpoint}${separator}${listParams}`; dispatch({ type: FETCH_GRAPHS, @@ -328,7 +341,7 @@ function fetchGraphIndicator( }); apiClient.get(url).then((res) => { - const indicatorData = res.data; + const indicatorData = adaptIndicatorResponse(indicatorConfig, res.data, translate); dispatch({ type: FETCH_GRAPHS, payload: { @@ -400,14 +413,27 @@ function fetchNumberIndicator( } export function reloadIndicator(indicatorConfig, params, locationId) { - return (dispatch) => { + return (dispatch, getState) => { // new reference so that the original config is not modified const indicatorConfigData = JSON.parse(JSON.stringify(indicatorConfig)); - fetchGraphIndicator(dispatch, indicatorConfigData, locationId, params); + fetchGraphIndicator( + dispatch, + indicatorConfigData, + locationId, + params, + getTranslateFromState(getState), + ); }; } -function getData(dispatch, dashboardConfig, locationId, config = 'personal', userId = '') { +function getData( + dispatch, + dashboardConfig, + locationId, + config = 'personal', + userId = '', + translate, +) { // new reference so that the original config is not modified const dashboard = dashboardConfig.dashboard[config] || {}; @@ -423,7 +449,7 @@ function getData(dispatch, dashboardConfig, locationId, config = 'personal', use _.forEach(visibleWidgets, (widgetConfig) => { if (widgetConfig.type === 'graph') { - fetchGraphIndicator(dispatch, widgetConfig, locationId, ''); + fetchGraphIndicator(dispatch, widgetConfig, locationId, '', translate); } else if (widgetConfig.type === 'number') { fetchNumberIndicator(dispatch, widgetConfig, locationId, userId); } @@ -436,7 +462,7 @@ export function fetchIndicators( locationId, userId, ) { - return (dispatch) => { + return (dispatch, getState) => { dispatch({ type: SET_ACTIVE_CONFIG, payload: { @@ -444,7 +470,7 @@ export function fetchIndicators( }, }); - getData(dispatch, configData, locationId, config, userId); + getData(dispatch, configData, locationId, config, userId, getTranslateFromState(getState)); }; } @@ -455,9 +481,15 @@ export function resetIndicators() { } export function addToIndicators(widgetConfig, locationId, userId = '') { - return (dispatch) => { + return (dispatch, getState) => { if (widgetConfig.type === 'graph') { - fetchGraphIndicator(dispatch, widgetConfig, locationId, ''); + fetchGraphIndicator( + dispatch, + widgetConfig, + locationId, + '', + getTranslateFromState(getState), + ); } else if (widgetConfig.type === 'number') { fetchNumberIndicator(dispatch, widgetConfig, locationId, userId); } @@ -478,8 +510,8 @@ export function reorderIndicators({ oldIndex, newIndex }, e, type) { } // eslint-disable-next-line default-param-last -export function fetchConfigAndData(locationId, config = 'personal', userId, id, filterSelected) { - return (dispatch) => { +export function fetchConfigAndData(locationId, config = 'personal', userId, id) { + return (dispatch, getState) => { apiClient.get(`/api/dashboard/${id}/config`).then((res) => { dispatch({ type: FETCH_CONFIG_AND_SET_ACTIVE, @@ -488,7 +520,7 @@ export function fetchConfigAndData(locationId, config = 'personal', userId, id, activeConfig: config, }, }); - getData(dispatch, res.data, locationId, config, userId, filterSelected); + getData(dispatch, res.data, locationId, config, userId, getTranslateFromState(getState)); }); }; } diff --git a/src/js/components/dashboard/TableCard.jsx b/src/js/components/dashboard/TableCard.jsx index 2c8dd200ca8..ba629e8b13a 100644 --- a/src/js/components/dashboard/TableCard.jsx +++ b/src/js/components/dashboard/TableCard.jsx @@ -2,6 +2,8 @@ import React from 'react'; import PropTypes from 'prop-types'; +import Translate from 'utils/Translate'; + /* global _ */ const TableCard = (props) => { const { @@ -60,6 +62,16 @@ const TableCard = (props) => { + {!data.body.length && data.emptyMessage ? ( + + + + + + ) : null} {data.body.map((item) => ( { @@ -116,6 +128,10 @@ TableCard.propTypes = { nameLink: PropTypes.string, nameDataList: PropTypes.arrayOf(PropTypes.string), nameLinksList: PropTypes.arrayOf(PropTypes.string), + emptyMessage: PropTypes.shape({ + id: PropTypes.string, + defaultMessage: PropTypes.string, + }), }).isRequired, columnsSize: PropTypes.shape({ name: PropTypes.string, diff --git a/src/js/components/stock-movement/inbound/StockMovementInboundList.jsx b/src/js/components/stock-movement/inbound/StockMovementInboundList.jsx index 57c6c873c46..ecf8896e8da 100644 --- a/src/js/components/stock-movement/inbound/StockMovementInboundList.jsx +++ b/src/js/components/stock-movement/inbound/StockMovementInboundList.jsx @@ -1,8 +1,9 @@ import React, { useMemo, useState } from 'react'; import PropTypes from 'prop-types'; +import queryString from 'query-string'; import { connect } from 'react-redux'; -import { withRouter } from 'react-router-dom'; +import { useLocation, withRouter } from 'react-router-dom'; import filterFields from 'components/stock-movement/inbound/FilterFields'; import StockMovementInboundFilters from 'components/stock-movement/inbound/StockMovementInboundFilters'; @@ -18,7 +19,10 @@ const StockMovementInboundList = (props) => { setFilterValues, filterParams, } = useInboundFilters(); - const [overdue, setOverdue] = useState(false); + const { search } = useLocation(); + const [overdue, setOverdue] = useState( + queryString.parse(search).overdue === 'true', + ); const tableFilterParams = useMemo(() => { if (Object.keys(filterParams).length === 0 && !overdue) return filterParams; return { ...filterParams, overdue }; diff --git a/src/js/tests/utils/dashboard-response-adapters.test.js b/src/js/tests/utils/dashboard-response-adapters.test.js new file mode 100644 index 00000000000..cb8ac827c03 --- /dev/null +++ b/src/js/tests/utils/dashboard-response-adapters.test.js @@ -0,0 +1,50 @@ +import dashboardResponseAdapters from 'utils/dashboard-response-adapters'; + +const translate = (id, defaultMessage) => defaultMessage; + +describe('overdueInboundShipments adapter', () => { + it('maps rows onto the table card shape, preserving the response order', () => { + const { data, link } = dashboardResponseAdapters.overdueInboundShipments({ + data: [ + { + id: 'ship1', + shipmentNumber: 'S1', + origin: { id: 'a', name: 'Warehouse A' }, + daysLate: 9, + }, + { + id: 'ship2', + shipmentNumber: 'S2', + origin: { id: 'b', name: 'Warehouse B' }, + daysLate: 2, + }, + ], + count: 2, + totalCount: 2, + }, translate); + + expect(data.number).toBe('Shipment'); + expect(data.name).toBe('Origin'); + expect(data.value).toBe('Days late'); + expect(data.body).toEqual([ + { + number: 'S1', numberLink: '/openboxes/stockMovement/show/ship1', name: 'Warehouse A', value: '9', + }, + { + number: 'S2', numberLink: '/openboxes/stockMovement/show/ship2', name: 'Warehouse B', value: '2', + }, + ]); + expect(link).toContain('overdue=true'); + }); + + it('returns an empty body with an empty message when there are no overdue shipments', () => { + const { data } = dashboardResponseAdapters.overdueInboundShipments({ + data: [], + count: 0, + totalCount: 0, + }, translate); + + expect(data.body).toEqual([]); + expect(data.emptyMessage.id).toBe('react.dashboard.overdueInbound.empty.label'); + }); +}); diff --git a/src/js/utils/dashboard-response-adapters.js b/src/js/utils/dashboard-response-adapters.js new file mode 100644 index 00000000000..381db62ba18 --- /dev/null +++ b/src/js/utils/dashboard-response-adapters.js @@ -0,0 +1,34 @@ +import { STOCK_MOVEMENT_URL } from 'consts/applicationUrls'; + +const overdueInboundShipments = (responseData, translate) => { + const rows = responseData?.data ?? []; + + return { + link: `${STOCK_MOVEMENT_URL.listInbound()}&overdue=true`, + data: { + number: translate('react.dashboard.overdueInbound.column.shipment.label', 'Shipment'), + name: translate('react.dashboard.overdueInbound.column.origin.label', 'Origin'), + value: translate('react.dashboard.overdueInbound.column.daysLate.label', 'Days late'), + emptyMessage: { + id: 'react.dashboard.overdueInbound.empty.label', + defaultMessage: 'No overdue inbound shipments', + }, + body: rows.map((row) => ({ + number: row.shipmentNumber, + numberLink: row.id ? STOCK_MOVEMENT_URL.show(row.id) : undefined, + name: row.origin?.name, + value: `${row.daysLate}`, + })), + }, + }; +}; + +/** + * Maps a response from an API outside /api/dashboard onto the shape a dashboard card expects. + * Keyed by the responseAdapter declared on the widget in openboxes.dashboardConfig. + */ +const dashboardResponseAdapters = { + overdueInboundShipments, +}; + +export default dashboardResponseAdapters;