Skip to content

Commit

Permalink
Add StatisticsReportUtils and associated njk macro
Browse files Browse the repository at this point in the history
  • Loading branch information
jsrobertson committed Sep 30, 2024
1 parent 23151ff commit 1d0b938
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import RoshAnalysisUtils from './risksAndNeeds/roshAnalysisUtils'
import ThinkingAndBehavingUtils from './risksAndNeeds/thinkingAndBehavingUtils'
import RouteUtils from './routeUtils'
import SentenceInformationUtils from './sentenceInformationUtils'
import StatisticsReportUtils from './statisticsReportUtils'
import StringUtils from './stringUtils'
import TypeUtils from './typeUtils'
import UserUtils from './userUtils'
Expand Down Expand Up @@ -65,6 +66,7 @@ export {
SentenceInformationUtils,
ShowReferralUtils,
ShowRisksAndNeedsUtils,
StatisticsReportUtils,
StringUtils,
ThinkingAndBehavingUtils,
TypeUtils,
Expand Down
76 changes: 76 additions & 0 deletions server/utils/statisticsReportUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import StatisticsReportUtils from './statisticsReportUtils'
import type { ReportContent } from '@accredited-programmes-api'

describe('StatisticsReportUtils', () => {
describe('reportContentDataBlock', () => {
const reportContent: ReportContent = {
content: {
count: 1,
courseCounts: [
{
audience: 'audience',
count: 1,
name: 'name',
},
],
},
parameters: {
endDate: '2022-01-01',
startDate: '2022-01-31',
},
reportType: 'REFERRAL_COUNT',
}
it('returns the report content data block', () => {
expect(StatisticsReportUtils.reportContentDataBlock(reportContent, new Date('2022-01-01'))).toEqual({
date: 'January 2022',
testId: 'referral-count',
title: 'Total referrals submitted',
value: '1',
})
})

describe('when report content is `null`', () => {
it('returns the correct values', () => {
expect(StatisticsReportUtils.reportContentDataBlock(null, new Date('2022-01-01'))).toEqual(
expect.objectContaining({
testId: 'unknown',
value: '0',
}),
)
})
})

describe('when there is no count value', () => {
it('returns the correct values', () => {
reportContent.content.count = undefined
expect(StatisticsReportUtils.reportContentDataBlock(reportContent, new Date('2022-01-01'))).toEqual(
expect.objectContaining({
value: '0',
}),
)
})
})
})

describe('reportContentTitle', () => {
it('returns the title for the given report type', () => {
expect(StatisticsReportUtils.reportContentTitle('REFERRAL_COUNT')).toEqual('Total referrals submitted')
expect(StatisticsReportUtils.reportContentTitle('PROGRAMME_COMPLETE_COUNT')).toEqual('Total programmes completed')
expect(StatisticsReportUtils.reportContentTitle('NOT_ELIGIBLE_COUNT')).toEqual('Total referrals not eligible')
expect(StatisticsReportUtils.reportContentTitle('WITHDRAWN_COUNT')).toEqual('Total referrals withdrawn')
expect(StatisticsReportUtils.reportContentTitle('DESELECTED_COUNT')).toEqual('Total referrals deselected')
})

describe('when the report type is unknown', () => {
it('returns "Unknown"', () => {
expect(StatisticsReportUtils.reportContentTitle('UNKNOWN')).toEqual('Unknown')
})
})

describe('when the report type is undefined', () => {
it('returns "Unknown"', () => {
expect(StatisticsReportUtils.reportContentTitle(undefined)).toEqual('Unknown')
})
})
})
})
38 changes: 38 additions & 0 deletions server/utils/statisticsReportUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { ReportContent } from '@accredited-programmes-api'

interface ReportContentDataBlock {
date: string
testId: string
title: string
value: string
}

export default class StatisticsReportUtils {
static reportContentDataBlock(reportContent: ReportContent | null, date: Date): ReportContentDataBlock {
const monthAndYear = date.toLocaleDateString('en-GB', { month: 'long', year: 'numeric' })

return {
date: monthAndYear,
testId: reportContent?.reportType.replace(/_/g, '-').toLowerCase() || 'unknown',
title: this.reportContentTitle(reportContent?.reportType),
value: reportContent?.content.count?.toString() || '0',
}
}

static reportContentTitle(reportType?: ReportContent['reportType']): string {
switch (reportType) {
case 'REFERRAL_COUNT':
return 'Total referrals submitted'
case 'PROGRAMME_COMPLETE_COUNT':
return 'Total programmes completed'
case 'NOT_ELIGIBLE_COUNT':
return 'Total referrals not eligible'
case 'WITHDRAWN_COUNT':
return 'Total referrals withdrawn'
case 'DESELECTED_COUNT':
return 'Total referrals deselected'
default:
return 'Unknown'
}
}
}
8 changes: 8 additions & 0 deletions server/views/reports/_reportDataBlock.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% macro reportDataBlock(data) %}
<div data-testid={{ data.testId }}>
<h3 class="govuk-heading-s">{{ data.title }}</h3>
<p class="govuk-body">
<span class="govuk-heading-xl govuk-!-display-inline">{{ data.value }}</span> in {{ data.date }}
</p>
</div>
{% endmacro %}

0 comments on commit 1d0b938

Please sign in to comment.