Skip to content

Commit bba2f03

Browse files
committed
feat(products): support search_term on product index endpoint
## Context The product catalog list in the app exposes a search box, and the ProductsQuery already supports a search_term (ransacking name and code). The REST index action did not forward it, so the endpoint could not be filtered. ## Description Pass params[:search_term] from the products index action to ProductsQuery, matching the convention used by the other V1 index endpoints.
1 parent f66bb66 commit bba2f03

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

app/controllers/api/v1/products_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def show
4949
def index
5050
result = ::ProductsQuery.call(
5151
organization: current_organization,
52+
search_term: params[:search_term],
5253
pagination: {
5354
page: params[:page],
5455
limit: params[:per_page] || PER_PAGE

spec/requests/api/v1/products_controller_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@
129129

130130
expect(json[:products].map { it[:lago_id] }).not_to include(other.id)
131131
end
132+
133+
context "with a search term" do
134+
subject { get_with_token(organization, "/api/v1/products?search_term=#{search_term}") }
135+
136+
let(:search_term) { "matching" }
137+
let(:matching) { create(:product, organization:, name: "matching product") }
138+
let(:other) { create(:product, organization:, name: "other product") }
139+
140+
before do
141+
matching
142+
other
143+
end
144+
145+
it "returns only the products matching the search term" do
146+
subject
147+
148+
expect(response).to have_http_status(:success)
149+
expect(json[:products].map { it[:lago_id] }).to eq([matching.id])
150+
end
151+
end
132152
end
133153

134154
describe "DELETE /api/v1/products/:code" do

0 commit comments

Comments
 (0)