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.

115 lines (112 loc) 4.31 kB
import { ReportType } from '../../../../types/UserReports.js'; import { ErrorHandler } from '../../../../utils/ErrorHandler/ErrorHandler.js'; import { FiltersType } from '../../../../components/_filters/filtersTypeEnum.js'; import PersonalisationUtils from '../../../../utils/Personalisation/personalisationUtils.js'; import ViewReportUtils, { redirectWithDefaults, updateStateToExpiredAndRedirect } from '../utils.js'; import { renderReport } from './report/utils.js'; import { renderDashboard } from './dashboard/utils.js'; class ViewAsyncController { layoutPath; services; constructor(layoutPath, services) { this.layoutPath = layoutPath; this.services = services; } GET = (type) => { let renderFunction; let displayType; if (type === ReportType.REPORT) { renderFunction = renderReport; displayType = 'report'; } else { renderFunction = renderDashboard; displayType = 'dashboard'; } return async (req, res, next) => { try { // Redirect the same path to attach query string if (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 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(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 ViewReportUtils.resetFilters(req, res, sessionKey); } catch (error) { req.body = { title: 'Failed to reset filters', error: new ErrorHandler(error).formatError(), ...(req.body && { ...req.body }), }; next(error); } }; saveDefaultFilterValues = async (req, res, next) => { try { await PersonalisationUtils.saveDefaults(FiltersType.INTERACTIVE, res, req, this.services); res.redirect(req.baseUrl); } catch (error) { req.body = { title: 'Failed to save defaults', error: new ErrorHandler(error).formatError(), ...(req.body && { ...req.body }), }; next(error); } }; removeDefaultFilterValues = async (req, res, next) => { try { await PersonalisationUtils.removeDefaults(FiltersType.INTERACTIVE, res, req, this.services); res.redirect(req.baseUrl); } catch (error) { req.body = { title: 'Failed to remove defaults', error: new ErrorHandler(error).formatError(), ...(req.body && { ...req.body }), }; next(error); } }; } export { ViewAsyncController }; //# sourceMappingURL=controller.js.map