UNPKG

@ministryofjustice/hmpps-digital-prison-reporting-frontend

Version:

The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.

88 lines (85 loc) 3.15 kB
import logger from './logger.js'; import { queryObjectToQs } from './queryMappers.js'; function getActiveJourneyValue(req, keyParts, // { reportId, id, tableId?, executionId? } field) { const store = req.session?.activeReport; if (!store) return undefined; logger.info('getActiveJourneyValue', keyParts, field); const { reportId, id, executionId, tableId } = keyParts; const baseKey = buildJourneyKey({ reportId, id }); const execKey = executionId ? buildJourneyKey({ reportId, id, executionId }) : undefined; const tableKey = tableId ? buildJourneyKey({ reportId, id, tableId }) : undefined; // 1. Table key takes highest priority (final async stage) if (tableKey && store[tableKey]) { const value = store[tableKey][field]; logger.info('getActiveJourneyValue: tableKey', { field, value }); return value; } // 2. Execution key is next (loading async stage) if (execKey && store[execKey]) { const value = store[execKey][field]; logger.info('getActiveJourneyValue: execKey', { field, value }); return value; } // 3. Fall back to base key (sync or start of async) const value = store[baseKey]?.[field]; logger.info('getActiveJourneyValue: base', { field, value }); return value; } /** * Sets a key value on the active report session * * @template F * @param {Request} req * @param {ActiveJourneyKey} keyParts * @param {F} field * @param {ActiveReportSessionData[F]} value */ function setActiveJourneyValue(req, keyParts, field, value) { if (!req.session) return; if (!req.session.activeReport) { req.session.activeReport = {}; } const store = req.session.activeReport; const { reportId, id, executionId, tableId } = keyParts; const baseKey = buildJourneyKey({ reportId, id }); const execKey = executionId ? buildJourneyKey({ reportId, id, executionId }) : undefined; const tableKey = tableId ? buildJourneyKey({ reportId, id, tableId }) : undefined; const targetKey = tableKey ?? execKey ?? baseKey; if (!store[targetKey]) { store[targetKey] = {}; } store[targetKey][field] = value; } /** * sets the current sort search on the active report session * * @export * @param {Request} req * @param {{ reportId: string; id: string; tableId?: string }} sessionKey * @param {(Record<string, string> | undefined)} sortData */ function setActiveJourneySortSearch(req, sessionKey, sortData) { if (sortData) { setActiveJourneyValue(req, sessionKey, 'requestedSortSearch', queryObjectToQs(sortData)); } } /** * Builds the active journey key * * @export * @param {ActiveJourneyKey} { reportId, id, type, executionId, tableId } * @return {*} {string} */ function buildJourneyKey({ reportId, id, executionId, tableId }) { const parts = [reportId, id]; if (executionId) parts.push(`exec:${executionId}`); if (tableId) parts.push(`table:${tableId}`); return parts.join(':'); } export { buildJourneyKey, getActiveJourneyValue, setActiveJourneySortSearch }; //# sourceMappingURL=sessionHelper.js.map