Skip to content

Open source CapCut alternative.

TypeScript

feat(analytics): cancel events carry where the subscription stands

David Le52ba876f15cf
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json
index c05d0eff..146e9f5c 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/Fixtures/analytics-rollup.json
@@ -26,6 +26,8 @@
"day": "2026-08-31",
"detail": "switched_service · Export broke.",
"email": "[email protected]",
+ "ended": false,
+ "endsAt": "2026-09-08T00:00:00.000Z",
"kind": "canceled",
"objectId": "sub_1"
},
@@ -35,6 +37,8 @@
"day": "2026-08-31",
"detail": "Your card has insufficient funds.",
"email": "[email protected]",
+ "ended": false,
+ "endsAt": null,
"kind": "declined",
"objectId": "pi_3"
}
diff --git a/site/src/lib/analytics/billing.test.ts b/site/src/lib/analytics/billing.test.ts
index 68795ef4..2c420228 100644
--- a/site/src/lib/analytics/billing.test.ts
+++ b/site/src/lib/analytics/billing.test.ts
@@ -25,11 +25,11 @@ const snapshot: AnalyticsStripeSnapshot = {
refunds: [{ amountMicros: "5000000", chargeId: "ch_2", day: "2026-09-03", id: "re_1" }],
subscriptions: [
// Live, and scheduled to end through the portal's cancel_at.
- { cancelAt: "2026-09-08T00:00:00.000Z", cancelScheduled: true, canceledAt: "2026-09-02T10:00:00.000Z", comment: "Export broke.", createdAt: "2026-08-09T00:00:00.000Z", customerId: "cus_p", email: "[email protected]", feedback: "switched_service", id: "sub_1", status: "active", userId: "u_p" },
+ { cancelAt: "2026-09-08T00:00:00.000Z", cancelScheduled: true, canceledAt: "2026-09-02T10:00:00.000Z", comment: "Export broke.", createdAt: "2026-08-09T00:00:00.000Z", customerId: "cus_p", email: "[email protected]", endedAt: null, feedback: "switched_service", id: "sub_1", status: "active", userId: "u_p" },
// Live, staying.
- { cancelAt: null, cancelScheduled: false, canceledAt: null, comment: null, createdAt: "2026-09-01T00:00:00.000Z", customerId: "cus_q", email: "[email protected]", feedback: null, id: "sub_2", status: "active", userId: "u_q" },
+ { cancelAt: null, cancelScheduled: false, canceledAt: null, comment: null, createdAt: "2026-09-01T00:00:00.000Z", customerId: "cus_q", email: "[email protected]", endedAt: null, feedback: null, id: "sub_2", status: "active", userId: "u_q" },
// Ended before the window.
- { cancelAt: null, cancelScheduled: false, canceledAt: "2026-07-01T00:00:00.000Z", comment: null, createdAt: "2026-06-01T00:00:00.000Z", customerId: "cus_r", email: "[email protected]", feedback: null, id: "sub_3", status: "canceled", userId: "u_r" },
+ { cancelAt: null, cancelScheduled: false, canceledAt: "2026-07-01T00:00:00.000Z", comment: null, createdAt: "2026-06-01T00:00:00.000Z", customerId: "cus_r", email: "[email protected]", endedAt: "2026-07-01T00:00:00.000Z", feedback: null, id: "sub_3", status: "canceled", userId: "u_r" },
],
};
@@ -78,6 +78,22 @@ describe("buildBilling", () => {
["2026-09-02", "declined", "pi_4", "Your card has insufficient funds."],
]);
});
+
+ test("a cancel event says where the subscription stands", () => {
+ const cancel = billing.events.find((e) => e.kind === "canceled");
+ expect([cancel?.ended, cancel?.endsAt]).toEqual([false, "2026-09-08T00:00:00.000Z"]);
+ const stopped = buildBilling(
+ {
+ ...snapshot,
+ subscriptions: [
+ { ...snapshot.subscriptions[2], canceledAt: "2026-09-01T00:00:00.000Z", endedAt: "2026-09-01T00:00:00.000Z" },
+ ],
+ },
+ days,
+ );
+ const gone = stopped.events.find((e) => e.kind === "canceled");
+ expect([gone?.ended, gone?.endsAt]).toEqual([true, "2026-09-01T00:00:00.000Z"]);
+ });
});
describe("subscriptionCancelScheduled", () => {
diff --git a/site/src/lib/analytics/pipeline.ts b/site/src/lib/analytics/pipeline.ts
index 12c4682b..f1d88f43 100644
--- a/site/src/lib/analytics/pipeline.ts
+++ b/site/src/lib/analytics/pipeline.ts
@@ -470,6 +470,8 @@ export function buildBilling(
day,
detail: [sub.feedback, sub.comment].filter(Boolean).join(" · ") || null,
email: sub.email,
+ ended: !isActiveProStatus(sub.status),
+ endsAt: sub.cancelAt ?? sub.endedAt,
kind: "canceled",
objectId: sub.id,
});
@@ -503,6 +505,8 @@ export function buildBilling(
day: charge.day,
detail: charge.failure,
email: charge.email,
+ ended: false,
+ endsAt: null,
kind: "declined",
objectId: charge.paymentIntentId,
});
diff --git a/site/src/lib/analytics/rollup-fixture.ts b/site/src/lib/analytics/rollup-fixture.ts
index d1d6551d..4d3e9dec 100644
--- a/site/src/lib/analytics/rollup-fixture.ts
+++ b/site/src/lib/analytics/rollup-fixture.ts
@@ -37,9 +37,9 @@ const snapshot: AnalyticsSnapshotFile = {
dashboardUrl: "https://dashboard.stripe.com/test",
refunds: [{ amountMicros: "1000000", chargeId: "ch_2", day: "2026-09-02", id: "re_1" }],
subscriptions: [
- { cancelAt: "2026-09-08T00:00:00.000Z", cancelScheduled: true, canceledAt: "2026-08-31T10:00:00.000Z", comment: "Export broke.", createdAt: "2026-08-09T00:00:00.000Z", customerId: "cus_p", email: "[email protected]", feedback: "switched_service", id: "sub_1", status: "active", userId: "u_pro" },
- { cancelAt: null, cancelScheduled: false, canceledAt: null, comment: null, createdAt: "2026-07-01T00:00:00.000Z", customerId: "cus_s", email: "[email protected]", feedback: null, id: "sub_2", status: "active", userId: "u_su" },
- { cancelAt: null, cancelScheduled: false, canceledAt: "2026-07-15T00:00:00.000Z", comment: null, createdAt: "2026-06-01T00:00:00.000Z", customerId: "cus_r", email: "[email protected]", feedback: null, id: "sub_3", status: "canceled", userId: "u_gone" },
+ { cancelAt: "2026-09-08T00:00:00.000Z", cancelScheduled: true, canceledAt: "2026-08-31T10:00:00.000Z", comment: "Export broke.", createdAt: "2026-08-09T00:00:00.000Z", customerId: "cus_p", email: "[email protected]", endedAt: null, feedback: "switched_service", id: "sub_1", status: "active", userId: "u_pro" },
+ { cancelAt: null, cancelScheduled: false, canceledAt: null, comment: null, createdAt: "2026-07-01T00:00:00.000Z", customerId: "cus_s", email: "[email protected]", endedAt: null, feedback: null, id: "sub_2", status: "active", userId: "u_su" },
+ { cancelAt: null, cancelScheduled: false, canceledAt: "2026-07-15T00:00:00.000Z", comment: null, createdAt: "2026-06-01T00:00:00.000Z", customerId: "cus_r", email: "[email protected]", endedAt: "2026-07-01T00:00:00.000Z", feedback: null, id: "sub_3", status: "canceled", userId: "u_gone" },
],
},
users: [
diff --git a/site/src/lib/analytics/schema.ts b/site/src/lib/analytics/schema.ts
index 4f3eb4fb..83b387aa 100644
--- a/site/src/lib/analytics/schema.ts
+++ b/site/src/lib/analytics/schema.ts
@@ -81,6 +81,8 @@ export const analyticsStripeSnapshotSchema = z.object({
// When it ends (or ended), and when the person asked for that.
cancelAt: z.string().nullable(),
canceledAt: z.string().nullable(),
+ // When it stopped, once it has.
+ endedAt: z.string().nullable().catch(null),
// What they told the portal on the way out.
feedback: z.string().nullable(),
comment: z.string().nullable(),
@@ -251,6 +253,10 @@ export const analyticsRollupSchema = z.object({
amountMicros: z.string().nullable(),
// The decline reason, or the cancel feedback and comment.
detail: z.string().nullable(),
+ // For a cancel: when the subscription ends (or ended), and whether
+ // it has already stopped.
+ endsAt: z.string().nullable().catch(null),
+ ended: z.boolean().catch(false),
}),
),
})
diff --git a/site/src/lib/analytics/stripe.ts b/site/src/lib/analytics/stripe.ts
index 86a47c9f..ca7a92b2 100644
--- a/site/src/lib/analytics/stripe.ts
+++ b/site/src/lib/analytics/stripe.ts
@@ -78,6 +78,7 @@ export async function pullStripeSnapshot(since: string): Promise<AnalyticsStripe
createdAt: isoOfUnix(sub.created),
customerId: who.customerId,
email: who.email,
+ endedAt: sub.ended_at !== null ? isoOfUnix(sub.ended_at) : null,
feedback: sub.cancellation_details?.feedback ?? null,
id: sub.id,
status: sub.status,