|
| 1 | +import { Request, Response, Router } from 'express' |
| 2 | +import asyncMiddleware from '../../middleware/asyncMiddleware' |
| 3 | +import AuditService, { Page } from '../../services/auditService' |
| 4 | +import ManagingPrisonerAppsService from '../../services/managingPrisonerAppsService' |
| 5 | +import { getApplicationType } from '../../utils/getApplicationType' |
| 6 | +import { validateForwardingApplication } from '../validate/validateForwardingApplication' |
| 7 | + |
| 8 | +export default function forwardApplicationRoutes({ |
| 9 | + auditService, |
| 10 | + managingPrisonerAppsService, |
| 11 | +}: { |
| 12 | + auditService: AuditService |
| 13 | + managingPrisonerAppsService: ManagingPrisonerAppsService |
| 14 | +}): Router { |
| 15 | + const router = Router() |
| 16 | + |
| 17 | + router.get( |
| 18 | + '/applications/:departmentName/:prisonerId/:applicationId/forward', |
| 19 | + asyncMiddleware(async (req: Request, res: Response) => { |
| 20 | + const { departmentName, prisonerId, applicationId } = req.params |
| 21 | + const { user } = res.locals |
| 22 | + |
| 23 | + const application = await managingPrisonerAppsService.getPrisonerApp(prisonerId, applicationId, user) |
| 24 | + |
| 25 | + if (!application) { |
| 26 | + return res.redirect(`/applications/${departmentName}/pending`) |
| 27 | + } |
| 28 | + |
| 29 | + await auditService.logPageView(Page.FORWARD_APPLICATION_PAGE, { |
| 30 | + who: user.username, |
| 31 | + correlationId: req.id, |
| 32 | + }) |
| 33 | + |
| 34 | + const applicationType = getApplicationType(application.type) |
| 35 | + |
| 36 | + if (!applicationType) { |
| 37 | + return res.redirect(`/applications/${departmentName}/pending?error=unknown-type`) |
| 38 | + } |
| 39 | + |
| 40 | + return res.render(`pages/forward-application/${applicationType.value}`, { |
| 41 | + application, |
| 42 | + departmentName, |
| 43 | + textareaValue: '', |
| 44 | + title: "Forward this application to swap VO's", |
| 45 | + errors: null, |
| 46 | + }) |
| 47 | + }), |
| 48 | + ) |
| 49 | + |
| 50 | + router.post( |
| 51 | + '/applications/:departmentName/:prisonerId/:applicationId/forward', |
| 52 | + asyncMiddleware(async (req: Request, res: Response) => { |
| 53 | + const { departmentName, prisonerId, applicationId } = req.params |
| 54 | + const { forwardToDepartment, forwardingReason } = req.body |
| 55 | + const { user } = res.locals |
| 56 | + |
| 57 | + const application = await managingPrisonerAppsService.getPrisonerApp(prisonerId, applicationId, user) |
| 58 | + |
| 59 | + if (!application) { |
| 60 | + return res.redirect(`/applications/${departmentName}/pending`) |
| 61 | + } |
| 62 | + |
| 63 | + const applicationType = getApplicationType(application.type) |
| 64 | + const errors = validateForwardingApplication(forwardToDepartment, forwardingReason) |
| 65 | + |
| 66 | + if (Object.keys(errors).length > 0) { |
| 67 | + return res.render(`pages/forward-application/${applicationType.value}`, { |
| 68 | + application, |
| 69 | + departmentName, |
| 70 | + textareaValue: forwardingReason, |
| 71 | + title: "Forward this application to swap VO's", |
| 72 | + errors, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + await managingPrisonerAppsService.forwardApp(prisonerId, applicationId, forwardToDepartment, user) |
| 77 | + |
| 78 | + return res.redirect(`/applications/${departmentName}/${prisonerId}/${applicationId}`) |
| 79 | + }), |
| 80 | + ) |
| 81 | + |
| 82 | + return router |
| 83 | +} |
0 commit comments