Skip to content

Commit 788cd6c

Browse files
authored
Merge pull request #274 from alphagov/limit-and-return-active-questions-in-api
Limit and only return active questions in API
2 parents 6a3230d + f842c13 commit 788cd6c

4 files changed

Lines changed: 40 additions & 8 deletions

File tree

app/controllers/api/v0/conversations_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def create
1616
end
1717

1818
def show
19-
answered_questions = @conversation.questions.joins(:answer)
19+
answered_questions = @conversation.questions_for_showing_conversation(only_answered: true)
2020
pending_question = @conversation.questions.unanswered.last
2121

2222
render(
@@ -69,7 +69,7 @@ def answer_feedback
6969

7070
def find_conversation
7171
@conversation = Conversation
72-
.includes(questions: { answer: %i[sources feedback] })
72+
.active
7373
.where(signon_user_id: current_user.id, source: :api)
7474
.find(params[:conversation_id])
7575
end

app/models/conversation.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ class Conversation < ApplicationRecord
1313
},
1414
prefix: true
1515

16-
def questions_for_showing_conversation
17-
Question.where(conversation: self)
18-
.includes(answer: %i[feedback sources])
19-
.active
20-
.last(Rails.configuration.conversations.max_question_count)
16+
def questions_for_showing_conversation(only_answered: false)
17+
scope = Question.where(conversation: self)
18+
.includes(answer: %i[feedback sources])
19+
.active
20+
scope = scope.joins(:answer) if only_answered
21+
scope.last(Rails.configuration.conversations.max_question_count)
2122
end
2223
end

spec/models/conversations_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,28 @@
3333
describe ".questions_for_showing_conversation" do
3434
let(:conversation) { create(:conversation) }
3535

36+
before do
37+
allow(Rails.configuration.conversations).to receive(:max_question_count).and_return(2)
38+
end
39+
3640
it "returns the last N active questions based on the configuration value" do
3741
create(:question, conversation:)
3842
expected = 2.times.map do |_|
3943
create(:question, conversation:)
4044
end
41-
allow(Rails.configuration.conversations).to receive(:max_question_count).and_return(2)
4245
expect(conversation.reload.questions_for_showing_conversation).to eq(expected)
4346
end
47+
48+
context "when only_answered is true" do
49+
it "returns the last N active answered questions based on the configuration value" do
50+
create(:question, :with_answer, conversation:)
51+
expected = 2.times.map do |_|
52+
create(:question, :with_answer, conversation:)
53+
end
54+
create(:question, conversation:)
55+
56+
expect(conversation.reload.questions_for_showing_conversation(only_answered: true)).to eq(expected)
57+
end
58+
end
4459
end
4560
end

spec/requests/api/v0/conversations_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@
164164
expect(response).to have_http_status(:not_found)
165165
end
166166

167+
it "returns a 404 if the conversation has expired" do
168+
conversation = create(:conversation, :api, :expired, signon_user: api_user)
169+
get api_v0_show_conversation_path(conversation)
170+
expect(response).to have_http_status(:not_found)
171+
end
172+
167173
it "returns a 404 if the conversation is not associated with the user" do
168174
different_user = create(:signon_user, :conversation_api)
169175
conversation = create(:conversation, signon_user: different_user)
@@ -234,6 +240,16 @@
234240
end
235241

236242
describe "PUT :update" do
243+
let(:conversation) do
244+
create(
245+
:conversation,
246+
:api,
247+
signon_user:
248+
api_user,
249+
questions: [create(:question, :with_answer)],
250+
)
251+
end
252+
237253
context "when the params are valid" do
238254
let(:user_question) { "What is the capital of France?" }
239255
let(:params) { { user_question: } }

0 commit comments

Comments
 (0)