2022-10-27 11:32:21 +02:00
|
|
|
import {
|
|
|
|
|
CollaborationType,
|
|
|
|
|
DashboardFolder,
|
|
|
|
|
Prisma,
|
|
|
|
|
PrismaClient,
|
|
|
|
|
Workspace,
|
2023-03-15 08:35:16 +01:00
|
|
|
} from '@typebot.io/prisma'
|
2022-10-06 08:33:46 +02:00
|
|
|
import Stripe from 'stripe'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { proWorkspaceId } from '@typebot.io/lib/playwright/databaseSetup'
|
2022-10-06 08:33:46 +02:00
|
|
|
|
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
|
|
2022-11-02 19:45:46 +01:00
|
|
|
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY ?? '', {
|
2022-11-21 19:08:14 +01:00
|
|
|
apiVersion: '2022-11-15',
|
2022-10-06 08:33:46 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const addSubscriptionToWorkspace = async (
|
|
|
|
|
workspaceId: string,
|
|
|
|
|
items: Stripe.SubscriptionCreateParams.Item[],
|
|
|
|
|
metadata: Pick<
|
|
|
|
|
Workspace,
|
|
|
|
|
'additionalChatsIndex' | 'additionalStorageIndex' | 'plan'
|
|
|
|
|
>
|
|
|
|
|
) => {
|
|
|
|
|
const { id: stripeId } = await stripe.customers.create({
|
|
|
|
|
email: 'test-user@gmail.com',
|
|
|
|
|
name: 'Test User',
|
|
|
|
|
})
|
|
|
|
|
const { id: paymentId } = await stripe.paymentMethods.create({
|
|
|
|
|
card: {
|
|
|
|
|
number: '4242424242424242',
|
|
|
|
|
exp_month: 12,
|
2023-01-02 10:33:35 +01:00
|
|
|
exp_year: 2024,
|
2022-10-06 08:33:46 +02:00
|
|
|
cvc: '123',
|
|
|
|
|
},
|
|
|
|
|
type: 'card',
|
|
|
|
|
})
|
|
|
|
|
await stripe.paymentMethods.attach(paymentId, { customer: stripeId })
|
|
|
|
|
await stripe.subscriptions.create({
|
|
|
|
|
customer: stripeId,
|
|
|
|
|
items,
|
|
|
|
|
default_payment_method: paymentId,
|
2022-10-31 19:08:04 +01:00
|
|
|
currency: 'usd',
|
2022-10-06 08:33:46 +02:00
|
|
|
})
|
|
|
|
|
await stripe.customers.update(stripeId, {
|
|
|
|
|
invoice_settings: { default_payment_method: paymentId },
|
|
|
|
|
})
|
|
|
|
|
await prisma.workspace.update({
|
|
|
|
|
where: { id: workspaceId },
|
|
|
|
|
data: {
|
|
|
|
|
stripeId,
|
|
|
|
|
...metadata,
|
|
|
|
|
},
|
|
|
|
|
})
|
2023-04-13 11:39:10 +02:00
|
|
|
return stripeId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const cancelSubscription = async (stripeId: string) => {
|
|
|
|
|
const currentSubscriptionId = (
|
|
|
|
|
await stripe.subscriptions.list({
|
|
|
|
|
customer: stripeId,
|
2023-05-05 11:02:59 -04:00
|
|
|
limit: 1,
|
|
|
|
|
status: 'active',
|
2023-04-13 11:39:10 +02:00
|
|
|
})
|
|
|
|
|
).data.shift()?.id
|
|
|
|
|
if (currentSubscriptionId)
|
|
|
|
|
await stripe.subscriptions.update(currentSubscriptionId, {
|
|
|
|
|
cancel_at_period_end: true,
|
|
|
|
|
})
|
2022-10-06 08:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const createCollaboration = (
|
|
|
|
|
userId: string,
|
|
|
|
|
typebotId: string,
|
|
|
|
|
type: CollaborationType
|
|
|
|
|
) =>
|
|
|
|
|
prisma.collaboratorsOnTypebots.create({ data: { userId, typebotId, type } })
|
|
|
|
|
|
|
|
|
|
export const getSignedInUser = (email: string) =>
|
|
|
|
|
prisma.user.findFirst({ where: { email } })
|
|
|
|
|
|
|
|
|
|
export const createFolders = (partialFolders: Partial<DashboardFolder>[]) =>
|
|
|
|
|
prisma.dashboardFolder.createMany({
|
|
|
|
|
data: partialFolders.map((folder) => ({
|
|
|
|
|
workspaceId: proWorkspaceId,
|
|
|
|
|
name: 'Folder #1',
|
|
|
|
|
...folder,
|
|
|
|
|
})),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const createFolder = (workspaceId: string, name: string) =>
|
|
|
|
|
prisma.dashboardFolder.create({
|
|
|
|
|
data: {
|
|
|
|
|
workspaceId,
|
|
|
|
|
name,
|
|
|
|
|
},
|
|
|
|
|
})
|
2022-10-27 11:32:21 +02:00
|
|
|
|
|
|
|
|
export const createClaimableCustomPlan = async (
|
|
|
|
|
data: Prisma.ClaimableCustomPlanUncheckedCreateInput
|
|
|
|
|
) =>
|
|
|
|
|
prisma.claimableCustomPlan.create({
|
|
|
|
|
data,
|
|
|
|
|
})
|