@@ -27,6 +27,7 @@ import { orgDomainSchema, orgNameSchema, repositoryQuerySchema } from "./lib/sch
2727import { RepositoryQuery } from "./lib/types" ;
2828import { MOBILE_UNSUPPORTED_SPLASH_SCREEN_DISMISSED_COOKIE_NAME } from "./lib/constants" ;
2929import { stripeClient } from "./lib/stripe" ;
30+ import { IS_BILLING_ENABLED } from "./lib/stripe" ;
3031
3132const ajv = new Ajv ( {
3233 validateFormats : false ,
@@ -174,19 +175,31 @@ export const completeOnboarding = async (domain: string): Promise<{ success: boo
174175 return notFound ( ) ;
175176 }
176177
177- const subscription = await fetchSubscription ( domain ) ;
178- if ( isServiceError ( subscription ) ) {
179- return subscription ;
180- }
178+ // If billing is not enabled, we can just mark the org as onboarded.
179+ if ( ! IS_BILLING_ENABLED ) {
180+ await prisma . org . update ( {
181+ where : { id : orgId } ,
182+ data : {
183+ isOnboarded : true ,
184+ }
185+ } ) ;
181186
182- await prisma . org . update ( {
183- where : { id : orgId } ,
184- data : {
185- isOnboarded : true ,
186- stripeSubscriptionStatus : StripeSubscriptionStatus . ACTIVE ,
187- stripeLastUpdatedAt : new Date ( ) ,
187+ // Else, validate that the org has an active subscription.
188+ } else {
189+ const subscriptionOrError = await fetchSubscription ( domain ) ;
190+ if ( isServiceError ( subscriptionOrError ) ) {
191+ return subscriptionOrError ;
188192 }
189- } ) ;
193+
194+ await prisma . org . update ( {
195+ where : { id : orgId } ,
196+ data : {
197+ isOnboarded : true ,
198+ stripeSubscriptionStatus : StripeSubscriptionStatus . ACTIVE ,
199+ stripeLastUpdatedAt : new Date ( ) ,
200+ }
201+ } ) ;
202+ }
190203
191204 return {
192205 success : true ,
@@ -708,9 +721,9 @@ export const redeemInvite = async (inviteId: string): Promise<{ success: boolean
708721 }
709722
710723 const res = await prisma . $transaction ( async ( tx ) => {
711- // @note : we need to use the internal subscription fetch here since we would otherwise fail the `withOrgMembership` check.
712- const subscription = await _fetchSubscriptionForOrg ( invite . orgId , tx ) ;
713- if ( subscription ) {
724+ if ( IS_BILLING_ENABLED ) {
725+ // @note : we need to use the internal subscription fetch here since we would otherwise fail the `withOrgMembership` check.
726+ const subscription = await _fetchSubscriptionForOrg ( invite . orgId , tx ) ;
714727 if ( isServiceError ( subscription ) ) {
715728 return subscription ;
716729 }
@@ -880,8 +893,7 @@ export const createOnboardingSubscription = async (domain: string) =>
880893 } satisfies ServiceError ;
881894 }
882895
883- // @nocheckin
884- const test_clock = env . AUTH_URL !== "https://app.sourcebot.dev" ? await stripeClient . testHelpers . testClocks . create ( {
896+ const test_clock = env . STRIPE_ENABLE_TEST_CLOCKS === 'true' ? await stripeClient . testHelpers . testClocks . create ( {
885897 frozen_time : Math . floor ( Date . now ( ) / 1000 )
886898 } ) : null ;
887899
@@ -911,7 +923,7 @@ export const createOnboardingSubscription = async (domain: string) =>
911923 } ) ( ) ;
912924
913925 const existingSubscription = await fetchSubscription ( domain ) ;
914- if ( existingSubscription && ! isServiceError ( existingSubscription ) ) {
926+ if ( ! isServiceError ( existingSubscription ) ) {
915927 return {
916928 statusCode : StatusCodes . BAD_REQUEST ,
917929 errorCode : ErrorCode . SUBSCRIPTION_ALREADY_EXISTS ,
@@ -1063,7 +1075,7 @@ export const getCustomerPortalSessionLink = async (domain: string): Promise<stri
10631075 } , /* minRequiredRole = */ OrgRole . OWNER )
10641076 ) ;
10651077
1066- export const fetchSubscription = ( domain : string ) : Promise < Stripe . Subscription | null | ServiceError > =>
1078+ export const fetchSubscription = ( domain : string ) : Promise < Stripe . Subscription | ServiceError > =>
10671079 withAuth ( async ( session ) =>
10681080 withOrgMembership ( session , domain , async ( { orgId } ) => {
10691081 return _fetchSubscriptionForOrg ( orgId , prisma ) ;
@@ -1167,8 +1179,8 @@ export const removeMemberFromOrg = async (memberId: string, domain: string): Pro
11671179 return notFound ( ) ;
11681180 }
11691181
1170- const subscription = await fetchSubscription ( domain ) ;
1171- if ( subscription ) {
1182+ if ( IS_BILLING_ENABLED ) {
1183+ const subscription = await fetchSubscription ( domain ) ;
11721184 if ( isServiceError ( subscription ) ) {
11731185 return subscription ;
11741186 }
@@ -1221,8 +1233,8 @@ export const leaveOrg = async (domain: string): Promise<{ success: boolean } | S
12211233 return notFound ( ) ;
12221234 }
12231235
1224- const subscription = await fetchSubscription ( domain ) ;
1225- if ( subscription ) {
1236+ if ( IS_BILLING_ENABLED ) {
1237+ const subscription = await fetchSubscription ( domain ) ;
12261238 if ( isServiceError ( subscription ) ) {
12271239 return subscription ;
12281240 }
@@ -1323,7 +1335,7 @@ export const dismissMobileUnsupportedSplashScreen = async () => {
13231335
13241336////// Helpers ///////
13251337
1326- const _fetchSubscriptionForOrg = async ( orgId : number , prisma : Prisma . TransactionClient ) : Promise < Stripe . Subscription | null | ServiceError > => {
1338+ const _fetchSubscriptionForOrg = async ( orgId : number , prisma : Prisma . TransactionClient ) : Promise < Stripe . Subscription | ServiceError > => {
13271339 const org = await prisma . org . findUnique ( {
13281340 where : {
13291341 id : orgId ,
0 commit comments