@@ -2,6 +2,7 @@ import { STRIPE_AVAILABLE, stripe } from "@cap/utils";
22import { and , eq } from "drizzle-orm" ;
33import type { PlanetScaleDatabase } from "drizzle-orm/planetscale-serverless" ;
44import type { Adapter } from "next-auth/adapters" ;
5+ import type Stripe from "stripe" ;
56import { nanoId } from "../helpers" ;
67import { accounts , sessions , users , verificationTokens } from "../schema" ;
78
@@ -25,17 +26,57 @@ export function DrizzleAdapter(db: PlanetScaleDatabase): Adapter {
2526 if ( ! row ) throw new Error ( "User not found" ) ;
2627
2728 if ( STRIPE_AVAILABLE ( ) ) {
28- const customer = await stripe ( ) . customers . create ( {
29+ const existingCustomers = await stripe ( ) . customers . list ( {
2930 email : userData . email ,
30- metadata : {
31- userId : nanoId ( ) ,
32- } ,
31+ limit : 1 ,
32+ } ) ;
33+
34+ let customer : Stripe . Customer ;
35+ if ( existingCustomers . data . length > 0 && existingCustomers . data [ 0 ] ) {
36+ customer = existingCustomers . data [ 0 ] ;
37+
38+ customer = await stripe ( ) . customers . update ( customer . id , {
39+ metadata : {
40+ ...customer . metadata ,
41+ userId : row . id ,
42+ } ,
43+ } ) ;
44+ } else {
45+ customer = await stripe ( ) . customers . create ( {
46+ email : userData . email ,
47+ metadata : {
48+ userId : row . id ,
49+ } ,
50+ } ) ;
51+ }
52+
53+ const subscriptions = await stripe ( ) . subscriptions . list ( {
54+ customer : customer . id ,
55+ status : "active" ,
56+ limit : 100 ,
3357 } ) ;
3458
59+ const inviteQuota = subscriptions . data . reduce ( ( total , sub ) => {
60+ return (
61+ total +
62+ sub . items . data . reduce (
63+ ( subTotal , item ) => subTotal + ( item . quantity || 1 ) ,
64+ 0 ,
65+ )
66+ ) ;
67+ } , 0 ) ;
68+
69+ const mostRecentSubscription = subscriptions . data [ 0 ] ;
70+
3571 await db
3672 . update ( users )
3773 . set ( {
3874 stripeCustomerId : customer . id ,
75+ ...( mostRecentSubscription && {
76+ stripeSubscriptionId : mostRecentSubscription . id ,
77+ stripeSubscriptionStatus : mostRecentSubscription . status ,
78+ inviteQuota : inviteQuota || 1 ,
79+ } ) ,
3980 } )
4081 . where ( eq ( users . id , row . id ) ) ;
4182 }
@@ -58,7 +99,6 @@ export function DrizzleAdapter(db: PlanetScaleDatabase): Adapter {
5899 . where ( eq ( users . email , email ) )
59100 . limit ( 1 )
60101 . catch ( ( e ) => {
61- console . log ( e ) ;
62102 throw e ;
63103 } ) ;
64104 const row = rows [ 0 ] ;
@@ -182,29 +222,23 @@ export function DrizzleAdapter(db: PlanetScaleDatabase): Adapter {
182222 await db . delete ( sessions ) . where ( eq ( sessions . sessionToken , sessionToken ) ) ;
183223 } ,
184224 async createVerificationToken ( verificationToken ) {
185- // First, check if a token for the given identifier already exists
186225 const existingTokens = await db
187226 . select ( )
188227 . from ( verificationTokens )
189228 . where ( eq ( verificationTokens . identifier , verificationToken . identifier ) )
190229 . limit ( 1 ) ;
191230
192- // If a token already exists, you can return the existing token
193- // or handle it based on your business logic
194231 if ( existingTokens . length > 0 ) {
195- // For example, updating the existing token:
196232 await db
197233 . update ( verificationTokens )
198234 . set ( {
199235 token : verificationToken . token ,
200236 expires : verificationToken . expires ,
201- // you may update other fields as necessary
202237 } )
203238 . where (
204239 eq ( verificationTokens . identifier , verificationToken . identifier ) ,
205240 ) ;
206241
207- // Return the updated token
208242 return await db
209243 . select ( )
210244 . from ( verificationTokens )
@@ -215,14 +249,12 @@ export function DrizzleAdapter(db: PlanetScaleDatabase): Adapter {
215249 . then ( ( rows ) => rows [ 0 ] ) ;
216250 }
217251
218- // If the token does not exist, proceed to create a new one
219252 await db . insert ( verificationTokens ) . values ( {
220253 expires : verificationToken . expires ,
221254 identifier : verificationToken . identifier ,
222255 token : verificationToken . token ,
223256 } ) ;
224257
225- // Retrieve and return the newly created token
226258 const rows = await db
227259 . select ( )
228260 . from ( verificationTokens )
0 commit comments