@@ -127,13 +127,12 @@ module.exports = router => {
127127 next ( )
128128 } )
129129
130- // Main route in to starting an event - used to clear any temp data
131- // Possibly not needed as if the ID doesn't match our use route would reset this - but this could
132- // be used to reset a patient back to defaults
130+ // Main route in to starting an event - used to clear any temp data, set status to in progress and store the user id of the mammographer doing the appointment
133131 router . get ( '/clinics/:clinicId/events/:eventId/start' , ( req , res ) => {
134132 const data = req . session . data
135133 const event = getEvent ( data , req . params . eventId )
136134 const currentUser = data . currentUser
135+ const returnTo = req . query . returnTo // Used by /index so we can 'start' an appointment but then go to a different page.
137136
138137 if ( event ?. status !== 'event_in_progress' ) {
139138 // Update status
@@ -148,22 +147,77 @@ module.exports = router => {
148147 } )
149148 }
150149
151- // Explicitly delete the temp event data just in case
152- // On next request this will be recreated from the event array
153- delete data . event
154- console . log ( 'Cleared temp event data' )
150+ // Parse and apply workflow status from query parameters
151+ // This lets links in index.njk pre-complete certain sections
152+ // Look for parameters like event[workflowStatus][section]=completed
153+ if ( req . query . event && req . query . event . workflowStatus ) {
154+ const workflowUpdates = req . query . event . workflowStatus
155+ console . log ( 'Applying workflow status updates:' , workflowUpdates )
156+
157+ updateEventData ( data , req . params . eventId , {
158+ workflowStatus : workflowUpdates
159+ } )
160+ }
161+
162+ // Determine redirect destination
163+ // This lets us deep link in to the flow whilst still going through this setup route
164+ const defaultDestination = `/clinics/${ req . params . clinicId } /events/${ req . params . eventId } /identity`
165+ const finalDestination = returnTo
166+ ? `/clinics/${ req . params . clinicId } /events/${ req . params . eventId } /${ returnTo } `
167+ : defaultDestination
155168
156- res . redirect ( `/clinics/${ req . params . clinicId } /events/${ req . params . eventId } /identity` )
169+ res . redirect ( finalDestination )
170+ } )
171+
172+ // Leave appointment - revert status from in_progress back to checked_in
173+ router . get ( '/clinics/:clinicId/events/:eventId/leave' , ( req , res ) => {
174+ const { clinicId, eventId } = req . params
175+ const data = req . session . data
176+ const event = getEvent ( data , eventId )
177+
178+ // Only allow leaving if the event is currently in progress
179+ if ( event ?. status === 'event_in_progress' ) {
180+ // Save any temporary changes before leaving
181+ saveTempEventToEvent ( data )
182+ saveTempParticipantToParticipant ( data )
183+
184+ // Revert status back to checked in
185+ updateEventStatus ( data , eventId , 'event_checked_in' )
186+
187+ // Clear session details
188+ updateEventData ( data , eventId , {
189+ sessionDetails : {
190+ startedAt : null ,
191+ startedBy : null
192+ }
193+ } )
194+
195+ // Clear temporary session data (now safe since we've saved changes)
196+ delete data . event
197+ delete data . participant
198+
199+ console . log ( 'Left appointment - saved temp data, reverted status to checked_in, and cleared temp data' )
200+
201+ // req.flash('info', 'You have left the appointment. The participant remains checked in.')
202+ }
203+
204+ // Use referrer chain for redirect, fallback to clinic view
205+ const returnUrl = getReturnUrl ( `/clinics/${ clinicId } ` , req . query . referrerChain )
206+ res . redirect ( returnUrl )
157207 } )
158208
159209 // Event within clinic context
160210 router . get ( '/clinics/:clinicId/events/:eventId' , ( req , res ) => {
161- res . render ( 'events/show' , {
162- } )
211+ const { clinicId, eventId } = req . params
212+ // res.render('events/show', {
213+ // })
214+ res . redirect ( `/clinics/${ clinicId } /events/${ eventId } /appointment` )
163215 } )
164216
165217
166218
219+
220+
167221 router . post ( '/clinics/:clinicId/events/:eventId/personal-details/ethnicity-answer' , ( req , res ) => {
168222 const { clinicId, eventId } = req . params
169223 const data = req . session . data
@@ -269,6 +323,8 @@ module.exports = router => {
269323 const data = req . session . data
270324 const previousMammogram = data . event ?. previousMammogramTemp
271325 const action = req . body . action
326+ const referrerChain = req . query . referrerChain
327+ const scrollTo = req . query . scrollTo
272328
273329 const mammogramAddedMessage = 'Previous mammogram added'
274330
@@ -297,7 +353,11 @@ module.exports = router => {
297353 req . flash ( 'success' , mammogramAddedMessage )
298354
299355 delete data . event ?. previousMammogramTemp
300- return res . redirect ( `/clinics/${ clinicId } /events/${ eventId } ` )
356+ // return res.redirect(`/clinics/${clinicId}/events/${eventId}`)
357+
358+ const returnUrl = getReturnUrl ( `/clinics/${ clinicId } /events/${ eventId } ` , referrerChain , scrollTo )
359+
360+ res . redirect ( returnUrl )
301361 }
302362
303363 // Handle the direct cancel action from appointment-should-not-proceed.html
@@ -339,7 +399,7 @@ module.exports = router => {
339399
340400 // If recent mammogram detected and not already coming from warning page
341401 if ( isRecentMammogram && action !== 'continue' ) {
342- return res . redirect ( `/clinics/${ clinicId } /events/${ eventId } /previous-mammograms/appointment-should-not-proceed` )
402+ return res . redirect ( urlWithReferrer ( `/clinics/${ clinicId } /events/${ eventId } /previous-mammograms/appointment-should-not-proceed` , referrerChain , scrollTo ) )
343403 }
344404
345405 // Normal flow - save the mammogram
@@ -366,7 +426,9 @@ module.exports = router => {
366426
367427 req . flash ( 'success' , mammogramAddedMessage )
368428
369- res . redirect ( `/clinics/${ clinicId } /events/${ eventId } ` )
429+ const returnUrl = getReturnUrl ( `/clinics/${ clinicId } /events/${ eventId } ` , referrerChain , scrollTo )
430+ res . redirect ( returnUrl )
431+
370432 } )
371433
372434 // Helper function to check if mammogram was taken within the last 6 months
0 commit comments