Skip to content

Commit

Permalink
improve folder + file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
madebyzak committed Mar 11, 2025
1 parent c6e1ee8 commit 0a7718e
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ context('Confirm Swap VOs for PIN Credit Details Page', () => {
cy.task('stubSignIn')
cy.signIn()

cy.visit('/log/swap-vos-pin-credit-details/confirm')
cy.visit('/log/confirm')

cy.contains('Swap visiting orders (VOs) for PIN credit').click()
cy.contains('button', 'Continue').click()
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/e2e/swap-vos-pin-credit-details.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ context('Swap VOs for PIN Credit Details Page', () => {
cy.task('reset')
cy.task('stubSignIn')
cy.signIn()
cy.visit('/log/swap-vos-pin-credit-details')
cy.visit('/log/application-details')

// Navigate through the pages
cy.contains('Swap visiting orders (VOs) for PIN credit').click()
Expand Down
1 change: 1 addition & 0 deletions server/@types/express/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export declare module 'express-session' {
interface ApplicationType {
value: string
name: string
apiValue: string
}

type AdditionalApplicationData = SwapVOsForPinCreditDetails
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,69 @@
import { Request, Response, Router } from 'express'
import { APPLICATION_TYPES } from '../../constants/applicationTypes'
import asyncMiddleware from '../../middleware/asyncMiddleware'
import AuditService, { Page } from '../../services/auditService'
import { getApplicationType } from '../../utils/getApplicationType'
import { updateSessionData } from '../../utils/session'
import { APPLICATION_TYPES } from '../../constants/applicationTypes'

export default function swapVosPinCreditDetailsRoutes({ auditService }: { auditService: AuditService }): Router {
const router = Router()

router.get(
'/log/swap-vos-pin-credit-details',
'/log/application-details',
asyncMiddleware(async (req: Request, res: Response) => {
await auditService.logPageView(Page.LOG_SWAP_VOS_PIN_CREDIT_DETAILS_PAGE, {
who: res.locals.user.username,
correlationId: req.id,
})

const selectedAppType = APPLICATION_TYPES.find(type => type.value === req.session.applicationData?.type.value)
const applicationType = getApplicationType(req.session.applicationData?.type.apiValue)

if (!selectedAppType) {
if (!applicationType) {
return res.redirect('/log/application-type')
}

return res.render('pages/log/swap-vos-pin-credit-details', {
return res.render(`pages/log-application/application-details/${applicationType.value}`, {
title: 'Log swap VOs for PIN credit details',
appTypeTitle: 'Swap VOs for PIN credit',
})
}),
)

router.post(
'/log/swap-vos-pin-credit-details',
'/log/application-details',
asyncMiddleware(async (req: Request, res: Response) => {
const { applicationData } = req.session

const isSwapVOsToPinCredit =
applicationData?.type?.apiValue ===
APPLICATION_TYPES.find(type => type.value === 'swap-visiting-orders-for-pin-credit')?.apiValue

updateSessionData(req, {
additionalData: {
...req.session.applicationData?.additionalData,
swapVOsToPinCreditDetails: req.body.swapVosPinCreditDetails,
...applicationData?.additionalData,
...(isSwapVOsToPinCredit ? { swapVOsToPinCreditDetails: req.body.swapVosPinCreditDetails } : {}),
},
})

res.redirect(`/log/swap-vos-pin-credit-details/confirm`)
res.redirect('/log/confirm')
}),
)

router.get(
'/log/swap-vos-pin-credit-details/confirm',
'/log/confirm',
asyncMiddleware(async (req: Request, res: Response) => {
await auditService.logPageView(Page.CONFIRM_SWAP_VOS_PIN_CREDIT_DETAILS_PAGE, {
who: res.locals.user.username,
correlationId: req.id,
})

const selectedAppType = APPLICATION_TYPES.find(type => type.value === req.session.applicationData?.type.value)
const applicationType = getApplicationType(req.session.applicationData?.type.apiValue)

if (!selectedAppType) {
if (!applicationType) {
return res.redirect('/log/application-type')
}

return res.render('pages/log/confirm-swap-vos-pin-credit-details', {
return res.render(`pages/log-application/confirm/${applicationType.value}`, {
title: 'Check details',
appTypeTitle: 'Swap VOs for PIN credit',
session: req.session,
Expand Down
16 changes: 10 additions & 6 deletions server/routes/applications/applicationTypeRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ export default function applicationTypeRoutes({ auditService }: { auditService:
})

const applicationTypes = APPLICATION_TYPES.map(applicationType => ({
apiValue: applicationType.apiValue,
value: applicationType.value,
text: applicationType.name,
}))

res.render('pages/log/application-type', {
res.render('pages/log-application/select-application-type', {
title: 'Select application type',
applicationTypes,
errorMessage: null,
Expand All @@ -33,13 +34,16 @@ export default function applicationTypeRoutes({ auditService }: { auditService:
asyncMiddleware(async (req: Request, res: Response) => {
const selectedAppType = APPLICATION_TYPES.find(type => type.value === req.body.applicationType)

const applicationTypes = APPLICATION_TYPES.map(applicationType => ({
apiValue: applicationType.apiValue,
value: applicationType.value,
text: applicationType.name,
}))

if (!selectedAppType) {
return res.render('pages/log/application-type', {
return res.render('pages/log-application/select-application-type', {
title: 'Select application type',
applicationTypes: APPLICATION_TYPES.map(applicationType => ({
value: applicationType.value,
text: applicationType.name,
})),
applicationTypes,
errorMessage: 'Choose one',
errorSummary: [{ text: 'Choose one', href: '#applicationType' }],
})
Expand Down
4 changes: 2 additions & 2 deletions server/routes/applications/forwardApplicationRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function forwardApplicationRoutes({
return res.redirect(`/applications/${departmentName}/pending?error=unknown-type`)
}

return res.render(`pages/forward-application/${applicationType.value}`, {
return res.render(`pages/applications/forward/${applicationType.value}`, {
application,
departmentName,
textareaValue: '',
Expand All @@ -64,7 +64,7 @@ export default function forwardApplicationRoutes({
const errors = validateForwardingApplication(forwardToDepartment, forwardingReason)

if (Object.keys(errors).length > 0) {
return res.render(`pages/forward-application/${applicationType.value}`, {
return res.render(`pages/applications/forward/${applicationType.value}`, {
application,
departmentName,
textareaValue: forwardingReason,
Expand Down
4 changes: 2 additions & 2 deletions server/routes/applications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import asyncMiddleware from '../../middleware/asyncMiddleware'
import AuditService, { Page } from '../../services/auditService'
import ManagingPrisonerAppsService from '../../services/managingPrisonerAppsService'
import PrisonService from '../../services/prisonService'
import applicationDetailsRoutes from './applicationDetailsRoutes'
import applicationTypeRoutes from './applicationTypeRoutes'
import forwardApplicationRoutes from './forwardApplicationRoutes'
import prisonerDetailsRoutes from './prisonerDetailsRoutes'
import submitApplicationRoutes from './submitApplicationRoutes'
import swapVosPinCreditDetailsRoutes from './swapVosPinCreditDetailsRoutes'
import viewApplicationRoutes from './viewApplicationsRoutes'

export default function applicationsRoutes({
Expand Down Expand Up @@ -66,7 +66,7 @@ export default function applicationsRoutes({
router.use(forwardApplicationRoutes({ auditService, managingPrisonerAppsService }))
router.use(prisonerDetailsRoutes({ auditService, prisonService }))
router.use(submitApplicationRoutes({ auditService }))
router.use(swapVosPinCreditDetailsRoutes({ auditService }))
router.use(applicationDetailsRoutes({ auditService }))
router.use(viewApplicationRoutes({ auditService, managingPrisonerAppsService }))

return router
Expand Down
4 changes: 2 additions & 2 deletions server/routes/applications/prisonerDetailsRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function prisonerDetailsRoutes({
return res.redirect('/log/application-type')
}

return res.render('pages/log/prisoner-details', {
return res.render('pages/log-application/prisoner-details', {
title: 'Log prisoner details',
appTypeTitle: req.session.applicationData.type.name,
})
Expand Down Expand Up @@ -69,7 +69,7 @@ export default function prisonerDetailsRoutes({
date: req.body.date,
})

res.redirect(`/log/swap-vos-pin-credit-details`)
res.redirect(`/log/application-details`)
}),
)

Expand Down
2 changes: 1 addition & 1 deletion server/routes/applications/submitApplicationRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function submitApplicationRoutes({ auditService }: { auditService
return
}

res.render(`pages/submit-application/${application.type}`, {
res.render(`pages/log-application/submit/${application.type}`, {
title: applicationType.name,
application,
})
Expand Down
2 changes: 1 addition & 1 deletion server/routes/applications/viewApplicationsRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function viewApplicationRoutes({
return res.redirect(`/applications/${departmentName}/pending?error=unknown-type`)
}

return res.render(`pages/view-application/${applicationType.value}`, {
return res.render(`pages/applications/view/${applicationType.value}`, {
title: applicationType.name,
application,
departmentName,
Expand Down
12 changes: 6 additions & 6 deletions server/utils/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,35 @@ describe(updateSessionData.name, () => {

it('should update an existing field without removing other fields', () => {
req.session.applicationData = {
type: { name: 'Swap VOs', value: 'swap-vos' },
type: { name: 'Swap VOs', value: 'swap-vos', apiValue: 'swap-vos' },
prisonerName: 'Jane Doe',
}

updateSessionData(req, { prisonerName: 'John Doe' })

expect(req.session.applicationData).toEqual({
type: { name: 'Swap VOs', value: 'swap-vos' },
type: { name: 'Swap VOs', value: 'swap-vos', apiValue: 'swap-vos' },
prisonerName: 'John Doe',
})
})

it('should add new fields while keeping existing ones', () => {
req.session.applicationData = {
type: { name: 'Swap VOs', value: 'swap-vos' },
type: { name: 'Swap VOs', value: 'swap-vos', apiValue: 'swap-vos' },
}

updateSessionData(req, { prisonerName: 'John Doe', date: new Date('2024-02-05') })

expect(req.session.applicationData).toEqual({
type: { name: 'Swap VOs', value: 'swap-vos' },
type: { name: 'Swap VOs', value: 'swap-vos', apiValue: 'swap-vos' },
prisonerName: 'John Doe',
date: new Date('2024-02-05'),
})
})

it('should not overwrite nested objects but merge updates', () => {
req.session.applicationData = {
type: { name: 'Swap VOs', value: 'swap-vos' },
type: { name: 'Swap VOs', value: 'swap-vos', apiValue: 'swap-vos' },
additionalData: { swapVOsToPinCreditDetails: 'Old value' },
}

Expand All @@ -58,7 +58,7 @@ describe(updateSessionData.name, () => {
})

expect(req.session.applicationData).toEqual({
type: { name: 'Swap VOs', value: 'swap-vos' },
type: { name: 'Swap VOs', value: 'swap-vos', apiValue: 'swap-vos' },
additionalData: { swapVOsToPinCreditDetails: 'New value' },
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% from "govuk/components/character-count/macro.njk" import govukCharacterCount %}
{% from "govuk/components/button/macro.njk" import govukButton %}

{% extends "../../partials/layout.njk" %}
{% extends "../../../partials/layout.njk" %}

{% set pageTitle = applicationName + " - " + title %}
{% set mainClasses = "app-container govuk-body applications-landing-page" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% from "govuk/components/button/macro.njk" import govukButton %}
{% from "govuk/components/summary-list/macro.njk" import govukSummaryList %}

{% extends "../../partials/layout.njk" %}
{% extends "../../../partials/layout.njk" %}

{% set pageTitle = applicationName + " - " + title %}
{% set mainClasses = "app-container govuk-body applications-landing-page" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{%- from "moj/components/date-picker/macro.njk" import mojDatePicker -%}

{% extends "../../partials/layout.njk" %}
{% extends "../../../partials/layout.njk" %}

{% set pageTitle = applicationName + " - " + title %}
{% set mainClasses = "app-container govuk-body applications-landing-page" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% from "govuk/components/back-link/macro.njk" import govukBackLink %}
{% from "govuk/components/error-summary/macro.njk" import govukErrorSummary %}

{% extends "../../partials/layout.njk" %}
{% extends "../../../partials/layout.njk" %}

{% set pageTitle = applicationName + " - " + title %}
{% set mainClasses = "app-container govuk-body applications-landing-page" %}
Expand Down

0 comments on commit 0a7718e

Please sign in to comment.