The Immigration & Asylum Case API implements a callback system to handle various stages of case processing. This document explains how the callback system works, focusing on:
- Pre-Submit Callbacks: Executed before a case is submitted/updated
- Post-Submit Callbacks: Executed after a case is successfully submitted/updated
These are four types of callbacks we can configure on an event in CCD. All are optional and only configured based on the requirements.
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ │ │ │ │ │ │ │
│ About to Start │────▶│ Mid Event │────▶│ About to Submit │────▶│ Submitted │
│ │ │ │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘ └─────────────────┘
Pre-Submit Pre-Submit Pre-Submit Post-Submit
Pre-submit callbacks are triggered before a case is submitted or updated. They serve two main purposes:
- Validation: Ensure the case data is valid before proceeding
- Transformation: Modify the case data as needed before submission
- About to Start: Triggered when a user is about to start a specific event
- Mid Event: Triggered during the event, typically after a page is completed but before the event is submitted
- About to Submit: Triggered when a user is about to submit an event
- PreSubmitCallbackController: REST controller that receives pre-submit callback requests
- PreSubmitCallbackDispatcher: Dispatches callback requests to appropriate handlers
- PreSubmitCallbackHandler: Interface implemented by handlers that process pre-submit callbacks
- PreSubmitCallbackStateHandler: Special type of handler that can change the state of a case
Post-submit callbacks are triggered after a case has been successfully submitted or updated. They are typically used for:
- Notifications: Sending emails, texts, or other notifications
- Integration: Triggering processes in other systems
- Audit: Recording information about the completed action
- PostSubmitCallbackController: REST controller that receives post-submit callback requests
- PostSubmitCallbackDispatcher: Dispatches callback requests to appropriate handlers
- PostSubmitCallbackHandler: Interface implemented by handlers that process post-submit callbacks
This controller exposes endpoints for "about to start", "mid event", and "about to submit" callbacks. It:
- Receives callback requests with case data
- Delegates to the PreSubmitCallbackDispatcher
- Returns the (potentially modified) case data and any validation errors
This controller exposes an endpoint for "submitted" callbacks. It:
- Receives callback requests with case data
- Delegates to the PostSubmitCallbackDispatcher
- Returns confirmation messages or other post-submission data
Handlers implement either the PreSubmitCallbackHandler or PostSubmitCallbackHandler interface. Each handler:
- Declares which event types and states it can handle
- Contains business logic for processing the callback
- May modify the case data (pre-submit) or generate confirmation messages (post-submit)
The AddAppealResponseHandler
is an example of a PreSubmitCallbackHandler that:
- Handles the "addAppealResponse" event at the ABOUT_TO_SUBMIT stage
- Validates that required fields are present
- Updates case data with appeal response information
- May change the state of the case
The AgeAssessmentVisibilityHandler
is an example of a PreSubmitCallbackHandler that:
- Handles the "startAppeal" and "editAppeal" events at the MID_EVENT stage
- Checks if the appeal is a non-detained appeal or a non-accelerated detained appeal
- Sets the visibility of age assessment fields based on these conditions
- Demonstrates how MID_EVENT callbacks can be used to control field visibility during form completion
Handlers are automatically discovered and registered through Spring's component scanning. To create a new handler:
- Implement the appropriate interface (PreSubmitCallbackHandler or PostSubmitCallbackHandler)
- Annotate the class with
@Component
- Implement the required methods to specify which events and states the handler supports
- Add your business logic in the
handle
method
When multiple handlers can process the same callback:
- Each handler receives the case data as modified by previous handlers
- Validation errors from any handler will prevent submission
- Validation success response from the callback will determine the success journey/path.
- Keep handlers focused on a single responsibility
- Use clear naming conventions for handlers (e.g.,
[Action]Handler
) - Write comprehensive unit tests for handlers
- Document the purpose and behavior of each handler
- Consider performance implications for complex handlers