File tree Expand file tree Collapse file tree 1 file changed +24
-6
lines changed
packages/altair-api/src/auth/user Expand file tree Collapse file tree 1 file changed +24
-6
lines changed Original file line number Diff line number Diff line change @@ -143,17 +143,35 @@ export class UserService {
143143 } ) ;
144144 }
145145
146- async getBillingUrl ( userId : string , returnUrl ? : string ) {
146+ async getStripeCustomerId ( userId : string ) {
147147 const user = await this . getUser ( userId ) ;
148- let customerId = user . stripeCustomerId ;
149148
150- if ( ! customerId ) {
151- const res = await this . stripeService . connectOrCreateCustomer ( user . email ) ;
152- customerId = res . id ;
149+ if ( user . stripeCustomerId ) {
150+ return user . stripeCustomerId ;
153151 }
154152
153+ // retrieve customer ID from Stripe
154+ const res = await this . stripeService . connectOrCreateCustomer ( user . email ) ;
155+ const customerId = res . id ;
156+
157+ // update user with customer ID
158+ await this . prisma . user . update ( {
159+ where : {
160+ id : userId ,
161+ } ,
162+ data : {
163+ stripeCustomerId : customerId ,
164+ } ,
165+ } ) ;
166+
167+ return customerId ;
168+ }
169+
170+ async getBillingUrl ( userId : string , returnUrl ?: string ) {
171+ const customerId = await this . getStripeCustomerId ( userId ) ;
172+
155173 const session = await this . stripeService . createBillingSession (
156- user . stripeCustomerId ,
174+ customerId ,
157175 returnUrl
158176 ) ;
159177
You can’t perform that action at this time.
0 commit comments