1
1
import type Stripe from 'stripe'
2
- import type { SubscriptionModel , SubscriptionsTable } from '../../../../orm/src/models/Subscription'
2
+ import type { SubscriptionsTable } from '../../../../orm/src/models/Subscription'
3
3
import type { UserModel } from '../../../../orm/src/models/User'
4
- import { manageCustomer , managePrice , stripe } from '..'
5
-
6
- import { Subscription } from '../../../../orm/src/models/Subscription'
7
4
import { db } from '@stacksjs/database'
8
5
6
+ import { manageCustomer , managePrice , stripe } from '..'
7
+
9
8
export interface SubscriptionManager {
10
9
create : ( user : UserModel , type : string , lookupKey : string , params : Partial < Stripe . SubscriptionCreateParams > ) => Promise < Stripe . Response < Stripe . Subscription > >
11
10
update : ( user : UserModel , type : string , lookupKey : string , params : Partial < Stripe . SubscriptionUpdateParams > ) => Promise < Stripe . Response < Stripe . Subscription > >
@@ -127,23 +126,19 @@ export const manageSubscription: SubscriptionManager = (() => {
127
126
}
128
127
129
128
async function updateStoredSubscription ( subscriptionId : string ) : Promise < void > {
130
- const subscription = await Subscription . where ( 'provider_id' , subscriptionId ) . first ( )
131
-
132
- subscription ?. update ( { provider_status : 'canceled' } )
129
+ await db . updateTable ( 'subscriptions' ) . set ( { provider_status : 'canceled' } ) . where ( 'provider_id' , '=' , subscriptionId ) . executeTakeFirst ( )
133
130
}
134
131
135
- async function isActive ( subscription : SubscriptionModel ) : Promise < boolean > {
132
+ async function isActive ( subscription : SubscriptionsTable ) : Promise < boolean > {
136
133
return subscription . provider_status === 'active'
137
134
}
138
135
139
- async function isTrial ( subscription : SubscriptionModel ) : Promise < boolean > {
136
+ async function isTrial ( subscription : SubscriptionsTable ) : Promise < boolean > {
140
137
return subscription . provider_status === 'trialing'
141
138
}
142
139
143
140
async function isIncomplete ( user : UserModel , type : string ) : Promise < boolean > {
144
- const subscription = await Subscription . where ( 'user_id' , user . id )
145
- . where ( 'type' , type )
146
- . first ( )
141
+ const subscription = await db . selectFrom ( 'subscriptions' ) . where ( 'type' , '=' , type ) . selectAll ( ) . executeTakeFirst ( )
147
142
148
143
if ( ! subscription )
149
144
return false
@@ -152,9 +147,7 @@ export const manageSubscription: SubscriptionManager = (() => {
152
147
}
153
148
154
149
async function isValid ( user : UserModel , type : string ) : Promise < boolean > {
155
- const subscription = await Subscription . where ( 'user_id' , user . id )
156
- . where ( 'type' , type )
157
- . first ( )
150
+ const subscription = await db . selectFrom ( 'subscriptions' ) . where ( 'user_id' , '=' , user . id ) . where ( 'type' , '=' , type ) . selectAll ( ) . executeTakeFirst ( )
158
151
159
152
if ( ! subscription )
160
153
return false
@@ -165,7 +158,7 @@ export const manageSubscription: SubscriptionManager = (() => {
165
158
return active || trial
166
159
}
167
160
168
- async function storeSubscription ( user : UserModel , type : string , lookupKey : string , options : Stripe . Subscription ) : Promise < SubscriptionModel > {
161
+ async function storeSubscription ( user : UserModel , type : string , lookupKey : string , options : Stripe . Subscription ) : Promise < SubscriptionsTable | undefined > {
169
162
const data = removeNullValues ( {
170
163
user_id : user . id ,
171
164
type,
@@ -180,7 +173,8 @@ export const manageSubscription: SubscriptionManager = (() => {
180
173
last_used_at : options . current_period_end != null ? String ( options . current_period_end ) : undefined ,
181
174
} )
182
175
183
- const subscriptionModel = await Subscription . create ( data )
176
+ const subscriptionModelCreated = await db . insertInto ( 'subscriptions' ) . values ( data ) . executeTakeFirst ( )
177
+ const subscriptionModel = await db . selectFrom ( 'subscriptions' ) . where ( 'id' , '=' , Number ( subscriptionModelCreated . insertId ) ) . selectAll ( ) . executeTakeFirst ( )
184
178
185
179
return subscriptionModel
186
180
}
0 commit comments