touchpoints
touchpoints copied to clipboard
Fix question position gaps in Touchpoints API responses
The Touchpoints API was returning non-sequential question positions when questions had been deleted from forms. For example, a form with 6 questions would return positions like [1, 3, 5, 6, 7, 16] instead of the expected sequential values [1, 2, 3, 4, 5, 6].
This occurred because:
- When questions are deleted from forms, the remaining questions retain their original position values
- The API was using the raw
questionsassociation which preserves these database position values with gaps - Frontend applications expect sequential positioning for proper display and functionality
Solution:
- Created a new
QuestionSerializerthat normalizes position values to be sequential starting from 1 - Updated both
FormSerializerandFullFormSerializerto use the new serializer - Modified serializers to use the existing
ordered_questionsmethod which returns questions in the correct order across form sections - Added comprehensive test coverage to verify positions are always sequential
Example of the fix:
# Before: Raw database positions with gaps
[1, 3, 5, 6, 7, 16]
# After: Normalized sequential positions
[1, 2, 3, 4, 5, 6]
The change is backward compatible and only affects the API response format - the underlying database positions remain unchanged to preserve form structure integrity.
Fixes #1876.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.