-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e6b8ed
commit 6fdbf98
Showing
5 changed files
with
100 additions
and
10 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
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= |
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 |
---|---|---|
@@ -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() |
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,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!') | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.