Skip to content

Conversation

groyoh
Copy link
Contributor

@groyoh groyoh commented Jun 4, 2025

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:

-- 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.

@groyoh groyoh added the Coupon label Jun 5, 2025
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.
Copy link
Collaborator

@lovrocolic lovrocolic left a 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?

@groyoh
Copy link
Contributor Author

groyoh commented Jun 11, 2025

@lovrocolic yes. I opened a PR for the OpenAPI and golang client. The Ruby and Python ones are working as is.

@groyoh groyoh merged commit 46bd374 into main Jun 11, 2025
14 checks passed
@groyoh groyoh deleted the feat/add-filter-applied-coupons branch June 11, 2025 08:28
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
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants