feat(tier-client): emit entitlement.tier_changed event (best-effort, opt-in) - #1822
Open
dzehnder wants to merge 1 commit into
Open
feat(tier-client): emit entitlement.tier_changed event (best-effort, opt-in)#1822dzehnder wants to merge 1 commit into
dzehnder wants to merge 1 commit into
Conversation
…opt-in) Emit an `entitlement.tier_changed` domain event from the single choke point where a tier changes (`TierClient.createEntitlement`): on a fresh create (`from: null`) and on a real tier transition (`from: prevTier`). No event on an unchanged tier, an untouched PAID entitlement, or an enrollment-only add. Emission is opt-in and best-effort so the 6+ existing consumers are unaffected: it is a no-op unless the context provides both `context.sqs` (the shared SQS helper) and an `ENTITLEMENT_EVENTS_QUEUE_URL` env var, and a publish failure is logged and swallowed — it never fails createEntitlement/save. No new dependency, no factory/constructor signature change; additions only. This is target-architecture groundwork (ADR-0002): there is no live consumer yet — current provisioning uses the api-service endpoint + fulfillment worker. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
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.
What
Introduces an
entitlement.tier_changeddomain event, emitted from the single choke point where an entitlement tier changes —TierClient.createEntitlement(tier).Event
ENTITLEMENT_TIER_CHANGED = 'entitlement.tier_changed'(exported from the package; typedEntitlementTierChangedEventinindex.d.ts).{ from: null, to: tier }setTier+save) →{ from: prevTier, to: tier }{ "type": "entitlement.tier_changed", "entitlementId": "…", "organizationId": "…", "productCode": "LLMO", "siteId": "…|null", // null for org-only scope "enrollmentId": "…|null", // null when no enrollment is in scope "from": "FREE_TRIAL|null", // null on a fresh create "to": "PAID", "occurredAt": "2026-07-17T00:00:00.000Z" }Design: opt-in, best-effort, non-breaking
Uses the ecosystem's idiomatic publish path — the shared SQS helper on the request context (
context.sqs, provided bysqsWrapperin every SpaceCat Lambda) plus anENTITLEMENT_EVENTS_QUEUE_URLenv var. No new SDK dependency, no factory/constructor signature change — additions only.context.sqsandcontext.env.ENTITLEMENT_EVENTS_QUEUE_URL. The 6+ existing consumers configure neither, so they are entirely unaffected.log.warn) — it never propagates out ofcreateEntitlement/save.I considered a pluggable injected-publisher, but the shared
context.sqshelper is the established convention for domain-event publishing across SpaceCat services and requires zero new infra from callers, so it wins here.Target-architecture groundwork
This writes the event down as the target architecture (per the review recommendation): the eventual replacement for the current endpoint/reaction approach. There is no live consumer yet — today's provisioning is handled by the api-service endpoint + the fulfillment worker. Intended future consumers: prompt-suggestion schedule provisioning on →PAID, and a PAID→trial teardown. See the ADR:
docs/adr/0002-entitlement-tier-changed-event.md.Files changed
packages/spacecat-shared-tier-client/src/events.js(new) —ENTITLEMENT_TIER_CHANGED+ queue-url key constantspackages/spacecat-shared-tier-client/src/tier-client.js—#emitTierChangedEvent+ wiring at the create/transition pointspackages/spacecat-shared-tier-client/src/index.js/index.d.ts— export constants + typed payload/contextpackages/spacecat-shared-tier-client/test/tier-client.test.js— emission testspackages/spacecat-shared-tier-client/README.md,docs/adr/0002-entitlement-tier-changed-event.md— docsTests / lint
npm test -w packages/spacecat-shared-tier-client→ 88 passing, coverage 100% lines/statements/branches/functions.npm run lint -w packages/spacecat-shared-tier-client→ clean.🤖 Generated with Claude Code