🛂 Add checkSubscriptions script
This commit is contained in:
46
packages/scripts/checkSubscriptionsStatus.ts
Normal file
46
packages/scripts/checkSubscriptionsStatus.ts
Normal file
@@ -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()
|
||||||
@@ -14,7 +14,8 @@
|
|||||||
"db:fixTypebots": "tsx fixTypebots.ts",
|
"db:fixTypebots": "tsx fixTypebots.ts",
|
||||||
"telemetry:sendTotalResultsDigest": "tsx sendTotalResultsDigest.ts",
|
"telemetry:sendTotalResultsDigest": "tsx sendTotalResultsDigest.ts",
|
||||||
"sendAlertEmails": "tsx sendAlertEmails.ts",
|
"sendAlertEmails": "tsx sendAlertEmails.ts",
|
||||||
"inspectUser": "tsx inspectUser.ts"
|
"inspectUser": "tsx inspectUser.ts",
|
||||||
|
"checkSubscriptionsStatus": "tsx checkSubscriptionsStatus.ts"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@typebot.io/emails": "workspace:*",
|
"@typebot.io/emails": "workspace:*",
|
||||||
|
|||||||
Reference in New Issue
Block a user