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.

176 lines (173 loc) 6.5 kB
import { ReportType, LoadType } from '../../types/UserReports.js'; import { FiltersType } from '../_filters/filtersTypeEnum.js'; import { apiTimestampToUiDateTime } from '../../utils/dateHelper.js'; import { getActiveJourneyValue } from '../../utils/sessionHelper.js'; import { qsToQueryObject } from '../../utils/queryMappers.js'; import localsHelper from '../../utils/localsHelper.js'; import { buildAppliedFilters } from '../_filters/filters-applied/utils.js'; import { setUpDownload } from '../../routes/journeys/download-report/utils.js'; import FiltersUtils from '../_filters/utils.js'; import ReportActionsUtils from '../_reports/report-heading/report-actions/utils.js'; import { setUpBookmark } from '../bookmark/utils.js'; import { getFields, getDashboardFields } from '../../utils/definitionUtils.js'; import { setupSubscriptionConfig } from '../subscription/utils.js'; class DataPresentation { services; res; req; definition; loadType; type; requestData; // Meta id; reportId; tableId; token; userId; expired = false; extractedRequestData; reportDetails; // Structure fields; // Request reportQuery; actions; // Filters filterData; appliedFilters; savedDefaultsConfig; constructor(services, res, req, definition, loadType, type, requestData) { this.services = services; this.res = res; this.req = req; this.definition = definition; this.loadType = loadType; this.type = type; this.requestData = requestData; // From locals this.token = res.locals['dprUser'].token; this.userId = res.locals['dprUser'].id; // From params this.id = this.req.params['id']; this.reportId = this.req.params['reportId']; this.tableId = this.req.params['tableId']; this.getFields(); } /** * Gets and sets the fields by report types * */ getFields = () => { this.fields = this.type === ReportType.REPORT ? getFields(this.definition) : getDashboardFields(this.definition); }; /** * Builds the report meta data * */ buildReportMeta = (type) => { const { csrfToken } = localsHelper.getValues(this.res); return { id: this.id, reportId: this.reportId, ...(this.tableId && { tableId: this.tableId }), loadType: this.loadType, csrfToken, type, }; }; /** * Build the filters */ buildFilters = async () => { this.filterData = await FiltersUtils.getInteractiveFilters({ fields: this.fields ?? [], req: this.req, }); }; /** * Builds the applied filters buttons * */ buildAppliedFilters = () => { const sessionKey = { id: this.id, reportId: this.reportId, tableId: this.tableId }; this.appliedFilters = buildAppliedFilters(this.req, sessionKey, this.fields ?? []); }; /** * Builds the saved defaults config * */ buildSavedDefaultsConfig = async () => { this.savedDefaultsConfig = { hasDefaults: await this.services.defaultFilterValuesService.hasDefaults(this.userId, this.reportId, this.id, FiltersType.INTERACTIVE), saveDefaultsEnabled: this.res.app.locals['saveDefaultsEnabled'], }; }; /** * Extracts the relevant data from the requested report data * * @param {RequestedReport} requestData * @return {*} {ExtractedRequestData} */ extractDataFromRequest = (requestData) => { const { query, url, timestamp } = requestData; return { executionId: requestData.executionId, requestedTimestamp: apiTimestampToUiDateTime(timestamp.requested), querySummary: query?.summary || [], queryData: query?.data, requestUrl: url?.request, defaultQuery: url?.report?.default, dataProductDefinitionsPath: requestData.dataProductDefinitionsPath, }; }; /** * Builds the report actions: * - download config * - bookmark config * - general actions */ buildActions = async (type, schedule) => { const { tableId, reportId, id } = this.req.params; this.extractedRequestData = this.requestData ? this.extractDataFromRequest(this.requestData) : undefined; // Setup bookmark const bookmarkConfig = setUpBookmark(this.res, this.req, this.services.bookmarkService); // Setup subscribe const subscriptionConfig = await setupSubscriptionConfig(this.req, this.res, reportId, id, schedule, this.services); // Setup download const downloadConfig = type === ReportType.REPORT ? setUpDownload(this.res, this.req) : undefined; // Setup other actions const actions = ReportActionsUtils.setActions(this.res, this.req, this.reportDetails, downloadConfig, this.requestData); // Get the feedback submission path const sessionKey = this.loadType === LoadType.SYNC ? { id, reportId } : { id, reportId, tableId }; const feedbackSubmissionFormPath = getActiveJourneyValue(this.req, sessionKey, 'feedbackSubmissionFormPath'); this.actions = { actions, downloadConfig, bookmarkConfig, subscriptionConfig, feedbackFormHref: feedbackSubmissionFormPath, }; }; /** * Gets the current request query * */ getCurrentQuery = () => { // Filters from query string // 1. Initialise the filters query to the defaults from the DPD const interactiveDefaultSearch = getActiveJourneyValue(this.req, { id: this.id, reportId: this.reportId }, 'interactiveDefaultFiltersSearch'); let filtersQuery = interactiveDefaultSearch ? qsToQueryObject(interactiveDefaultSearch, 'filters.') : {}; // 2. Get the search params from the current report and use those if they are present const currentSearch = getActiveJourneyValue(this.req, { id: this.id, reportId: this.reportId, tableId: this.tableId }, 'currentReportFiltersSearch'); if (currentSearch) { filtersQuery = qsToQueryObject(currentSearch, 'filters.'); } return filtersQuery; }; } export { DataPresentation as default }; //# sourceMappingURL=DataPresentation.js.map