touchpoints icon indicating copy to clipboard operation
touchpoints copied to clipboard

Fix question position gaps in Touchpoints API responses

Open Copilot opened this issue 5 months ago • 2 comments

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:

  1. When questions are deleted from forms, the remaining questions retain their original position values
  2. The API was using the raw questions association which preserves these database position values with gaps
  3. Frontend applications expect sequential positioning for proper display and functionality

Solution:

  • Created a new QuestionSerializer that normalizes position values to be sequential starting from 1
  • Updated both FormSerializer and FullFormSerializer to use the new serializer
  • Modified serializers to use the existing ordered_questions method 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.

Copilot avatar Aug 19 '25 19:08 Copilot