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.

178 lines (174 loc) 6.61 kB
'use strict'; var UserReports = require('../../types/UserReports.js'); var filtersTypeEnum = require('../_filters/filtersTypeEnum.js'); var dateHelper = require('../../utils/dateHelper.js'); var sessionHelper = require('../../utils/sessionHelper.js'); var queryMappers = require('../../utils/queryMappers.js'); var localsHelper = require('../../utils/localsHelper.js'); var utils$1 = require('../_filters/filters-applied/utils.js'); var utils$4 = require('../../routes/journeys/download-report/utils.js'); var utils = require('../_filters/utils.js'); var utils$5 = require('../_reports/report-heading/report-actions/utils.js'); var utils$2 = require('../bookmark/utils.js'); var definitionUtils = require('../../utils/definitionUtils.js'); var utils$3 = require('../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 === UserReports.ReportType.REPORT ? definitionUtils.getFields(this.definition) : definitionUtils.getDashboardFields(this.definition); }; /** * Builds the report meta data * */ buildReportMeta = (type) => { const { csrfToken } = localsHelper.default.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 utils.default.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 = utils$1.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, filtersTypeEnum.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: dateHelper.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 = utils$2.setUpBookmark(this.res, this.req, this.services.bookmarkService); // Setup subscribe const subscriptionConfig = await utils$3.setupSubscriptionConfig(this.req, this.res, reportId, id, schedule, this.services); // Setup download const downloadConfig = type === UserReports.ReportType.REPORT ? utils$4.setUpDownload(this.res, this.req) : undefined; // Setup other actions const actions = utils$5.default.setActions(this.res, this.req, this.reportDetails, downloadConfig, this.requestData); // Get the feedback submission path const sessionKey = this.loadType === UserReports.LoadType.SYNC ? { id, reportId } : { id, reportId, tableId }; const feedbackSubmissionFormPath = sessionHelper.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 = sessionHelper.getActiveJourneyValue(this.req, { id: this.id, reportId: this.reportId }, 'interactiveDefaultFiltersSearch'); let filtersQuery = interactiveDefaultSearch ? queryMappers.qsToQueryObject(interactiveDefaultSearch, 'filters.') : {}; // 2. Get the search params from the current report and use those if they are present const currentSearch = sessionHelper.getActiveJourneyValue(this.req, { id: this.id, reportId: this.reportId, tableId: this.tableId }, 'currentReportFiltersSearch'); if (currentSearch) { filtersQuery = queryMappers.qsToQueryObject(currentSearch, 'filters.'); } return filtersQuery; }; } module.exports = DataPresentation; //# sourceMappingURL=DataPresentation.js.map