Skip to content
Merged
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
179 changes: 179 additions & 0 deletions app/controllers/appointment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import _ from 'lodash'

import { ClinicBooking, Session } from '../models.js'
import { getResults, getPagination } from '../utils/pagination.js'

export const appointmentController = {
read(request, response, next, appointment_uuid) {
console.log(`read: ${appointment_uuid}`)

next()
},

readAll(request, response, next) {
const { session_id } = request.params
let appointments = ClinicBooking.findAll(request.session.data)
?.flatMap(({ appointments }) => appointments)
.filter(({ patient_uuid }) => !patient_uuid)

// Sort
appointments = _.sortBy(appointments, 'startAt')

// Session appointments
if (session_id) {
const session = Session.findOne(session_id, request.session.data)
response.locals.session = session

appointments = appointments.filter(
(appointment) => appointment.session_id === session_id
)
}

response.locals.appointments = appointments
response.locals.appointmentsPath = session_id
? `/sessions/${session_id}/appointments`
: '/appointments'
response.locals.results = getResults(appointments, request.query)
response.locals.pages = getPagination(appointments, request.query)

next()
},

show(request, response) {
const view = request.params.view || 'show'

response.render(`appointments/${view}`)
},

list(request, response) {
response.render('appointments/list')
}

// readMatches(request, response, next) {
// let { hasMissingNhsNumber, page, limit, q } = request.query
// const { data } = request.session

// let patients = Patient.findAll(data)

// // Sort
// patients = _.sortBy(patients, 'lastName')

// // Paginate
// page = parseInt(page) || 1
// limit = parseInt(limit) || 50

// // Query
// if (q) {
// patients = patients.filter((patient) =>
// patient.tokenized.includes(String(q).toLowerCase())
// )
// }

// // Filter by missing NHS number
// if (hasMissingNhsNumber) {
// patients = patients.filter((patient) => patient.hasMissingNhsNumber)
// }

// // Toggle initial view
// response.locals.initial =
// Object.keys(request.query).filter((key) => key !== 'referrer').length ===
// 0

// // Results
// response.locals.patients = patients
// response.locals.results = getResults(patients, page, limit)
// response.locals.pages = getPagination(patients, request.query)

// // Clean up session data
// delete data.hasMissingNhsNumber
// delete data.q

// next()
// },

// filterMatches(request, response) {
// const { hasMissingNhsNumber, q } = request.body
// const { consent } = response.locals
// const params = new URLSearchParams()

// if (q) {
// params.append('q', String(q))
// }

// if (hasMissingNhsNumber?.includes('true')) {
// params.append('hasMissingNhsNumber', 'true')
// }

// response.redirect(`${consent.uri}/match?${params}`)
// },

// link(request, response) {
// const { consent_uuid } = request.params
// const { data } = request.session
// const { __, consent, patient, consentsPath } = response.locals

// // Link consent with patient record
// consent.linkToPatient(patient)

// // Update session data
// Consent.update(consent_uuid, consent, data)
// Patient.update(patient.uuid, patient, data)

// request.flash('success', __(`consent.link.success`, { consent, patient }))

// response.redirect(consentsPath)
// },

// add(request, response) {
// const { consent_uuid } = request.params
// const { data } = request.session
// const { __, consent, consentsPath } = response.locals

// // Create patient
// const patient = Patient.create(consent.child, data)

// // Create and add patient session
// const patientSession = PatientSession.create(
// {
// patient_uuid: patient.uuid,
// programme_id: consent.programme_id,
// session_id: consent.session_id
// },
// data
// )

// // Add to session
// patient.addToSession(patientSession)

// // Invite parent to give consent
// patient.requestConsent(patientSession)

// // Link consent with patient record
// consent.linkToPatient(patient)

// // Update session data
// Consent.update(consent_uuid, consent, data)
// Patient.update(patient.uuid, patient, data)

// request.flash('success', __(`consent.add.success`, { consent, patient }))

// response.redirect(consentsPath)
// },

// invalidate(request, response) {
// const { note } = request.body.consent
// const { consent_uuid } = request.params
// const { data } = request.session
// const { __, consentsPath } = response.locals

// // Clean up session data
// delete data.consent

// // Update session data
// const consent = Consent.update(consent_uuid, { invalid: true, note }, data)

// request.flash('success', __(`consent.invalidate.success`, { consent }))

// response.redirect(consentsPath)
// }
}
89 changes: 48 additions & 41 deletions app/controllers/book-into-a-clinic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,67 @@ import { fakerEN_GB as faker } from '@faker-js/faker'
import wizard from '@x-govuk/govuk-prototype-wizard'
import _ from 'lodash'

import { ParentalRelationship, SessionPresets } from '../enums.js'
import { ParentalRelationship } from '../enums.js'
import { ClinicBooking } from '../models.js'
import {
getAllAppointmentPaths,
getHealthQuestionPaths,
getPreviousAddressItems
} from '../utils/clinic-appointment.js'
import {
ConjunctionType,
programmeNamesListForSentence
} from '../utils/programme.js'
import { kebabToCamelCase } from '../utils/string.js'

