Skip to content

Commit 4bbf13a

Browse files
Update previous mammograms flows (#185)
* Get editing and deleting previous mammograms working * Clean up mammogram display * Clean up summary list keys
1 parent d5d8540 commit 4bbf13a

File tree

10 files changed

+572
-385
lines changed

10 files changed

+572
-385
lines changed

app/data/users.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,45 @@ module.exports = [
2424
2525
id: 'e1945412-aad7-46a2-a984-d4f6f654c229',
2626
breastScreeningUnit: 'm5ekcxvu'
27+
},
28+
{
29+
firstName: 'Claudia',
30+
lastName: 'Patterson',
31+
role: ['clinician'],
32+
33+
id: '7b2d8f4e-3c1a-4e9b-8f6d-2a5c9e1b4d3f',
34+
breastScreeningUnit: 'm5ekcxvu'
35+
},
36+
{
37+
firstName: 'Priya',
38+
lastName: 'Sharma',
39+
role: ['clinician'],
40+
41+
id: 'c4e6f2a8-9d5b-4c3e-a1f7-8b2d4e6c9a1b',
42+
breastScreeningUnit: 'm5ekcxvu'
43+
},
44+
{
45+
firstName: 'Agatha',
46+
lastName: 'Thompson',
47+
role: ['clinician'],
48+
49+
id: 'f3a1c5e7-2b4d-4a6e-9c8f-1d3b5e7a9c2d',
50+
breastScreeningUnit: 'm5ekcxvu'
51+
},
52+
{
53+
firstName: 'Rachel',
54+
lastName: 'Bennett',
55+
role: ['administrative'],
56+
57+
id: 'a9c2e4f6-1d3b-4e5c-8a7f-2c4d6e8b1a3c',
58+
breastScreeningUnit: 'm5ekcxvu'
59+
},
60+
{
61+
firstName: 'Mohammed',
62+
lastName: 'Ali',
63+
role: ['administrative'],
64+
65+
id: 'e2d4f6a8-3c5b-4e7d-9a1c-6b8e2d4f6a9c',
66+
breastScreeningUnit: 'm5ekcxvu'
2767
}
2868
]

app/routes/events.js

