@@ -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
0 commit comments