@ministryofjustice/hmpps-digital-prison-reporting-frontend
Version:
The Digital Prison Reporting Frontend contains templates and code to help display data effectively in UI applications.
42 lines (35 loc) • 1.35 kB
text/typescript
import { RequestHandler } from 'express'
import { Services } from '../../../../../types/Services'
import ErrorSummaryUtils from '../../../../../components/error-summary/utils'
import LocalsHelper from '../../../../../utils/localsHelper'
import AsyncReportUtils from './utils'
export default class ViewAyncReportController {
layoutPath: string
services: Services
constructor(layoutPath: string, services: Services) {
this.layoutPath = layoutPath
this.services = services
}
GET: RequestHandler = async (req, res, next) => {
const { type } = req.params
try {
const params = { req, res, services: this.services, next }
const renderData = await AsyncReportUtils.renderReport(params)
res.render(`dpr/routes/journeys/view-report/report`, {
layoutPath: this.layoutPath,
...renderData,
})
} catch (error) {
const dprError = ErrorSummaryUtils.handleError(error, req.params.type)
let refreshLink
if (dprError.status === 'EXPIRED') {
const { userId } = LocalsHelper.getValues(res)
refreshLink = await this.services.recentlyViewedService.asyncSetToExpiredByTableId(req.params.tableId, userId)
}
req.body.title = `Failed to retrieve ${type}`
req.body.error = dprError
req.body.refreshLink = refreshLink
next()
}
}
}