Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/rpc/src/orpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { runTracked } from "./middleware/track-mutation";
import { runAuditedMutation } from "./middleware/audit-mutation";
import { type BillingOwner, getBillingOwner } from "./utils/billing";
import { logger } from "./lib/logger";
import { getOrganizationOwnerId } from "./utils/organization";

export interface PreResolvedAuth {
Expand Down Expand Up @@ -104,13 +105,20 @@ export const createRPCContext = async (
if (billingResolved) {
return billingCache;
}
if (user) {
billingCache = await getBillingOwner(user.id, organizationId);
} else if (apiKey?.organizationId) {
const ownerId = await getOrganizationOwnerId(apiKey.organizationId);
if (ownerId) {
billingCache = await getBillingOwner(ownerId, apiKey.organizationId);
try {
if (user) {
billingCache = await getBillingOwner(user.id, organizationId);
} else if (apiKey?.organizationId) {
const ownerId = await getOrganizationOwnerId(apiKey.organizationId);
if (ownerId) {
billingCache = await getBillingOwner(ownerId, apiKey.organizationId);
}
}
} catch (error) {
logger.warn(
{ error },
"Failed to resolve billing owner; falling back to free tier"
);
}
Comment on lines +117 to 122

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Usage fallback still fails

When AUTUMN_SECRET_KEY is missing, this catch makes context.getBilling() return undefined, but getUsage then calls getAutumn().check() directly. That call throws the same missing-secret error and is converted into an internal RPC error, so local environments still cannot retrieve usage through the intended free-tier fallback. The fallback must also cover or avoid that second Autumn call.

billingResolved = true;
Comment on lines +117 to 123

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Fallback behavior lacks coverage

The new exception-to-free-tier behavior has no direct test coverage. Existing procedure tests inject mocked getBilling implementations instead of invoking this closure, so regressions in the missing-secret fallback, warning, or resolved-state behavior could go unnoticed. Please add focused tests that cover returning undefined, logging the fallback, and avoiding another lookup on later calls.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

return billingCache;
Expand Down
Loading