2
0

🛂 Add setCustomPlan script

This commit is contained in:
Baptiste Arnaud
2022-11-16 09:04:37 +01:00
parent 4e6b8ed521
commit 6fdbf98eed
5 changed files with 100 additions and 10 deletions

View File

@ -1,2 +1,8 @@
DATABASE_URL=postgresql://postgres:typebot@localhost:5432/typebot
ENCRYPTION_SECRET=
ENCRYPTION_SECRET=
# For setCustomPlan
STRIPE_SECRET_KEY=
STRIPE_SUBSCRIPTION_ID=
STRIPE_PRODUCT_ID=
WORKSPACE_ID=

View File

@ -1,23 +1,22 @@
import { PrismaClient } from 'db'
import path from 'path'
import { setCustomPlan } from './setCustomPlan'
require('dotenv').config({
path: path.join(
__dirname,
process.env.NODE_ENV === 'staging' ? '.env.staging' : '.env.local'
process.env.NODE_ENV === 'production'
? '.env.production'
: process.env.NODE_ENV === 'staging'
? '.env.staging'
: '.env.local'
),
})
const prisma = new PrismaClient({ log: ['query', 'info', 'warn', 'error'] })
const main = async () => {
const workspaces = await prisma.workspace.findMany({
where: {
members: { some: { userId: 'coucou' } },
},
})
console.log(workspaces)
setCustomPlan()
}
main().then()

View File

@ -16,6 +16,7 @@
"emails": "workspace:*",
"got": "12.5.2",
"models": "workspace:*",
"stripe": "10.17.0",
"tsx": "3.12.1",
"typescript": "4.8.4",
"utils": "workspace:*"

View File

@ -0,0 +1,83 @@
import { Plan, PrismaClient } from 'db'
import Stripe from 'stripe'
const prisma = new PrismaClient()
export const setCustomPlan = async () => {
if (
!process.env.STRIPE_SECRET_KEY ||
!process.env.STRIPE_PRODUCT_ID ||
!process.env.STRIPE_SUBSCRIPTION_ID ||
!process.env.WORKSPACE_ID
)
throw Error(
'STRIPE_SECRET_KEY or STRIPE_SUBSCRIPTION_ID or STRIPE_PRODUCT_ID or process.env.WORKSPACE_ID var is missing'
)
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2022-08-01',
})
const claimablePlan = await prisma.claimableCustomPlan.findFirst({
where: { workspaceId: process.env.WORKSPACE_ID, claimedAt: null },
})
if (!claimablePlan) throw Error('No claimable plan found')
console.log('Claimable plan found')
const { items: existingItems } = await stripe.subscriptions.retrieve(
process.env.STRIPE_SUBSCRIPTION_ID
)
if (existingItems.data.length === 0) return
const planItem = existingItems.data.find(
(item) => item.plan.product === process.env.STRIPE_PRODUCT_ID
)
if (!planItem) throw Error("Couldn't find plan item")
console.log('Updating subscription...')
await stripe.subscriptions.update(process.env.STRIPE_SUBSCRIPTION_ID, {
items: [
{
id: planItem.id,
price_data: {
currency: 'usd',
tax_behavior: 'exclusive',
recurring: { interval: 'month' },
product: process.env.STRIPE_PRODUCT_ID,
unit_amount: claimablePlan.price * 100,
},
},
...existingItems.data
.filter((item) => item.plan.product !== process.env.STRIPE_PRODUCT_ID)
.map((item) => ({ id: item.id, deleted: true })),
],
})
console.log('Subscription updated!')
console.log('Updating workspace...')
await prisma.workspace.update({
where: { id: process.env.WORKSPACE_ID },
data: {
plan: Plan.CUSTOM,
customChatsLimit: claimablePlan.chatsLimit,
customSeatsLimit: claimablePlan.seatsLimit,
customStorageLimit: claimablePlan.storageLimit,
},
})
console.log('Workspace updated!')
console.log('Updating claimable plan...')
await prisma.claimableCustomPlan.update({
where: { id: claimablePlan.id },
data: { claimedAt: new Date() },
})
console.log('Claimable plan updated!')
}

3
pnpm-lock.yaml generated
View File

@ -549,6 +549,7 @@ importers:
emails: workspace:*
got: 12.5.2
models: workspace:*
stripe: 10.17.0
tsx: 3.12.1
typescript: 4.8.4
utils: workspace:*
@ -559,6 +560,7 @@ importers:
emails: link:../emails
got: 12.5.2
models: link:../models
stripe: 10.17.0
tsx: 3.12.1
typescript: 4.8.4
utils: link:../utils
@ -16933,7 +16935,6 @@ packages:
dependencies:
'@types/node': 18.11.9
qs: 6.11.0
dev: false
/strnum/1.0.5:
resolution: {integrity: sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA==}