-
Notifications
You must be signed in to change notification settings - Fork 127
feat(coupons): Add coupon_code
filter for "List all applied coupons" endpoint
#3782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
groyoh
added a commit
to getlago/lago-go-client
that referenced
this pull request
Jun 5, 2025
…endpoint This commit implements the `coupon_code` filter added to the `GET /api/v1/applied_coupons` endpoint in getlago/lago-api#3782.
groyoh
added a commit
to getlago/lago-go-client
that referenced
this pull request
Jun 5, 2025
…endpoint This commit implements the `coupon_code` filter added to the `GET /api/v1/applied_coupons` endpoint in getlago/lago-api#3782.
lovrocolic
approved these changes
Jun 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
Have you checked if we should update api clients and openapi for the new filter?
@lovrocolic yes. I opened a PR for the OpenAPI and golang client. The Ruby and Python ones are working as is. |
groyoh
added a commit
to getlago/lago-openapi
that referenced
this pull request
Jun 11, 2025
…" endpoint (#373) ## Context Clients can review applied coupons using the relevant endpoint: https://getlago.com/docs/api-reference/coupons/get-all-applied. However, since there is no way to filter by coupon code, clients must page through all results to find redemptions for a specific coupon. ## Description This commit documents the `coupon_code` filter added to the `GET /api/v1/applied_coupons` endpoint in getlago/lago-api#3782.
groyoh
added a commit
to getlago/lago-go-client
that referenced
this pull request
Jun 11, 2025
…endpoint (#258) This commit implements the `coupon_code` filter added to the `GET /api/v1/applied_coupons` endpoint in getlago/lago-api#3782. Some tests cases were added for the `GET /api/v1/applied_coupons` endpoint. A new `test` CI workflow was also added to run the Golang tests.
diegocharles
pushed a commit
that referenced
this pull request
Jul 11, 2025
…" endpoint (#3782) ## Context Clients can review applied coupons using the relevant endpoint: https://getlago.com/docs/api-reference/coupons/get-all-applied. However, since there is no way to filter by coupon code, clients must page through all results to find redemptions for a specific coupon. ## Description This PR adds a `coupon_code` filter to the `GET /api/v1/applied_coupons` endpoint. Since we're joining on `coupons` and the query already includes a `WHERE` clause with `organization_id` and `coupons.deleted_at IS NULL`, we don't need any extra index as PG will rely on the existing `(organization_id,code) UNIQUE WHERE (deleted_at IS NULL)` index: ```sql -- puts AppliedCouponsQuery.call(organization: Organization.last, filters: {coupon_code: "test"}).applied_coupons.to_sql lago=# EXPLAIN SELECT "applied_coupons"."id", "applied_coupons"."coupon_id", "applied_coupons"."customer_id", "applied_coupons"."status", "applied_coupons"."amount_cents", "applied_coupons"."amount_currency", "applied_coupons"."created_at", "applied_coupons"."updated_at", "applied_coupons"."terminated_at", "applied_coupons"."percentage_rate", "applied_coupons"."frequency", "applied_coupons"."frequency_duration", "applied_coupons"."frequency_duration_remaining", "applied_coupons"."organization_id" FROM "applied_coupons" INNER JOIN "coupons" ON "applied_coupons"."coupon_id" = "coupons"."id" INNER JOIN "customers" ON "customers"."deleted_at" IS NULL AND "customers"."id" = "applied_coupons"."customer_id" WHERE "coupons"."deleted_at" IS NULL AND "coupons"."organization_id" = 'xyz' AND "customers"."deleted_at" IS NULL AND "coupons"."code" = 'test' ORDER BY "applied_coupons"."created_at" DESC, "applied_coupons"."id" ASC LIMIT 25 OFFSET 0; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=18.08..18.09 rows=1 width=160) -> Sort (cost=18.08..18.09 rows=1 width=160) Sort Key: applied_coupons.created_at DESC, applied_coupons.id -> Nested Loop (cost=4.43..18.07 rows=1 width=160) -> Nested Loop (cost=4.29..17.67 rows=1 width=160) -> Index Scan using index_coupons_on_organization_id_and_code on coupons (cost=0.12..8.14 rows=1 width=16) Index Cond: ((organization_id = 'xyz'::uuid) AND ((code)::text = 'test'::text)) -> Bitmap Heap Scan on applied_coupons (cost=4.16..9.50 rows=2 width=160) Recheck Cond: (coupon_id = coupons.id) -> Bitmap Index Scan on index_applied_coupons_on_coupon_id (cost=0.00..4.16 rows=2 width=0) Index Cond: (coupon_id = coupons.id) -> Index Scan using customers_pkey on customers (cost=0.14..0.27 rows=1 width=16) Index Cond: (id = applied_coupons.customer_id) Filter: ((deleted_at IS NULL) AND (deleted_at IS NULL)) ``` There were also some missing scenarios in the request specs such as filtering by `status` or `customer_external_id`. I added some tests for those. Note: The SQL query above show that we may have an unnecessary `.where(customers: {deleted_at: nil})` clause as the default scope of `Customer` is `-> { kept }`. This causes `"customers"."deleted_at" IS NULL` to be both in the `JOIN` and `WHERE` clauses.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
Clients can review applied coupons using the relevant endpoint: https://getlago.com/docs/api-reference/coupons/get-all-applied. However, since there is no way to filter by coupon code, clients must page through all results to find redemptions for a specific coupon.
Description
This PR adds a
coupon_code
filter to theGET /api/v1/applied_coupons
endpoint.Since we're joining on
coupons
and the query already includes aWHERE
clause withorganization_id
andcoupons.deleted_at IS NULL
, we don't need any extra index as PG will rely on the existing(organization_id,code) UNIQUE WHERE (deleted_at IS NULL)
index:There were also some missing scenarios in the request specs such as filtering by
status
orcustomer_external_id
. I added some tests for those.Note: The SQL query above show that we may have an unnecessary
.where(customers: {deleted_at: nil})
clause as the default scope ofCustomer
is-> { kept }
. This causes"customers"."deleted_at" IS NULL
to be both in theJOIN
andWHERE
clauses.