Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(billing): wire batch billing onto bill_team_7 (#4008)

firecrawl-spring[bot]ef8ca672d63d
diff --git a/apps/api/src/db/rpc.ts b/apps/api/src/db/rpc.ts
index 59ed4284d..02fc53640 100644
--- a/apps/api/src/db/rpc.ts
+++ b/apps/api/src/db/rpc.ts
@@ -73,17 +73,16 @@ export function agentConsumeFreeRequestIfLeft(
);
}
-export function billTeam6(params: {
+export function billTeam7(params: {
team_id: string;
subscription_id: string | null;
- fetch_subscription: boolean;
credits: number;
api_key_id: number | null;
is_extract: boolean;
}): Promise<{ api_key: string }[]> {
return execRows(
db,
- sql`select * from bill_team_6(_team_id => ${params.team_id}, sub_id => ${params.subscription_id}, fetch_subscription => ${params.fetch_subscription}, credits => ${params.credits}, i_api_key_id => ${params.api_key_id}, is_extract_param => ${params.is_extract})`,
+ sql`select * from bill_team_7(_team_id => ${params.team_id}, sub_id => ${params.subscription_id}, credits => ${params.credits}, i_api_key_id => ${params.api_key_id}, is_extract_param => ${params.is_extract})`,
);
}
diff --git a/apps/api/src/services/billing/__tests__/batch_billing.test.ts b/apps/api/src/services/billing/__tests__/batch_billing.test.ts
index d79762a20..d687f27d7 100644
--- a/apps/api/src/services/billing/__tests__/batch_billing.test.ts
+++ b/apps/api/src/services/billing/__tests__/batch_billing.test.ts
@@ -10,7 +10,7 @@ const {
withAuth,
trackCredits,
refundCredits,
- billTeam6,
+ billTeam7,
} = vi.hoisted(() => {
const logger: any = {
info: vi.fn(),
@@ -24,7 +24,7 @@ const {
withAuth: vi.fn((fn: any) => fn),
trackCredits: vi.fn<(args: any) => Promise<boolean>>(),
refundCredits: vi.fn<(args: any) => Promise<void>>(),
- billTeam6: vi.fn<(params: any) => Promise<{ api_key: string }[]>>(),
+ billTeam7: vi.fn<(params: any) => Promise<{ api_key: string }[]>>(),
};
});
@@ -50,7 +50,7 @@ vi.mock("../../autumn/autumn.service", () => ({
}));
vi.mock("../../../db/rpc", () => ({
- billTeam6,
+ billTeam7,
}));
let queue: string[] = [];
@@ -136,7 +136,7 @@ beforeEach(() => {
queue = [];
billedTeams.clear();
locks.clear();
- billTeam6.mockResolvedValue([]);
+ billTeam7.mockResolvedValue([]);
trackCredits.mockResolvedValue(true);
refundCredits.mockResolvedValue(undefined);
});
@@ -150,7 +150,7 @@ describe("processBillingBatch", () => {
await processBillingBatch();
- expect(billTeam6).toHaveBeenCalled();
+ expect(billTeam7).toHaveBeenCalled();
expect(trackCredits).not.toHaveBeenCalled();
expect(captureException).not.toHaveBeenCalled();
});
@@ -160,13 +160,13 @@ describe("processBillingBatch", () => {
await processBillingBatch();
- expect(billTeam6).toHaveBeenCalled();
+ expect(billTeam7).toHaveBeenCalled();
expect(trackCredits).not.toHaveBeenCalled();
});
it("refunds request-tracked credits when billing returns success false", async () => {
queue = [makeOp({ autumnTrackInRequest: true })];
- billTeam6.mockRejectedValueOnce(new Error("db failed"));
+ billTeam7.mockRejectedValueOnce(new Error("db failed"));
await processBillingBatch();
@@ -186,7 +186,7 @@ describe("processBillingBatch", () => {
it("captures exceptions and refunds when billing throws", async () => {
queue = [makeOp({ autumnTrackInRequest: true })];
- billTeam6.mockRejectedValueOnce(new Error("rpc exploded"));
+ billTeam7.mockRejectedValueOnce(new Error("rpc exploded"));
await processBillingBatch();
@@ -217,7 +217,7 @@ describe("processBillingBatch", () => {
autumnTrackInRequest: true,
}),
];
- billTeam6
+ billTeam7
.mockRejectedValueOnce(new Error("db failed"))
.mockResolvedValueOnce([]);
refundCredits.mockRejectedValueOnce(new Error("refund failed"));
@@ -235,7 +235,7 @@ describe("processBillingBatch", () => {
},
featureId: "CREDITS",
});
- expect(billTeam6).toHaveBeenCalledTimes(2);
+ expect(billTeam7).toHaveBeenCalledTimes(2);
// The batch never tracks usage to Autumn, regardless of the request-time flag.
expect(trackCredits).not.toHaveBeenCalled();
expect(captureException).toHaveBeenCalled();
diff --git a/apps/api/src/services/billing/batch_billing.ts b/apps/api/src/services/billing/batch_billing.ts
index d2bb64fc5..7fb087d9b 100644
--- a/apps/api/src/services/billing/batch_billing.ts
+++ b/apps/api/src/services/billing/batch_billing.ts
@@ -1,6 +1,6 @@
import { logger } from "../../lib/logger";
import { getRedisConnection } from "../queue-service";
-import { billTeam6 } from "../../db/rpc";
+import { billTeam7 } from "../../db/rpc";
import * as Sentry from "@sentry/node";
import { withAuth } from "../../lib/withAuth";
import {
@@ -102,7 +102,7 @@ async function refundRequestTrackedCredits(group: GroupedBillingOperation) {
/**
* Dequeues pending billing operations from Redis, groups them by team, and
- * commits each group to Supabase via the `bill_team_6` RPC.
+ * commits each group to Supabase via the `bill_team_7` RPC.
*/
export async function processBillingBatch() {
const redis = getRedisConnection();
@@ -358,10 +358,9 @@ async function supaBillTeam(
// Perform the actual database operation
let data: { api_key: string }[];
try {
- data = await billTeam6({
+ data = await billTeam7({
team_id,
subscription_id: subscription_id ?? null,
- fetch_subscription: subscription_id === undefined,
credits,
api_key_id: api_key_id ?? null,
is_extract,