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.

117 lines (113 loc) 4.41 kB
'use strict'; var UserReports = require('../../../../types/UserReports.js'); var ErrorHandler = require('../../../../utils/ErrorHandler/ErrorHandler.js'); var filtersTypeEnum = require('../../../../components/_filters/filtersTypeEnum.js'); var personalisationUtils = require('../../../../utils/Personalisation/personalisationUtils.js'); var utils = require('../utils.js'); var utils$1 = require('./report/utils.js'); var utils$2 = require('./dashboard/utils.js'); class ViewAsyncController { layoutPath; services; constructor(layoutPath, services) { this.layoutPath = layoutPath; this.services = services; } GET = (type) => { let renderFunction; let displayType; if (type === UserReports.ReportType.REPORT) { renderFunction = utils$1.renderReport; displayType = 'report'; } else { renderFunction = utils$2.renderDashboard; displayType = 'dashboard'; } return async (req, res, next) => { try { // Redirect the same path to attach query string if (utils.redirectWithDefaults(res, req)) { return; } // Get the validation errors const validationErrors = res.locals['validationErrors'] || []; const subscriptionMessages = { subscribedMessage: res.locals['subscribedMessage'] || [], unsubscribedMessage: res.locals['unsubscribedMessage'] || [], }; // get the render config const data = await renderFunction({ req, res, services: this.services, next, }); // If report/dashboard is expired redirect the the polling page to show expired status if (data.expired) { await utils.updateStateToExpiredAndRedirect(req, res, this.services); return; } // Render the report/dashboard res.render(`dpr/routes/journeys/view-report/${displayType}`, { layoutPath: this.layoutPath, ...data, validationErrors, subscriptionMessages, }); } catch (error) { const dprError = new ErrorHandler.ErrorHandler(error).formatError(); req.body ??= {}; req.body.title = `Failed to retrieve ${displayType}`; req.body.error = dprError; next(error); } }; }; resetFilters = async (req, res, next) => { try { const { id, reportId, tableId } = req.params; const sessionKey = { id, reportId, tableId }; await utils.default.resetFilters(req, res, sessionKey); } catch (error) { req.body = { title: 'Failed to reset filters', error: new ErrorHandler.ErrorHandler(error).formatError(), ...(req.body && { ...req.body }), }; next(error); } }; saveDefaultFilterValues = async (req, res, next) => { try { await personalisationUtils.default.saveDefaults(filtersTypeEnum.FiltersType.INTERACTIVE, res, req, this.services); res.redirect(req.baseUrl); } catch (error) { req.body = { title: 'Failed to save defaults', error: new ErrorHandler.ErrorHandler(error).formatError(), ...(req.body && { ...req.body }), }; next(error); } }; removeDefaultFilterValues = async (req, res, next) => { try { await personalisationUtils.default.removeDefaults(filtersTypeEnum.FiltersType.INTERACTIVE, res, req, this.services); res.redirect(req.baseUrl); } catch (error) { req.body = { title: 'Failed to remove defaults', error: new ErrorHandler.ErrorHandler(error).formatError(), ...(req.body && { ...req.body }), }; next(error); } }; } exports.ViewAsyncController = ViewAsyncController; //# sourceMappingURL=controller.js.map