Skip to content

Commit

Permalink
🛂 Add checkSubscriptions script
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jun 23, 2023
1 parent de0b105 commit 2abce89
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
46 changes: 46 additions & 0 deletions packages/scripts/checkSubscriptionsStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { PrismaClient } from '@typebot.io/prisma'
import { promptAndSetEnvironment } from './utils'
import { Stripe } from 'stripe'

const checkSubscriptionsStatus = async () => {
await promptAndSetEnvironment()
const prisma = new PrismaClient()

const workspacesWithPaidPlan = await prisma.workspace.findMany({
where: {
plan: {
in: ['PRO', 'STARTER', 'CUSTOM'],
},
},
})

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: '2022-11-15',
})

let activeSubscriptions = 0
for (const workspace of workspacesWithPaidPlan) {
if (!workspace.stripeId) {
console.log('No stripe ID', workspace.id)
continue
}
const customer = await stripe.customers.retrieve(workspace.stripeId)
const subscription = (
await stripe.subscriptions.list({
customer: customer.id,
})
).data.at(0)
if (!subscription) {
console.log('No subscription', workspace.id)
continue
}
if (subscription.status === 'active') {
activeSubscriptions++
continue
}
console.log(`${workspace.id} - ${workspace.name} - ${subscription.status}`)
}
console.log('Active subscriptions', activeSubscriptions)
}

checkSubscriptionsStatus()
3 changes: 2 additions & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"db:fixTypebots": "tsx fixTypebots.ts",
"telemetry:sendTotalResultsDigest": "tsx sendTotalResultsDigest.ts",
"sendAlertEmails": "tsx sendAlertEmails.ts",
"inspectUser": "tsx inspectUser.ts"
"inspectUser": "tsx inspectUser.ts",
"checkSubscriptionsStatus": "tsx checkSubscriptionsStatus.ts"
},
"devDependencies": {
"@typebot.io/emails": "workspace:*",
Expand Down

0 comments on commit 2abce89

Please sign in to comment.