@@ -10,20 +10,7 @@ import { TRPCError, type TRPCRouterRecord } from "@trpc/server";
1010import { z } from "zod/v4" ;
1111import type { TRPCContext } from "../trpc.js" ;
1212import { 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