Skip to content

Commit

Permalink
MIA-122: Port of stats page from react app (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackReeveMoJ authored Feb 6, 2025
1 parent d5a4b1d commit 216ed1d
Show file tree
Hide file tree
Showing 20 changed files with 949 additions and 5 deletions.
227 changes: 227 additions & 0 deletions integration_tests/mockApis/keyworkerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,236 @@ const stubKeyworkerApiStatusFail = () =>
},
})

const stubKeyworkerMigrationStatus = () =>
stubFor({
request: {
method: 'GET',
urlPathPattern: '/keyworker-api/key-worker/prison/(.*)',
},
response: {
status: 200,
jsonBody: {
prisonId: 'LEI',
supported: true,
migrated: true,
autoAllocatedSupported: true,
capacityTier1: 6,
capacityTier2: 9,
kwSessionFrequencyInWeeks: 1,
migratedDateTime: '2025-01-01T01:12:55.000',
},
headers: {
'Content-Type': 'application/json',
},
},
})

const stubKeyworkerApiStats2025 = () =>
stubFor({
request: {
method: 'GET',
urlPathPattern: '/keyworker-api/key-worker-stats',
queryParameters: {
prisonId: {
matches: '.+',
},
fromDate: {
matches: '2025.+',
},
toDate: {
matches: '.+',
},
},
},
response: {
status: 200,
headers: { 'Content-Type': 'application/json;charset=UTF-8' },
jsonBody: {
summary: {
requestedFromDate: '2025-01-01',
requestedToDate: '2025-01-31',
current: {
dataRangeFrom: '2025-01-11',
dataRangeTo: '2025-01-11',
numPrisonersAssignedKeyWorker: 1337,
totalNumPrisoners: 1918,
totalNumEligiblePrisoners: 1169,
numberKeyWorkerSessions: 0,
numberKeyWorkerEntries: 0,
numberOfActiveKeyworkers: 8,
percentagePrisonersWithKeyworker: 69.69,
numProjectedKeyworkerSessions: 167,
complianceRate: 0,
},
previous: {
dataRangeFrom: '2024-12-03',
dataRangeTo: '2024-12-03',
numPrisonersAssignedKeyWorker: 1072,
totalNumPrisoners: 1165,
totalNumEligiblePrisoners: 1165,
numberKeyWorkerSessions: 0,
numberKeyWorkerEntries: 0,
numberOfActiveKeyworkers: 6,
percentagePrisonersWithKeyworker: 42,
numProjectedKeyworkerSessions: 166,
complianceRate: 0,
},
complianceTimeline: {
'2024-04-14': 0,
'2024-05-19': 0,
'2024-06-02': 0,
'2024-08-18': 0,
'2024-11-10': 0,
'2024-12-01': 0,
'2025-01-05': 0,
},
avgOverallCompliance: 0,
keyworkerSessionsTimeline: {
'2024-04-14': 0,
'2024-05-19': 0,
'2024-06-02': 0,
'2024-08-18': 0,
'2024-11-10': 0,
'2024-12-01': 0,
'2025-01-05': 0,
},
avgOverallKeyworkerSessions: 0,
},
},
},
})

const stubKeyworkerApiStats2024 = () =>
stubFor({
request: {
method: 'GET',
urlPathPattern: '/keyworker-api/key-worker-stats',
queryParameters: {
prisonId: {
matches: '.+',
},
fromDate: {
matches: '2024.+',
},
toDate: {
matches: '.+',
},
},
},
response: {
status: 200,
headers: { 'Content-Type': 'application/json;charset=UTF-8' },
jsonBody: {
summary: {
requestedFromDate: '2024-01-01',
requestedToDate: '2024-01-31',
current: {
dataRangeFrom: '2024-01-11',
dataRangeTo: '2024-01-11',
numPrisonersAssignedKeyWorker: 4200,
totalNumPrisoners: 6900,
totalNumEligiblePrisoners: 5000,
numberKeyWorkerSessions: 1,
numberKeyWorkerEntries: 1,
numberOfActiveKeyworkers: 24,
percentagePrisonersWithKeyworker: 61,
numProjectedKeyworkerSessions: 200,
complianceRate: 0,
},
previous: {
dataRangeFrom: '2023-12-03',
dataRangeTo: '2023-12-03',
numPrisonersAssignedKeyWorker: 4205,
totalNumPrisoners: 7000,
totalNumEligiblePrisoners: 6500,
numberKeyWorkerSessions: 3,
numberKeyWorkerEntries: 5,
numberOfActiveKeyworkers: 2,
percentagePrisonersWithKeyworker: 60,
numProjectedKeyworkerSessions: 169,
complianceRate: 0,
},
complianceTimeline: {
'2023-04-14': 0,
'2023-05-19': 0,
'2023-06-02': 0,
'2023-08-18': 0,
'2023-11-10': 0,
'2023-12-01': 0,
'2024-01-05': 0,
},
avgOverallCompliance: 0,
keyworkerSessionsTimeline: {
'2023-04-14': 0,
'2023-05-19': 0,
'2023-06-02': 0,
'2023-08-18': 0,
'2023-11-10': 0,
'2023-12-01': 0,
'2024-01-05': 0,
},
avgOverallKeyworkerSessions: 0,
},
},
},
})

