Skip to content

Commit

Permalink
🛂 Add setCustomPlan script
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Nov 16, 2022
1 parent 4e6b8ed commit 6fdbf98
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 10 deletions.
8 changes: 7 additions & 1 deletion packages/scripts/.env.local.example
Original file line number Diff line number Diff line change
@@ -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=
15 changes: 7 additions & 8 deletions packages/scripts/index.ts
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*"
Expand Down
83 changes: 83 additions & 0 deletions packages/scripts/setCustomPlan.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6fdbf98

Please sign in to comment.