Skip to content

feat(coupons): Add lifecycle webhooks - #6306

Merged
groyoh merged 5 commits into
getlago:mainfrom
avanish-garg:feature/coupon-lifecycle-webhooks
Oct 19, 2026
Merged

groyoh merged 5 commits into
getlago:mainfrom
avanish-garg:feature/coupon-lifecycle-webhooks

Conversation

@avanish-garg

@avanish-garg avanish-garg commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Context

Creating, updating or deleting a coupon sent no webhook, unlike plans, billable metrics and features. Terminating a coupon, by hand or when its expiration date passes, was not announced either, so a system that mirrors coupons from the webhooks could not keep them in sync.

Description

Four webhooks are added, with the coupon object as payload (the same object the coupon endpoints return):

  • coupon.created, coupon.updated and coupon.deleted, sent by the create, update and destroy services.
  • coupon.terminated, sent by Coupons::TerminateService when it terminates a coupon. This covers the terminateCoupon mutation and the hourly job for expired coupons, which now calls the same service for each coupon. Nothing is sent when the coupon was already terminated.

The types are declared in config/webhook_event_types.yml under a new Coupons category, so a webhook endpoint can subscribe to them and the webhook form lists them. The GraphQL schema was regenerated, since its event type and category enums are built from that file. Endpoints that listen to all events receive the new webhooks without any change.

The webhook specs compare the whole payload, so a missing or renamed field is caught.

Fixes #6307

@cla-check-bot

cla-check-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown

Welcome, @avanish-garg!

Thanks for your first contribution!

Before we proceed with the review, please sign the Fiduciary License Agreement:

Sign the FLA

Once signed, this PR will be automatically updated.

@cla-check-bot

cla-check-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown

Thanks, @avanish-garg! 🎉

Your CLA has been signed and is now on file. We'll proceed with the review shortly.

@groyoh groyoh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some points may block the merge: the ⚠️ comments change what API users get and should be fixed first.

  • The branch is based on a main from early September and needs a rebase. spec/models/webhook_endpoint_spec.rb was added on main since then and fails with this change (see the inline comment).
  • A coupon also changes state when it is terminated, through the terminateCoupon GraphQL mutation or the daily Clock::TerminateCouponsJob for expired coupons (Coupons::TerminateService). The payload exposes terminated_at, yet no webhook is sent for that change, so a consumer that mirrors coupons from these webhooks keeps them active. Sending coupon.updated from Coupons::TerminateService (both #call and .terminate_all_expired) would cover it.
  • The new webhooks are not documented in getlago/lago-openapi (src/webhooks/ has billable_metric_created.yaml but nothing for coupons). A matching PR there would let SDK users and the docs see the payload.

Comment thread app/jobs/send_webhook_job.rb Resolved
@groyoh
groyoh force-pushed the feature/coupon-lifecycle-webhooks branch from 2bfcc07 to 510adf3 Compare October 15, 2026 17:03
@groyoh

groyoh commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

The branch was rebased on main. Terminating a coupon now sends a new coupon.terminated webhook, from the terminateCoupon mutation and from the hourly expiry job, which now reuses Coupons::TerminateService#call (d2cf5c5).

@groyoh groyoh changed the title feat(coupons): add lifecycle webhooks feat(coupons): Add lifecycle webhooks Oct 19, 2026
avanish-garg and others added 5 commits October 19, 2026 11:01
coupon.created/updated/deleted webhook event names already existed
(added for activity logs), but nothing ever fired them -- sibling
catalog resources Plan/BillableMetric/Feature all fire webhooks on
create/update/destroy, Coupon didn't.

Adds Webhooks::Coupons::{Created,Updated,Deleted}Service mirroring
the existing BillableMetrics webhook services, registers them in
SendWebhookJob::WEBHOOK_SERVICES, and wires the SendWebhookJob calls
into Coupons::{Create,Update,Destroy}Service.
## Context

Webhook endpoints can only subscribe to the event types declared in `config/webhook_event_types.yml`. The `coupon.*` webhooks were added to `SendWebhookJob::WEBHOOK_SERVICES` without being declared there, so an endpoint that selects its events could not subscribe to them, and the webhook form did not list them. `spec/models/webhook_endpoint_spec.rb` also requires every handler type to be declared.

## Description

`coupon.created`, `coupon.updated` and `coupon.deleted` are declared under a new `Coupons` category. The GraphQL schema was dumped again, since `EventTypeEnum` and `EventCategoryEnum` are built from this file.
## Context

A coupon is terminated by the `terminateCoupon` GraphQL mutation, or by `Clock::TerminateCouponsJob` once its `expiration_at` has passed. The coupon webhook payload exposes `status` through `terminated_at`, but no webhook was sent on termination, so a consumer that mirrors coupons from the webhooks kept them active.

## Description

`Coupons::TerminateService` now sends `coupon.updated` when it terminates a coupon, both for a single coupon and for the expired ones. Nothing is sent when the coupon was already terminated.
## Context

Termination is a distinct step in a coupon's life, like `subscription.terminated` for subscriptions. Sending it as `coupon.updated` made it hard to tell apart from an edit. The expired coupons were also terminated by a loop that repeated the body of `Coupons::TerminateService#call`.

## Description

`Coupons::TerminateService` now sends a new `coupon.terminated` webhook, declared in `config/webhook_event_types.yml`, in place of `coupon.updated`. `terminate_all_expired` calls the service for each expired coupon, so both paths terminate and notify the same way.
## Context

The coupon webhook specs only checked a few keys of the payload, mostly by type, through the `creates webhook` shared example, which ignores the keys it is not given. A field missing from the payload, renamed or with a wrong value was not caught.

## Description

Each spec now builds a percentage coupon with a recurring frequency, a time limit and a plan and a billable metric as targets, so most fields hold a real value, and compares the whole payload with `eq`. The terminated spec also checks `terminated_at`.
@groyoh
groyoh force-pushed the feature/coupon-lifecycle-webhooks branch from 377561b to b5230a9 Compare October 19, 2026 09:01
@groyoh
groyoh merged commit 3d70855 into getlago:main Oct 19, 2026
14 checks passed
groyoh added a commit to getlago/lago-openapi that referenced this pull request Oct 19, 2026
## Context

lago-api sends `coupon.created`, `coupon.updated`, `coupon.deleted` and `coupon.terminated` webhooks once getlago/lago-api#6306 is merged. The payload is the coupon object, the same one the coupon endpoints return. These webhooks are not documented yet.

## Description

The four webhooks are documented with `CouponObject` as payload, following the billable metric and plan webhooks. `coupon.terminated` is sent when a coupon is terminated, by hand or because its expiration date has passed.

This PR should be merged after getlago/lago-api#6306.
@groyoh

groyoh commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Thanks for you contribution @avanish-garg !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT]: Coupon lifecycle (create/update/destroy) never fires webhooks

2 participants