const stubKeyworkerApiStatsNoData = () =>
stubFor({
request: {
method: 'GET',
urlPathPattern: '/keyworker-api/key-worker-stats',
queryParameters: {
prisonId: {
matches: '.+',
},
fromDate: {
matches: '.+',
},
toDate: {
matches: '.+',
},
},
},
response: {
status: 200,
headers: { 'Content-Type': 'application/json;charset=UTF-8' },
jsonBody: {
summary: {
requestedFromDate: '2025-01-01',
requestedToDate: '2025-01-31',
complianceTimeline: {
'2024-04-14': 0,
'2024-05-19': 0,
'2024-06-02': 0,
'2024-08-18': 0,
'2024-11-10': 0,
'2024-12-01': 0,
'2025-01-05': 0,
},
avgOverallCompliance: 0,
keyworkerSessionsTimeline: {
'2024-04-14': 0,
'2024-05-19': 0,
'2024-06-02': 0,
'2024-08-18': 0,
'2024-11-10': 0,
'2024-12-01': 0,
'2025-01-05': 0,
},
avgOverallKeyworkerSessions: 0,
},
},
},
})

export default {
stubKeyworkerApiHealth,
stubKeyworkerApiStatusIsKeyworker: () => stubKeyworkerApiStatusIsKeyworker(true),
stubKeyworkerApiStatusIsNotKeyworker: () => stubKeyworkerApiStatusIsKeyworker(false),
stubKeyworkerApiStatusFail: () => stubKeyworkerApiStatusFail(),
stubKeyworkerApiStats2025,
stubKeyworkerApiStats2024,
stubKeyworkerMigrationStatus,
stubKeyworkerApiStatsNoData,
}
23 changes: 22 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"connect-flash": "^0.1.1",
"connect-redis": "^8.0.1",
"csrf-sync": "^4.0.3",
"date-fns": "^4.1.0",
"express": "^4.21.2",
"express-session": "^1.18.1",
"govuk-frontend": "^5.8.0",
Expand All @@ -120,7 +121,8 @@
"passport-oauth2": "^1.8.0",
"redis": "^4.7.0",
"superagent": "^10.1.1",
"uuid": "^11.0.5"
"uuid": "^11.0.5",
"zod": "^3.24.1"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
Expand Down
2 changes: 2 additions & 0 deletions server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type { Services } from './services'
import populateClientToken from './middleware/populateSystemClientToken'
import breadcrumbs from './middleware/breadcrumbs'
import populateUserPermissions from './middleware/permissionsMiddleware'
import populateValidationErrors from './middleware/populateValidationErrors'

export default function createApp(services: Services): express.Application {
const app = express()
Expand Down Expand Up @@ -72,6 +73,7 @@ export default function createApp(services: Services): express.Application {
})
app.use(breadcrumbs())
app.use(dpsComponents.retrieveCaseLoadData({ logger }))
app.use(populateValidationErrors())

app.get('/not-authorised', (_, res) => {
res.status(403)
Expand Down
16 changes: 16 additions & 0 deletions server/middleware/populateValidationErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { RequestHandler } from 'express'
import { FLASH_KEY__FORM_RESPONSES, FLASH_KEY__VALIDATION_ERRORS } from '../utils/constants'

export default function populateValidationErrors(): RequestHandler {
return async (req, res, next) => {
const validationErrors = req.flash(FLASH_KEY__VALIDATION_ERRORS)[0]
const formResponses = req.flash(FLASH_KEY__FORM_RESPONSES)[0]
if (validationErrors) {
res.locals['validationErrors'] = JSON.parse(validationErrors)
}
if (formResponses) {
res.locals['formResponses'] = JSON.parse(formResponses)
}
next()
}
}
Loading

0 comments on commit 216ed1d

Please sign in to comment.