Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/serializers/form_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class FormSerializer < ActiveModel::Serializer
:last_response_created_at,
:tag_list

has_many :questions
has_many :questions, serializer: QuestionSerializer
belongs_to :service

def questions
object.ordered_questions
end
end
6 changes: 5 additions & 1 deletion app/serializers/full_form_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ def links
:response_count,
:last_response_created_at

has_many :questions
has_many :questions, serializer: QuestionSerializer
has_many :submissions

def questions
object.ordered_questions
end

def submissions
object.submissions.order(:id).where('created_at BETWEEN ? AND ?', start_date, end_date).limit(size).offset(size * page)
end
Expand Down
26 changes: 26 additions & 0 deletions app/serializers/question_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class QuestionSerializer < ActiveModel::Serializer
attributes :id,
:form_id,
:text,
:question_type,
:answer_field,
:position,
:is_required,
:created_at,
:updated_at,
:form_section_id,
:character_limit,
:placeholder_text,
:help_text

has_many :question_options

def position
# Normalize position based on the order within the form's ordered questions
form_questions = object.form.ordered_questions
index = form_questions.index(object)
index ? index + 1 : 1
end
end
31 changes: 31 additions & 0 deletions spec/controllers/api/v1/forms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,37 @@
expect(response.status).to eq(400)
end
end

context 'question positions' do
let!(:user) { FactoryBot.create(:user) }
let!(:organization) { FactoryBot.create(:organization) }
let!(:form) { FactoryBot.create(:form, organization: organization) }
let!(:user_role) { FactoryBot.create(:user_role, :form_manager, user:, form:) }

before do
# Create questions with gaps in positions (simulating deleted questions)
FactoryBot.create(:question, form: form, form_section: form.form_sections.first, position: 1, answer_field: 'answer_01', text: 'Question 1')
FactoryBot.create(:question, form: form, form_section: form.form_sections.first, position: 3, answer_field: 'answer_02', text: 'Question 2')
FactoryBot.create(:question, form: form, form_section: form.form_sections.first, position: 5, answer_field: 'answer_03', text: 'Question 3')
FactoryBot.create(:question, form: form, form_section: form.form_sections.first, position: 6, answer_field: 'answer_04', text: 'Question 4')
FactoryBot.create(:question, form: form, form_section: form.form_sections.first, position: 7, answer_field: 'answer_05', text: 'Question 5')
FactoryBot.create(:question, form: form, form_section: form.form_sections.first, position: 16, answer_field: 'answer_06', text: 'Question 6')

user.update(api_key: TEST_API_KEY)
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(ENV.fetch('API_HTTP_USERNAME'), ENV.fetch('API_HTTP_PASSWORD'))
get :show, format: :json, params: { id: form.short_uuid, 'API_KEY' => user.api_key }
@parsed_response = JSON.parse(response.body)
end

it 'normalizes question positions to be sequential starting from 1' do
questions_data = @parsed_response['data']['relationships']['questions']['data']
expect(questions_data.size).to eq(6)

# Question positions should be normalized to 1, 2, 3, 4, 5, 6
positions = questions_data.map { |q| q['position'] }
expect(positions).to eq([1, 2, 3, 4, 5, 6])
end
end
end
end
end