@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
72 lines (69 loc) • 2.71 kB
JavaScript
import { ErrorHandler } from '../../../../../utils/ErrorHandler/ErrorHandler.js';
import ViewReportUtils from '../../utils.js';
import { LoadType } from '../../../../../types/UserReports.js';
import { renderReport } from './utils.js';
class ViewSyncReportController {
layoutPath;
services;
constructor(layoutPath, services) {
this.layoutPath = layoutPath;
this.services = services;
}
GET = async (req, res, next) => {
try {
// Redirect the same path to attach query string
if (ViewReportUtils.redirectWithDefaults(res, req)) {
return;
}
// Get the validation errors
const validationErrors = res.locals['validationErrors'] || [];
// Render the report
const renderData = await renderReport({ req, res, services: this.services });
res.render(`dpr/routes/journeys/view-report/report`, {
layoutPath: this.layoutPath,
...renderData,
validationErrors,
});
}
catch (error) {
req.body ??= {};
req.body.title = `Report Failed`;
req.body.errorDescription = 'We were unable to show this report for the following reason:';
req.body.error = new ErrorHandler(error).formatError();
next(error);
}
};
// -----------------------------
// Filters
// -----------------------------
applyFilters = async (req, res, _next) => {
await ViewReportUtils.applyReportInteractiveQuery(req, res, this.services, 'filters', LoadType.SYNC);
};
// -----------------------------
// Columns
// -----------------------------
applyColumns = async (req, res, _next) => {
await ViewReportUtils.applyReportInteractiveQuery(req, res, this.services, 'columns', LoadType.SYNC);
};
resetColumns = async (req, res, next) => {
try {
const { id, reportId } = req.params;
// Create the final querystring
const finalQuery = ViewReportUtils.resetColumnsQueryString(req, { id, reportId });
// Redirect with new query string - query string will handle all rendered elements
if (finalQuery) {
res.redirect(`${req.baseUrl}?${finalQuery}`);
}
}
catch (error) {
req.body = {
title: 'Failed to reset filters',
error: new ErrorHandler(error).formatError(),
...(req.body && { ...req.body }),
};
next(error);
}
};
}
export { ViewSyncReportController };
//# sourceMappingURL=controller.js.map