An open-source, faster Devin alternative
TypeScript
api-edge: no trial-ended email or alert for a trial that ended long ago
Igor Zalutski via Computerde44087c8425
diff --git a/cloudflare-workers/api-edge/src/autumn_webhook.ts b/cloudflare-workers/api-edge/src/autumn_webhook.tsindex bd266415..6384a0c2 100644--- a/cloudflare-workers/api-edge/src/autumn_webhook.ts+++ b/cloudflare-workers/api-edge/src/autumn_webhook.ts@@ -891,8 +891,13 @@ export async function syncAutumnToD1(env: AutumnSyncEnv, orgID: string): Promise// per org, against concurrent projections that both see the transition.const lowReason = trialReason(warning);if (lowReason && !trialReason(prevRow?.billing_warning)) await notifyTrialEvent(env, orgID, "trial_low", lowReason);+ // A trial that ran out of days long ago (an org idle since before the clock+ // shipped, or a cancelled paid org back on the trial plan) halts silently:+ // only a trial that just ended is news to the owner or to us.const endReason = trialReason(reason);- if (endReason && !wasHalted) {+ const staleEnd =+ endReason === "days" && (clock.trialStartedAt == null || nowSec - clock.trialStartedAt >= (TRIAL_DAYS + 1) * DAY_SEC);+ if (endReason && !wasHalted && !staleEnd) {await notifyTrialEvent(env, orgID, "trial_ended", endReason);await sendSlackAlert(env, trialEndedAlert(orgID, endReason));}diff --git a/cloudflare-workers/api-edge/src/loops.test.ts b/cloudflare-workers/api-edge/src/loops.test.tsindex 66ab0154..5656b678 100644--- a/cloudflare-workers/api-edge/src/loops.test.ts+++ b/cloudflare-workers/api-edge/src/loops.test.ts@@ -234,6 +234,24 @@ describe("trial events from the Autumn projection", () => {expect(loops).toEqual([expect.objectContaining({ eventName: "trial_ended", eventProperties: { reason: "minutes" } })]);});+ it("sends trial_ended when the trial's days just ran out", async () => {+ const loops = mockAutumn(100);+ const now = Math.floor(Date.now() / 1000);+ const db = fakeDb({ org: { trial_started_at: now - 3 * 86_400 - 600 } });+ await syncAutumnToD1(syncEnv(db), "org_1");+ expect(db.org.halt_reason).toBe("trial_days");+ expect(loops).toEqual([expect.objectContaining({ eventName: "trial_ended", eventProperties: { reason: "days" } })]);+ });++ it("halts a trial that ran out long ago without telling anyone", async () => {+ const loops = mockAutumn(100);+ const now = Math.floor(Date.now() / 1000);+ const db = fakeDb({ org: { trial_started_at: now - 30 * 86_400 } });+ await syncAutumnToD1(syncEnv(db), "org_1");+ expect(db.org.halt_reason).toBe("trial_days");+ expect(loops).toEqual([]);+ });+it("mirrors a plan change onto the contact", async () => {const loops: Array<Record<string, unknown>> = [];vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => {