Skip to content

Commit 2d19901

Browse files
committed
feat(products): expose product_items_count on the product API
## Context The product catalog needs the number of items per product, but returning the full items collection inline would bloat the payload. The GraphQL type already exposes product_items_count only. ## Description Add product_items_count to the REST product serializer to match the GraphQL shape, without serializing the items themselves.
1 parent b541408 commit 2d19901

2 files changed

Lines changed: 4 additions & 0 deletions

File tree

app/serializers/v1/product_serializer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def serialize
99
code: model.code,
1010
description: model.description,
1111
invoice_display_name: model.invoice_display_name,
12+
product_items_count: model.product_items.count,
1213
created_at: model.created_at.iso8601
1314
}
1415
end

spec/requests/api/v1/products_controller_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,14 @@
7676
include_examples "requires API permission", "product", "read"
7777

7878
it "returns the product" do
79+
create(:product_item, organization:, product:)
80+
7981
subject
8082

8183
expect(response).to have_http_status(:success)
8284
expect(json[:product][:lago_id]).to eq(product.id)
8385
expect(json[:product][:code]).to eq(product.code)
86+
expect(json[:product][:product_items_count]).to eq(1)
8487
end
8588

8689
context "when the product does not exist" do

0 commit comments

Comments
 (0)