Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions app/controllers/patient.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import _ from 'lodash'

import { ArchiveRecordReason } from '../enums.js'
import programmes from '../datasets/programmes.js'
import { ArchiveRecordReason, ProgrammePreset } from '../enums.js'
import { Patient } from '../models/patient.js'
import { Programme } from '../models/programme.js'
import { getDateValueDifference } from '../utils/date.js'
import { getResults, getPagination } from '../utils/pagination.js'

export const patientController = {
Expand All @@ -13,10 +16,36 @@ export const patientController = {

response.locals.patient = patient

response.locals.recordTitle = patient.post16
const recordTitle = patient.post16
? __('patient.label').replace('Child', 'Patient')
: __('patient.label')

const activeProgrammes = Object.values(ProgrammePreset)
.filter((preset) => preset.active)
.flatMap((preset) => preset.primaryProgrammeTypes)
.map((programmeType) => new Programme(programmes[programmeType]))

response.locals.activeProgrammes = activeProgrammes

response.locals.recordTitle = recordTitle

response.locals.secondaryNavigationItems = [
{
text: recordTitle,
href: patient.uri,
current: request.originalUrl === patient.uri
},
...Object.values(activeProgrammes).map((programme) => {
const href = `${patient.uri}/programmes/${programme.slug}`

return {
text: programme.name,
href,
current: request.originalUrl === href
}
})
]

response.locals.archiveRecordReasonItems = Object.values(
ArchiveRecordReason
)
Expand Down Expand Up @@ -178,6 +207,45 @@ export const patientController = {
response.redirect(paths.next)
},

readProgramme(request, response, next) {
const { programme_slug } = request.params
const { data } = request.session
const { patient } = response.locals

if (!programme_slug) {
return response.redirect(patient.uri)
}

response.locals.programme = Object.values(programmes)
.map((programme) => new Programme(programme))
.find(({ slug }) => slug === programme_slug)

response.locals.programmeAuditEvents = patient.auditEvents
.filter(({ programme_ids }) =>
programme_ids?.some((programme_id) =>
programme_id.startsWith(programme_slug)
)
)
.sort((a, b) => getDateValueDifference(b.createdAt, a.createdAt))
.reverse()

response.locals.programmePatientSessions = patient.patientSessions
.filter(({ programme_id }) => programme_id?.startsWith(programme_slug))
.sort((a, b) => getDateValueDifference(b.createdAt, a.createdAt))

const { vaccinations } = Patient.read(patient.nhsn, data)

response.locals.vaccination = vaccinations
.filter(({ programme_id }) => programme_id?.startsWith(programme_slug))
.find(({ given }) => given)

next()
},

showProgramme(request, response) {
response.render(`patient/programme`)
},

archive(request, response) {
const { account } = request.app.locals
const { data } = request.session
Expand Down
4 changes: 2 additions & 2 deletions app/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ export const SchoolTerm = {
export const ProgrammePreset = {
SeasonalFlu: {
name: 'Flu',
active: true,
active: false,
primaryProgrammeTypes: [ProgrammeType.Flu],
term: SchoolTerm.Autumn
},
HPV: {
name: 'HPV',
active: false,
active: true,
adolescent: true,
primaryProgrammeTypes: [ProgrammeType.HPV],
term: SchoolTerm.Spring
Expand Down
3 changes: 3 additions & 0 deletions app/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,9 @@ export const en = {
status:
'{{patient.fullName}} is no longer eligible for school age immunisations'
},
programmeEligibility: {
text: 'Eligible from %s'
},
fullNameAndNhsn: {
label: 'Name and NHS number'
},
Expand Down
56 changes: 54 additions & 2 deletions app/models/patient.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { fakerEN_GB as faker } from '@faker-js/faker'
import _ from 'lodash'

import programmes from '../datasets/programmes.js'
import schools from '../datasets/schools.js'
import { AuditEventType, NoticeType } from '../enums.js'
import { getDateValueDifference, removeDays, today } from '../utils/date.js'
import { AcademicYear, AuditEventType, NoticeType } from '../enums.js'
import {
formatDate,
getDateValueDifference,
removeDays,
today
} from '../utils/date.js'
import { tokenize } from '../utils/object.js'
import { getProgrammeEligibilityDate } from '../utils/patient.js'
import { getPreferredNames } from '../utils/reply.js'
import {
formatLink,
Expand Down Expand Up @@ -265,6 +272,51 @@ export class Patient extends Child {
return []
}

/**
* Get programme eligibility
*
* @returns {object} Eligibility per programme
*/
get programmeEligibility() {
const academicYears = Object.values(AcademicYear)
const currentAcademicYear = academicYears.at(-1)
const eligibility = {}

for (const programmeType of Object.keys(programmes)) {
const eligibilityDate = getProgrammeEligibilityDate(
this.yearGroup,
programmes[programmeType].yearGroups[0],
currentAcademicYear
)

eligibility[programmeType] = formatDate(eligibilityDate, {
dateStyle: 'long'
})
}

return eligibility
}

/**
* Get programme outcomes
*
* @returns {object} Outcome per programme
*/
get programmeOutcomes() {
const outcomes = {}

for (const programmeType of Object.keys(programmes)) {
const vaccination = this.vaccinations.find(
({ programme }) => programme.type === programmeType
)

outcomes[programmeType] =
vaccination || this.programmeEligibility[programmeType]
}

return outcomes
}

/**
* Get vaccinations
*
Expand Down
10 changes: 10 additions & 0 deletions app/models/programme.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import vaccines from '../datasets/vaccines.js'
import { ProgrammeStatus, VaccineMethod } from '../enums.js'
import { isBetweenDates, today } from '../utils/date.js'
import {
camelToKebabCase,
formatLink,
formatTag,
sentenceCaseProgrammeName
Expand Down Expand Up @@ -71,6 +72,15 @@ export class Programme {
return sentenceCaseProgrammeName(this.type)
}

/**
* Get programme name as a slug
*
* @returns {string} Programme name
*/
get slug() {
return camelToKebabCase(this.type).replace('/', '-')
}

/**
* Get programme name shown within tag component
*
Expand Down
3 changes: 3 additions & 0 deletions app/routes/patient.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ router.post('/:nhsn/edit/:view', patient.updateForm)

router.post('/:nhsn/archive', patient.archive)

router.all('/:nhsn/programmes{/:programme_slug}', patient.readProgramme)
router.get('/:nhsn/programmes{/:programme_slug}', patient.showProgramme)

router.get('/:nhsn{/:view}', patient.show)

export const patientRoutes = router
24 changes: 24 additions & 0 deletions app/utils/patient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Get patient (programme) outcome status properties
*
* @param {number} childYearGroup - Child’s year group
* @param {number} programmeYearGroup - Programme’s year group
* @param {import('../enums.js').AcademicYear} academicYear - Academic year
* @returns {Date} Date child becomes eligible for programme
*/
export function getProgrammeEligibilityDate(
childYearGroup,
programmeYearGroup,
academicYear
) {
// Extract the start year from current academic year string
const currentStartYear = parseInt(academicYear.split(' ')[0])

// Calculate how many years until the child reaches programme year group
const yearsUntilEligible = programmeYearGroup - childYearGroup

// Calculate the eligible academic year start
const eligibleStartYear = currentStartYear + yearsUntilEligible

return new Date(`${eligibleStartYear}-09-01`)
}
10 changes: 1 addition & 9 deletions app/views/patient/_navigation.njk
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@
}) if patient.archived }}

{{ appSecondaryNavigation({
items: [{
text: recordTitle,
href: params.patient.uri,
current: params.view == "show"
}, {
text: __("patient.events.title"),
href: params.patient.uri + "/events",
current: params.view == "events"
}]
items: secondaryNavigationItems
}) }}

<h2 class="nhsuk-heading-m nhsuk-u-visually-hidden">
Expand Down
2 changes: 1 addition & 1 deletion app/views/patient/events.njk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{% endblock %}

{% block content %}
<div class="nhsuk-u-width-three-quarters">
<div class="nhsuk-u-width-full">
{{ super() }}

{{ patientNavigation({
Expand Down
117 changes: 117 additions & 0 deletions app/views/patient/programme.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{% from "patient/_navigation.njk" import patientNavigation with context %}

{% extends "_layouts/default.njk" %}

{% set title = patient.fullName + " – " + __("patient.events.title") %}

{% block beforeContent %}
{{ breadcrumb({
items: [{
text: __("home.show.title"),
href: "/"
}, {
text: __("patient.list.title"),
href: "/patients"
}]
}) }}
{% endblock %}

{% block content %}
<div class="nhsuk-u-width-three-quarters">
{{ super() }}

{{ patientNavigation({
patient: patient
}) }}

{% if vaccination %}
{% set vaccinationDescriptionHtml %}
{{ summaryList({
rows: summaryRows(vaccination, {
programme: {},
outcome: {},
vaccine_snomed: {},
method: {},
site: {},
dose: {},
sequence: {},
batch: {},
identifiedBy: {},
suppliedBy: {},
createdBy: {},
createdAt: {},
updatedAt: {},
location: {},
protocol: {},
note: {},
syncStatus: {}
})
}) }}

{{ button({
classes: "nhsuk-button--secondary nhsuk-u-margin-0",
text: __("vaccination.edit.title"),
href: vaccination.uri + "/edit"
}) }}
{% endset %}

{{ card({
heading: __("vaccination.label"),
headingClasses: "nhsuk-heading-m",
descriptionHtml: vaccinationDescriptionHtml
}) }}
{% else %}
{{ __("patient.programmeEligibility.text", patient.programmeEligibility[programme.type]) | nhsukMarkdown }}
{% endif %}

{% set sessionsDescriptionHtml %}
{% if programmePatientSessions.length %}
{% set patientSessionRows = [] %}
{% for patientSession in programmePatientSessions %}
{% set patientSessionRows = patientSessionRows | push([
{
header: __("session.location.label"),
html: link(patientSession.uri, patientSession.session.location.name)
},
{
header: __("session.dates.label"),
html: patientSession.session.formatted.dates if patientSession.session.dates.length else patientSession.session.status
},
{
header: "Status",
html: patientSession.formatted.status.report
}
]) %}
{% endfor %}

{{ table({
id: "patient-sessions",
sort: "name",
responsive: true,
head: [
{ text: __("session.location.label") },
{ text: __("session.dates.label") },
{ text: "Status" }
],
rows: patientSessionRows
}) }}
{% else %}
{{ __n("session.count", 0) | nhsukMarkdown }}
{% endif %}
{% endset %}

{{ card({
heading: __("session.label"),
headingLevel: 3,
descriptionHtml: sessionsDescriptionHtml
}) if programmePatientSessions.length }}

{{ card({
heading: "Activity",
headingLevel: 3,
descriptionHtml: appTimeline({
items: timelineItems(programmeAuditEvents)
})
}) if programmeAuditEvents.length }}
</div>
{% endblock %}
Loading