Lines changed: 137 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,26 @@ module.exports = (router) => {
207207
? `/clinics/${req.params.clinicId}/events/${req.params.eventId}/${returnTo}`
208208
: defaultDestination
209209

210-
res.redirect(finalDestination)
210+
// Preserve all query string parameters except returnTo (already used)
211+
// Todo: could a library do this for us?
212+
const queryParams = { ...req.query }
213+
delete queryParams.returnTo
214+
const queryString = Object.keys(queryParams).length
215+
? '?' +
216+
Object.entries(queryParams)
217+
.map(([key, value]) =>
218+
Array.isArray(value)
219+
? value
220+
.map(
221+
(v) => `${encodeURIComponent(key)}=${encodeURIComponent(v)}`
222+
)
223+
.join('&')
224+
: `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
225+
)
226+
.join('&')
227+
: ''
228+
229+
res.redirect(finalDestination + queryString)
211230
})
212231

213232
// Leave appointment - revert status from in_progress back to checked_in
@@ -387,28 +406,75 @@ module.exports = (router) => {
387406
router.get(
388407
'/clinics/:clinicId/events/:eventId/previous-mammograms/add',
389408
(req, res) => {
409+
const { clinicId, eventId } = req.params
390410
delete req.session.data?.event?.previousMammogramTemp
391-
res.render('events/previous-mammograms/edit')
411+
// Redirect to the form page
412+
res.redirect(
413+
urlWithReferrer(
414+
`/clinics/${clinicId}/events/${eventId}/previous-mammograms/form`,
415+
req.query.referrerChain,
416+
req.query.scrollTo
417+
)
418+
)
392419
}
393420
)
394421

395422
// Save data about a mammogram
396423
router.post(
397-
'/clinics/:clinicId/events/:eventId/previous-mammograms-answer',
424+
'/clinics/:clinicId/events/:eventId/previous-mammograms/save',
398425
(req, res) => {
399426
const { clinicId, eventId } = req.params
400427
const data = req.session.data
401-
const previousMammogram = data.event?.previousMammogramTemp
428+
const previousMammogramTemp = data.event?.previousMammogramTemp
402429
const action = req.body.action
403430
const referrerChain = req.query.referrerChain
404431
const scrollTo = req.query.scrollTo
405432

406-
const mammogramAddedMessage = 'Previous mammogram added'
433+
// Check if editing existing or creating new
434+
const isNewMammogram = !previousMammogramTemp?.id
435+
436+
const mammogramAddedMessage = isNewMammogram
437+
? 'Previous mammogram added'
438+
: 'Previous mammogram updated'
439+
440+
// Helper function to build mammogram object with ID and metadata
441+
const buildMammogramObject = (tempData, additionalFields = {}) => {
442+
const mammogram = {
443+
id: tempData.id || generateId(),
444+
...tempData,
445+
...additionalFields
446+
}
447+
448+
// Add metadata for new mammograms
449+
if (isNewMammogram) {
450+
mammogram.dateAdded = new Date().toISOString()
451+
mammogram.addedBy = data.currentUser.id
452+
}
453+
454+
return mammogram
455+
}
456+
457+
// Helper function to save mammogram (update existing or add new)
458+
const saveMammogram = (mammogram) => {
459+
if (!data.event.previousMammograms) {
460+
data.event.previousMammograms = []
461+
}
462+
463+
// Update existing or add new
464+
const existingIndex = data.event.previousMammograms.findIndex(
465+
(m) => m.id === mammogram.id
466+
)
467+
if (existingIndex !== -1) {
468+
data.event.previousMammograms[existingIndex] = mammogram
469+
} else {
470+
data.event.previousMammograms.push(mammogram)
471+
}
472+
}
407473

408474
// Check if this is coming from "proceed anyway" page
409475
if (action === 'proceed-anyway') {
410476
// Validate that a reason was provided
411-
if (!previousMammogram?.overrideReason) {
477+
if (!previousMammogramTemp?.overrideReason) {
412478
// Set error in flash and redirect back to proceed-anyway page
413479
req.flash('error', {
414480
text: 'Enter a reason for proceeding with this appointment',
@@ -419,20 +485,15 @@ module.exports = (router) => {
419485
)
420486
}
421487

422-
// Save the mammogram with override flag and reason
423-
if (!data.event.previousMammograms) {
424-
data.event.previousMammograms = []
425-
}
426-
427-
data.event.previousMammograms.push({
428-
...previousMammogram,
488+
// Build and save the mammogram with override flag
489+
const mammogram = buildMammogramObject(previousMammogramTemp, {
429490
warningOverridden: true
430491
})
492+
saveMammogram(mammogram)
431493

432494
req.flash('success', mammogramAddedMessage)
433495

434496
delete data.event?.previousMammogramTemp
435-
// return res.redirect(`/clinics/${clinicId}/events/${eventId}`)
436497

437498
const returnUrl = getReturnUrl(
438499
`/clinics/${clinicId}/events/${eventId}`,
@@ -452,11 +513,9 @@ module.exports = (router) => {
452513
data.event.appointmentStopped.stoppedReason = 'recent_mammogram'
453514
data.event.appointmentStopped.needsReschedule = 'no' // Default to no reschedule needed
454515

455-
// Add the mammogram to history
456-
if (!data.event.previousMammograms) {
457-
data.event.previousMammograms = []
458-
}
459-
data.event.previousMammograms.push(previousMammogram)
516+
// Build and save the mammogram
517+
const mammogram = buildMammogramObject(previousMammogramTemp)
518+
saveMammogram(mammogram)
460519
delete data.event?.previousMammogramTemp
461520

462521
// Save changes and update status
@@ -478,7 +537,7 @@ module.exports = (router) => {
478537
}
479538

480539
// Check if this is a recent mammogram (within 6 months)
481-
const isRecentMammogram = checkIfRecentMammogram(previousMammogram)
540+
const isRecentMammogram = checkIfRecentMammogram(previousMammogramTemp)
482541

483542
// If recent mammogram detected and not already coming from warning page
484543
if (isRecentMammogram && action !== 'continue') {
@@ -492,11 +551,9 @@ module.exports = (router) => {
492551
}
493552

494553
// Normal flow - save the mammogram
495-
if (previousMammogram) {
496-
if (!data.event.previousMammograms) {
497-
data.event.previousMammograms = []
498-
}
499-
data.event.previousMammograms.push(previousMammogram)
554+
if (previousMammogramTemp) {
555+
const mammogram = buildMammogramObject(previousMammogramTemp)
556+
saveMammogram(mammogram)
500557
}
501558

502559
delete data.event?.previousMammogramTemp
@@ -526,6 +583,61 @@ module.exports = (router) => {
526583
}
527584
)
528585

586+
// Edit existing previous mammogram
587+
router.get(
588+
'/clinics/:clinicId/events/:eventId/previous-mammograms/edit/:mammogramId',
589+
(req, res) => {
590+
const { clinicId, eventId, mammogramId } = req.params
591+
const data = req.session.data
592+
593+
// Find the mammogram by ID
594+
const mammogram = data.event?.previousMammograms?.find(
595+
(m) => m.id === mammogramId
596+
)
597+
598+
if (mammogram) {
599+
// Copy to temp for editing
600+
data.event.previousMammogramTemp = { ...mammogram }
601+
} else {
602+
console.log(`Cannot find previous mammogram with ID ${mammogramId}`)
603+
}
604+
605+
// Redirect to the form page
606+
res.redirect(
607+
urlWithReferrer(
608+
`/clinics/${clinicId}/events/${eventId}/previous-mammograms/form`,
609+
req.query.referrerChain,
610+
req.query.scrollTo
611+
)
612+
)
613+
}
614+
)
615+
616+
// Delete previous mammogram
617+
router.get(
618+
'/clinics/:clinicId/events/:eventId/previous-mammograms/delete/:mammogramId',
619+
(req, res) => {
620+
const { clinicId, eventId, mammogramId } = req.params
621+
const data = req.session.data
622+
623+
// Remove mammogram from array
624+
if (data.event?.previousMammograms) {
625+
data.event.previousMammograms = data.event.previousMammograms.filter(
626+
(m) => m.id !== mammogramId
627+
)
628+
}
629+
630+
req.flash('success', 'Previous mammogram deleted')
631+
632+
const returnUrl = getReturnUrl(
633+
`/clinics/${clinicId}/events/${eventId}`,
634+
req.query.referrerChain,
635+
req.query.scrollTo
636+
)
637+
res.redirect(returnUrl)
638+
}
639+
)
640+
529641
// Helper function to check if mammogram was taken within the last 6 months
530642
function checkIfRecentMammogram(mammogram) {
531643
if (!mammogram) return false

app/views/_includes/medical-information/index.njk

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
{% set scrollTo = sectionId %}
1515

1616
{% set mammogramHistoryHtml %}
17-
{# {% include "summary-lists/medical-information/mammogram-history.njk" %} #}
17+
{% include "_includes/medical-information/mammogram-history.njk" %}
1818

19-
{% call summaryList() %}
20-
{% set noBorder = true %}
21-
{% include "_includes/summary-lists/rows/last-known-mammogram.njk" %}
22-
{% endcall %}
19+
{% if allowEdits %}
20+
{{ button({
21+
text: "Add another mammogram",
22+
href: './previous-mammograms/add' | urlWithReferrer(currentUrl, scrollTo),
23+
classes: "nhsuk-button--secondary app-button--small"
24+
}) }}
25+
{% endif %}
2326
{% endset %}
2427

2528
{% set mammogramHistoryContentsSummary %}

0 commit comments

Comments
 (0)