export const bookIntoClinicController = {
read(request, response, next, session_preset_slug) {
setupServiceHeader(request, response, next) {
const serviceName = 'Book into a clinic'

response.locals.assetsName = 'public'
response.locals.serviceName = serviceName
response.locals.headerOptions = { service: { text: serviceName } }

// Record the session preset (aka "primary programme" to the parent)
const sessionPreset =
SessionPresets.find((preset) => preset.slug === session_preset_slug) ??
SessionPresets[0]
response.locals.sessionPreset = sessionPreset

// Allow us to offer a phone booking if not wanting online (start.njk)
response.locals.bookingPhoneNumber =
request.session.data.teams[0]?.tel ??
faker.helpers.replaceSymbols('01### ######')

next()
},

redirect(request, response) {
const { sessionPreset } = response.locals
readProgrammes(request, response) {
const { data } = request.session

// Read the invited programme IDs from the querystring and store them
const { programme_id } = request.query
let programme_ids
if (programme_id) {
programme_ids = Array.isArray(programme_id)
? programme_id
: [programme_id]

data.clinicInvite = {
programme_ids,
programmeNames: programmeNamesListForSentence(
programme_ids,
ConjunctionType.and,
data
)
}
}

response.redirect(`${request.baseUrl}/${sessionPreset.slug}/start`)
response.redirect(`/book-into-a-clinic/start`)
},

new(request, response) {
const { data } = request.session
const { sessionPreset } = response.locals

// Create a new clinic booking in the wizard context
const booking = ClinicBooking.create(
{
sessionPreset
},
data.wizard
)
const booking = ClinicBooking.create({}, data.wizard)

// Redirect to the first page in the booking journey (after the start page, that is)
const redirectUrl = `${request.baseUrl}/${booking.bookingUri}/new/child-count`
response.redirect(redirectUrl)
},

readForm(request, response, next) {
const { session_preset_slug, booking_uuid } = request.params
const { booking_uuid } = request.params
const appointment_uuid = request.params.appointment_uuid
const { data, referrer } = request.session

Expand Down Expand Up @@ -115,30 +120,29 @@ export const bookIntoClinicController = {
}

const journey = {
[`/${session_preset_slug}`]: {},
[`/${session_preset_slug}/${booking_uuid}/new/child-count`]: {},
[`/`]: {},
[`/${booking_uuid}/new/child-count`]: {},

// Appointment journey; once per child
...getAllAppointmentPaths(
session_preset_slug,
booking_uuid,
request.session.data,
booking.appointments
),

// Parent journey
[`/${session_preset_slug}/${booking_uuid}/new/parent`]: {
[`/${session_preset_slug}/${booking_uuid}/new/offer-health-questions`]:
() => !request.session.data.booking?.parent?.tel
[`/${booking_uuid}/new/parent`]: {
[`/${booking_uuid}/new/offer-health-questions`]: () =>
!request.session.data.booking?.parent?.tel
},
[`/${session_preset_slug}/${booking_uuid}/new/contact-preference`]: {},
[`/${booking_uuid}/new/contact-preference`]: {},

// Check answers
[`/${session_preset_slug}/${booking_uuid}/new/check-answers`]: {},
[`/${booking_uuid}/new/check-answers`]: {},

// Health questions (optional)
[`/${session_preset_slug}/${booking_uuid}/new/offer-health-questions`]: {
[`/${session_preset_slug}/${booking_uuid}/new/confirmation`]: {
[`/${booking_uuid}/new/offer-health-questions`]: {
[`/${booking_uuid}/new/confirmation`]: {
data: 'transaction.optedIntoHealthQuestions',
value: 'false'
}
Expand All @@ -147,14 +151,14 @@ export const bookIntoClinicController = {
// For each child being booked in, and their selected vaccinations, ask the
// relevant health questions and impairments/adjustments questions
...getHealthQuestionPaths(
`/${session_preset_slug}/${booking_uuid}/new/`,
`/${booking_uuid}/new/`,
booking_uuid,
data.wizard,
data
),

// Confirmation! \o/
[`/${session_preset_slug}/${booking_uuid}/new/confirmation`]: {}
[`/${booking_uuid}/new/confirmation`]: {}
}

const paths = wizard(journey, request)
Expand Down Expand Up @@ -223,8 +227,6 @@ export const bookIntoClinicController = {
_.merge(data.wizard.transaction, request.body.transaction)
}

let nextUrl = paths.next

if (view === 'child-count') {
// We've just set the child count, so create the appointments we'll need
const booking = ClinicBooking.findOne(booking_uuid, data.wizard)
Expand All @@ -245,8 +247,8 @@ export const bookIntoClinicController = {

// Start the appointment journey for the first child
const firstAppointment = booking.appointments[0]
const firstAppointmentUrl = `${request.baseUrl}/${booking.bookingUri}/new/${firstAppointment.appointmentUri}/child`
nextUrl = firstAppointmentUrl
const firstAppointmentUrl = `${request.baseUrl}/${booking.bookingUri}/new/${firstAppointment.uuid}/child`
paths.next = firstAppointmentUrl
} else if (
view === 'address-selection' &&
request.body.transaction.addressChoice !== 'new'
Expand All @@ -269,13 +271,18 @@ export const bookIntoClinicController = {

// NB: request.session.save was needed to avoid race condition issues on heroku
request.session.save((error) => {
if (!error) response.redirect(nextUrl)
if (!error) response.redirect(paths.next)
})
},

show(request, response) {
const view = request.params.view || 'start'

// Allow us to offer a phone booking if not wanting online (start.njk)
response.locals.bookingPhoneNumber =
request.session.data.teams[0]?.tel ??
faker.helpers.replaceSymbols('01### ######')

response.render(`book-into-a-clinic/${view}`)
}
}
Loading