Skip to content

Commit 6e3b106

Browse files
committed
clean up and fix
1 parent 31dc6b1 commit 6e3b106

File tree

3 files changed

+13
-30
lines changed

3 files changed

+13
-30
lines changed

apps/web/src/app/api/supabase/usage/route.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,10 @@ export const POST = handler(zUsageEventWebhook, async (payload) => {
2121
}
2222

2323
try {
24-
// Get or create the usage period for this user
25-
const usagePeriod = await api.usage.getCurrentUsagePeriod({
26-
user_id: payload.record.user_id,
27-
});
28-
29-
if (!usagePeriod) {
30-
throw new Error("Error getting/creating usage period");
31-
}
32-
3324
// Process the usage event with the usage period
3425
const result = await api.usage.processUsageEvent({
3526
usage_event_id: payload.record.id,
36-
usage_period_id: usagePeriod.id,
27+
user_id: payload.record.user_id,
3728
});
3829

3930
return NextResponse.json({ result, success: true });

packages/api/src/api-router/subscription.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ function getAuthHeaders(headers: Headers): Headers {
1919
return authHeaders;
2020
}
2121

22+
/**
23+
* Get the free plan from the database
24+
*/
25+
export async function getFreePlan(ctx: TRPCContext) {
26+
return ctx.db.query.Plan.findFirst({
27+
where: (plans, { eq, and }) =>
28+
and(eq(plans.active, true), eq(plans.name, "free")),
29+
});
30+
}
31+
2232
/**
2333
* Get the active subscription for a user
2434
*/

packages/api/src/api-router/usage.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,7 @@ import { TRPCError, type TRPCRouterRecord } from "@trpc/server";
1010
import { z } from "zod/v4";
1111
import type { TRPCContext } from "../trpc.js";
1212
import { publicProcedure } from "../trpc.js";
13-
import { getActiveSubscription } from "./subscription.js";
14-
15-
/**
16-
* Get the free plan from the database
17-
*/
18-
async function getFreePlan(ctx: TRPCContext) {
19-
return ctx.db.query.Plan.findFirst({
20-
where: (plans, { eq, and }) =>
21-
and(
22-
eq(plans.active, true),
23-
eq(plans.name, "free"), // Exact match for "free" plan name
24-
),
25-
});
26-
}
13+
import { getActiveSubscription, getFreePlan } from "./subscription.js";
2714

2815
/**
2916
* Get or create the current usage period for a user
@@ -121,7 +108,7 @@ async function getCurrentUsagePeriod({
121108
.values({
122109
period_end: monthEnd,
123110
period_start: monthStart,
124-
plan_id: freePlan?.id || null, // Use free plan ID if found
111+
plan_id: freePlan?.id,
125112
subscription_id: null, // Free users don't have a subscription_id
126113
user_id: userId,
127114
})
@@ -146,25 +133,20 @@ export const usageRouter = {
146133
.input(z.object({ user_id: z.string() }))
147134
.query(async ({ ctx, input }) => {
148135
try {
149-
// Get active subscription
150136
const activeSubscription = await getActiveSubscription({
151137
ctx,
152138
userId: input.user_id,
153139
});
154140

155-
// Get current usage period (creates one if it doesn't exist)
156141
const usagePeriod = await getCurrentUsagePeriod({
157142
ctx,
158143
userId: input.user_id,
159144
});
160145

161-
// Determine quota based on subscription status
162146
let quotaUsd: number;
163147
if (activeSubscription?.plan) {
164-
// Pro user: use plan quota
165148
quotaUsd = activeSubscription.plan.quota / 100;
166149
} else {
167-
// Free user: get quota from free plan
168150
const freePlan = await getFreePlan(ctx);
169151
if (!freePlan) {
170152
throw new TRPCError({

0 commit comments

Comments
 (0)