-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👷 Add convenient script for migrating Stripe prices
- Loading branch information
1 parent
be9daee
commit 11186d8
Showing
4 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { PrismaClient } from '@typebot.io/prisma' | ||
import { promptAndSetEnvironment } from './utils' | ||
|
||
const getUsage = async () => { | ||
await promptAndSetEnvironment() | ||
const prisma = new PrismaClient({ | ||
log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'], | ||
}) | ||
|
||
prisma.$on('query', (e) => { | ||
console.log(e.query) | ||
console.log(e.params) | ||
console.log(e.duration, 'ms') | ||
}) | ||
|
||
const count = await prisma.result.count({ | ||
where: { | ||
typebot: { workspaceId: '' }, | ||
hasStarted: true, | ||
createdAt: { | ||
gte: '2023-09-18T00:00:00.000Z', | ||
}, | ||
}, | ||
}) | ||
|
||
console.log(count) | ||
} | ||
|
||
getUsage() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { PrismaClient } from '@typebot.io/prisma' | ||
import { promptAndSetEnvironment } from './utils' | ||
import { writeFileSync } from 'fs' | ||
import Stripe from 'stripe' | ||
|
||
const migrateSubscriptionItemPriceId = async () => { | ||
await promptAndSetEnvironment() | ||
const prisma = new PrismaClient({ | ||
log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'], | ||
}) | ||
|
||
prisma.$on('query', (e) => { | ||
console.log(e.query) | ||
console.log(e.params) | ||
console.log(e.duration, 'ms') | ||
}) | ||
|
||
if ( | ||
!process.env.STRIPE_STARTER_CHATS_PRICE_ID_OLD || | ||
!process.env.STRIPE_STARTER_CHATS_PRICE_ID || | ||
!process.env.STRIPE_PRO_CHATS_PRICE_ID_OLD || | ||
!process.env.STRIPE_PRO_CHATS_PRICE_ID || | ||
!process.env.STRIPE_SECRET_KEY | ||
) | ||
throw new Error('Missing some env variables') | ||
|
||
const workspacesWithPaidPlan = await prisma.workspace.findMany({ | ||
where: { | ||
plan: { | ||
in: ['PRO', 'STARTER'], | ||
}, | ||
isSuspended: false, | ||
}, | ||
select: { | ||
plan: true, | ||
name: true, | ||
id: true, | ||
stripeId: true, | ||
isQuarantined: true, | ||
members: { | ||
select: { | ||
user: { | ||
select: { email: true }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
writeFileSync( | ||
'./workspacesWithPaidPlan.json', | ||
JSON.stringify(workspacesWithPaidPlan, null, 2) | ||
) | ||
|
||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { | ||
apiVersion: '2022-11-15', | ||
}) | ||
|
||
let i = 0 | ||
for (const workspace of workspacesWithPaidPlan) { | ||
i += 1 | ||
console.log( | ||
`(${i} / ${workspacesWithPaidPlan.length})`, | ||
'Migrating workspace:', | ||
workspace.id, | ||
workspace.name, | ||
workspace.stripeId, | ||
JSON.stringify(workspace.members.map((member) => member.user.email)) | ||
) | ||
if (!workspace.stripeId) { | ||
console.log('No stripe ID, skipping...') | ||
continue | ||
} | ||
|
||
const subscriptions = await stripe.subscriptions.list({ | ||
customer: workspace.stripeId, | ||
}) | ||
|
||
const currentSubscription = subscriptions.data | ||
.filter((sub) => ['past_due', 'active'].includes(sub.status)) | ||
.sort((a, b) => a.created - b.created) | ||
.shift() | ||
|
||
if (!currentSubscription) { | ||
console.log('No current subscription in workspace:', workspace.id) | ||
continue | ||
} | ||
|
||
const subscriptionItem = currentSubscription.items.data.find( | ||
(item) => | ||
item.price.id === process.env.STRIPE_STARTER_CHATS_PRICE_ID_OLD || | ||
item.price.id === process.env.STRIPE_PRO_CHATS_PRICE_ID_OLD | ||
) | ||
|
||
if (!subscriptionItem) { | ||
console.log('Could not find subscriptio item. Skipping...') | ||
continue | ||
} | ||
|
||
await stripe.subscriptionItems.update(subscriptionItem.id, { | ||
price: | ||
workspace.plan === 'STARTER' | ||
? process.env.STRIPE_STARTER_CHATS_PRICE_ID | ||
: process.env.STRIPE_PRO_CHATS_PRICE_ID, | ||
}) | ||
} | ||
} | ||
|
||
migrateSubscriptionItemPriceId() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters