Repository navigation
Sync dev branch #22
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
Merged
Sync dev branch #22
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import type { Session } from "@/lib/auth"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| chargebeeCustomerId: "cust-1" as string | null, | ||
| list: vi.fn(), | ||
| process: vi.fn(async () => undefined), | ||
| syncEntitlements: vi.fn(async () => undefined), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/auth", () => ({ auth: { $context: Promise.resolve({}) } })); | ||
| vi.mock("@chargebee/better-auth", () => ({ | ||
| createChargebeeWebhookProcessor: () => ({ process: mocks.process }), | ||
| })); | ||
| vi.mock("@/plugins/chargebee-plugin", () => ({ | ||
| chargebeeClient: { subscription: { list: mocks.list } }, | ||
| chargebeePluginOptions: {}, | ||
| })); | ||
| vi.mock("@/lib/entitlements/subject", () => ({ | ||
| resolveBillingSubject: async () => ({ | ||
| customerType: "user", | ||
| referenceId: "user-1", | ||
| chargebeeCustomerId: mocks.chargebeeCustomerId, | ||
| }), | ||
| })); | ||
| vi.mock("@/lib/entitlements/sync", () => ({ | ||
| syncSubscriptionEntitlements: mocks.syncEntitlements, | ||
| })); | ||
|
|
||
| import { syncSignupBilling } from "./signup-sync"; | ||
|
|
||
| const session = { user: { id: "user-1" }, session: {} } as unknown as Session; | ||
| const live = { | ||
| subscription: { id: "sub-free", status: "active" }, | ||
| customer: { id: "cust-1" }, | ||
| }; | ||
|
|
||
| describe("syncSignupBilling", () => { | ||
| beforeEach(() => { | ||
| mocks.chargebeeCustomerId = "cust-1"; | ||
| mocks.list.mockReset(); | ||
| mocks.list.mockResolvedValue({ list: [live] }); | ||
| mocks.process.mockClear(); | ||
| mocks.syncEntitlements.mockClear(); | ||
| }); | ||
|
|
||
| it("mirrors the live subscription and fetches its entitlements", async () => { | ||
| await syncSignupBilling(session); | ||
|
|
||
| expect(mocks.list).toHaveBeenCalledWith( | ||
| expect.objectContaining({ customer_id: { is: "cust-1" }, limit: 1 }), | ||
| ); | ||
| expect(mocks.process).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| event_type: "subscription_created", | ||
| content: live, | ||
| }), | ||
| ); | ||
| expect(mocks.syncEntitlements).toHaveBeenCalledWith("sub-free", { | ||
| trigger: "signup", | ||
| }); | ||
| }); | ||
|
|
||
| it("skips users without a Chargebee customer", async () => { | ||
| mocks.chargebeeCustomerId = null; | ||
|
|
||
| await syncSignupBilling(session); | ||
|
|
||
| expect(mocks.list).not.toHaveBeenCalled(); | ||
| expect(mocks.syncEntitlements).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("skips customers without a live subscription", async () => { | ||
| mocks.list.mockResolvedValue({ list: [] }); | ||
|
|
||
| await syncSignupBilling(session); | ||
|
|
||
| expect(mocks.process).not.toHaveBeenCalled(); | ||
| expect(mocks.syncEntitlements).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("leaves the webhook path in charge when Chargebee fails", async () => { | ||
| mocks.list.mockRejectedValue(new Error("chargebee down")); | ||
|
|
||
| await expect(syncSignupBilling(session)).resolves.toBeUndefined(); | ||
| expect(mocks.syncEntitlements).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { | ||
| createChargebeeWebhookProcessor, | ||
| type ChargebeeWebhookProcessorSource, | ||
| } from "@chargebee/better-auth"; | ||
| import type { WebhookEvent } from "chargebee"; | ||
|
|
||
| import { auth, type Session } from "@/lib/auth"; | ||
| import { resolveBillingSubject } from "@/lib/entitlements/subject"; | ||
| import { syncSubscriptionEntitlements } from "@/lib/entitlements/sync"; | ||
| import { | ||
| findLiveSubscription, | ||
| type LiveSubscription, | ||
| } from "@/lib/free-subscription"; | ||
| import { | ||
| chargebeeClient, | ||
| chargebeePluginOptions, | ||
| } from "@/plugins/chargebee-plugin"; | ||
|
|
||
| /** | ||
| * A list entry carries the same `subscription` + `customer` content as the | ||
| * `subscription_created` webhook, so the plugin's own hook writes the mirror | ||
| * rows. The real webhook later finds them and skips. | ||
| */ | ||
| async function mirrorSubscription(live: LiveSubscription): Promise<void> { | ||
| const processor = createChargebeeWebhookProcessor(chargebeePluginOptions, { | ||
| auth, | ||
| } as unknown as ChargebeeWebhookProcessorSource); | ||
| await processor.process({ | ||
| id: `signup-${live.subscription.id}`, | ||
| event_type: "subscription_created", | ||
| content: live, | ||
| } as unknown as WebhookEvent); | ||
| } | ||
|
|
||
| /** | ||
| * Read-your-writes for sign-up. The free subscription was created through the | ||
| * API moments ago, so its webhook is still in flight. Pull the subscription | ||
| * and its entitlements from Chargebee instead of waiting for it. | ||
| * | ||
| * sign-up ──► Chargebee: create subscription | ||
| * home ──► Chargebee: list subscription ──► mirror rows | ||
| * ──► Chargebee: fetch entitlements ──► snapshot | ||
| * webhook ──► rows exist, skipped | ||
| */ | ||
| export async function syncSignupBilling(session: Session): Promise<void> { | ||
| try { | ||
| const subject = await resolveBillingSubject(session, { | ||
| customerType: "user", | ||
| }); | ||
| if (!subject.chargebeeCustomerId) { | ||
| return; | ||
| } | ||
|
|
||
| const live = await findLiveSubscription( | ||
| chargebeeClient, | ||
| subject.chargebeeCustomerId, | ||
| ); | ||
| if (!live) { | ||
| return; | ||
| } | ||
|
|
||
| await mirrorSubscription(live); | ||
| await syncSubscriptionEntitlements(live.subscription.id, { | ||
| trigger: "signup", | ||
| }); | ||
| } catch (error) { | ||
| // The webhook remains the correctness path; the provisioning page polls. | ||
| console.error("[signup] direct billing sync failed", error); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: chargebee/reference-architecture
Length of output: 9734
Verify the mirror row after
processor.processresolves.The processor can swallow a DB-sync hook error, so
mirrorSubscriptioncan return without creating the local subscription.syncSubscriptionEntitlementsthen emitschargebee.entitlements_sync_failedfromassertLocalSubscription, and the outer signup handler logs and suppresses the error. The webhook and home-page polling remain recovery paths, but they do not prevent this misleading signup failure event.After processing, verify that the mirror contains
live.subscription.idand throw a retryable error if it does not. Do not rely only onprocessor.processto propagate hook failures.🤖 Prompt for AI Agents